Problem:
Score : 1100 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th edge in this tree connects Vertices A_i and B_i and has a length of C_i.

Joisino created a complete graph with N vertices.
The length of the edge connecting Vertices u and v in this graph, is equal to the shortest distance between Vertices u and v in the tree above.

Joisino would like to know the length of the longest Hamiltonian path (see Notes) in this complete graph.
Find the length of that path.

Notes

A Hamiltonian path in a graph is a path in the graph that visits each vertex exactly once.

Constraints

2 \leq N \leq 10^5

1 \leq A_i < B_i \leq N

The given graph is a tree.

1 \leq C_i \leq 10^8

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
:
A_{N-1} B_{N-1} C_{N-1}

Output

Print the length of the longest Hamiltonian path in the complete graph created by Joisino.

Sample Input 1

5
1 2 5
3 4 7
2 3 3
2 5 2

Sample Output 1

38

The length of the Hamiltonian path 5 → 3 → 1 → 4 → 2 is 5+8+15+10=38. Since there is no Hamiltonian path with length 39 or greater in the graph, the answer is 38.

Sample Input 2

8
2 8 8
1 5 1
4 8 2
2 5 4
3 8 6
6 8 9
2 7 12

Sample Output 2

132

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

/*#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#include<bits/stdc++.h>
#define ll long long
#define inf 1000000005
#define put putchar('\n')
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,t) for (int i=head[t];i;i=Next[i])
#define sqr(x) ((x)*(x))
#define re register
#define mp make_pair
#define fi first
#define se second
#define pa pair<int,int>
#define pb push_back
#define be begin()
#define en end()
#define ret return puts("-1"),0;
#define mod 1000000007
#define N 500055
#define int ll
using namespace std;
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){char c=getchar();int tot=1;while ((c<'0'|| c>'9')&&c!='-') c=getchar();if (c=='-'){tot=-1;c=getchar();}
int sum=0;while (c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}return sum*tot;}
inline void wr(int x){if (x<0) {putchar('-');wr(-x);return;}if(x>=10)wr(x/10);putchar(x%10+'0');}
inline void wrn(int x){wr(x);put;}inline void wri(int x){wr(x);putchar(' ');}
inline void wrn(int x,int y){wri(x);wrn(y);}inline void wrn(int a,int b,int c){wri(a);wrn(b,c);}
int n,m,sum[N],fa[N],mx1,tot,z[N],a,b,c,mi,ans,lon[N];
int nedge,Next[N*2],head[N],to[N*2];
#define V to[i]
void add(int a,int b,int c){
	Next[++nedge]=head[a];head[a]=nedge;to[nedge]=b;lon[nedge]=c;
}
void add_ne(int a,int b){add(a,b,c);add(b,a,c);}
void dfs(int x){
	sum[x]=1;
	int mx=0;
	go(i,x){
		if (fa[x]==V) continue;
		fa[V]=x;dfs(V);sum[x]+=sum[V];mx=max(mx,sum[V]);
	}
	mx=max(mx,n-sum[x]);
	if (mx<mx1) mx1=mx,tot=0;
	if (mx==mx1) z[++tot]=x;
}
void dfs2(int x){
	sum[x]=1;
	go(i,x){
		if (fa[x]==V) continue;
		fa[V]=x;dfs2(V);sum[x]+=sum[V];ans+=sum[V]*lon[i]*2;
	}
}
signed main(){
	n=read();
	F(i,1,n-1){
		a=read();b=read();c=read();
		add(a,b,c);add(b,a,c);
	}
	mx1=inf;dfs(1);
	fa[z[1]]=0;dfs2(z[1]);
	if (tot==1){
		int mi=inf;
		go(i,z[1]) mi=min(mi,lon[i]);
		ans-=mi;
	}
	else{
		go(i,z[1]) if (V==z[2]) ans-=lon[i];
	}
	wrn(ans);
	return 0;
}