赛后在麦当劳由科大一牛提点才会的~
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int NN=250000;
struct Edge{
int u,v;
LL w;
bool operator <(const Edge a)const{
return w>a.w;
}
} e[NN];
int belong[NN];
struct Set{
int num;
LL value;
} a[NN];
int find(int x)
{
if (belong[x]!=x) belong[x]=find(belong[x]);
return belong[x];
}
void set_union(int u,int v,LL w)
{
if (a[u].num<a[v].num)
{
belong[u]=v;
a[v].num+=a[u].num;
a[v].value=w;
}
else
{
belong[v]=u;
a[u].num+=a[v].num;
a[u].value=w;
}
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
for (int i=1; i<n; i++) scanf("%d%d%lld",&e[i].u,&e[i].v,&e[i].w);
sort(e+1,e+n);
for (int i=1; i<=n; i++)
{
belong[i]=i;
a[i].num=1;
a[i].value=0;
}
for (int i=1; i<n; i++)
{
int u=find(e[i].u);
int v=find(e[i].v);
LL tmp1=a[u].value+a[v].num*e[i].w;
LL tmp2=a[v].value+a[u].num*e[i].w;
set_union(u,v,max(tmp1,tmp2));
}
printf("%lld\n",a[find(1)].value);
}
return 0;
}