1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5 const int maxn=2e4+10;
6 const int maxm=4e4+10;
7 long long ans;
8 int n;
9 int a,b,c;
10 int h[maxn],hs;
11 struct edge{int s,t,w;}e[maxm];
12 int f[maxn],sz[maxn];
13 int find_f(int k){return f[k]==k?k:f[k]=find_f(f[k]);}
14 bool comp(const edge &x,const edge &y){return x.w<y.w;}
15 int main(){
16 scanf("%d",&n);
17 for(int i=1;i<n;i++){
18 scanf("%d%d%d",&a,&b,&c);
19 e[++hs]=(edge){a,b,c};
20 ans+=c;
21 }
22 sort(e+1,e+hs+1,comp);
23 for(int i=1;i<=n;i++) f[i]=i,sz[i]=1;
24 for(int i=1;i<=hs;i++){
25 a=find_f(e[i].s),b=find_f(e[i].t);
26 f[b]=a;
27 ans+=1ll*(1ll*sz[a]*sz[b]-1)*(e[i].w+1);
28 sz[a]+=sz[b];
29 }
30 cout<<ans<<endl;
31 return 0;
32 }