#include #define REP(i,n) for(int i=0,i##_len=(n);i>N; typedef pair P; vector> graph(N); REP(i,N-1){ int a,b; long long c; cin>>a>>b>>c; a--;b--; graph[a].push_back(P(c,b)); graph[b].push_back(P(c,a)); } long long ans=0; vector leaf(N,0); auto dfs=[&](auto &&f,int i,int pre)->int{ for(auto e:graph[i]){ if(e.second==pre){ continue; } leaf[i]+=f(f,e.second,i)+1; } for(auto e:graph[i]){ if(e.second==pre) continue; ans+=(leaf[e.second]+1)*(N-leaf[e.second]-1)*e.first; } return leaf[i]; }; dfs(dfs,0,-1); cout<<2*ans<