1 /**************************************************************
2 Problem: 2435
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:5292 ms
7 Memory:73348 kb
8 ****************************************************************/
9
10 //BZOJ 2435
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1001000,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31
32 int to[N<<1],nxt[N<<1],head[N],cnt,l[N<<1];
33 void add(int x,int y,int z){
34 to[++cnt]=y; nxt[cnt]=head[x]; head[x]=cnt; l[cnt]=z;
35 to[++cnt]=x; nxt[cnt]=head[y]; head[y]=cnt; l[cnt]=z;
36 }
37 int n,s[N];
38 LL ans;
39
40 int dfs(int x,int fa){
41 s[x]=1;
42 for(int i=head[x];i;i=nxt[i])
43 if (to[i]!=fa){
44 s[x]+=dfs(to[i],x);
45 ans+=(LL)abs(n-s[to[i]]*2)*l[i];
46 }
47 return s[x];
48 }
49 int main(){
50 #ifndef ONLINE_JUDGE
51 freopen("2435.in","r",stdin);
52 freopen("2435.out","w",stdout);
53 #endif
54 n=getint();
55 F(i,2,n){
56 int x=getint(),y=getint(),z=getint();
57 add(x,y,z);
58 }
59 dfs(1,0);
60 cout <<ans<<endl;
61 return 0;
62 }