小水题~
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1000010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,h[N],num=0,sz[N];
ll ans=0;
struct edge{
int to,next,val;
}data[N<<1];
inline void dfs(int x,int Fa){
sz[x]=1;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(y==Fa) continue;
dfs(y,x);sz[x]+=sz[y];ans+=(ll)abs(n-sz[y]-sz[y])*data[i].val;
}
}
int main(){
// freopen("a.in","r",stdin);
n=read();
for(int i=1;i<n;++i){
int x=read(),y=read(),val=read();
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=val;
}dfs(1,0);
printf("%lld\n",ans);
return 0;
}