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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }

    Scanner cin;
    int N;
    List<Tuple<int, int>>[] es;

    long ans;
    int need = -1;

    void calc()
    {
        cin = new Scanner();
        N = cin.nextInt();
        es = new List<Tuple<int, int>>[N];
        for (int i = 0; i < N; i++)
        {
            es[i] = new List<Tuple<int, int>>();
        }
        for (int i = 0; i < N - 1; i++)
        {
            int A = cin.nextInt() - 1;
            int B = cin.nextInt() - 1;
            int C = cin.nextInt();
            es[A].Add(Tuple.Create(B, C));
            es[B].Add(Tuple.Create(A, C));
        }
        int start = 0;
        for (int i = 0; i < N; i++)
        {
            if (es[i].Count <= 1) start = i;
        }
        dec = 999999999;
        num = new int[N];
        maxDep = -1;
        maxPos = -1;
        ans = 0;
        dfs(start, -1, 0);
        start = maxPos;
        num = new int[N];
        maxDep = -1;
        maxPos = -1;
        ans = 0;
        dfs(start, -1, 0);
        if (need != -1) dec = need;
        Console.WriteLine(ans - dec);
    }

    int[] num;
    int dec;
    int maxDep;
    int maxPos;

    int dfs(int now, int pre,int d)
    {
        if (num[now] != 0) return num[now];
        if(maxDep < d)
        {
            maxDep = d;
            maxPos = now;
        }
        int ret = 1;
        foreach (var item in es[now])
        {
            if (item.Item1 == pre) continue;
            int sum = dfs(item.Item1, now, d + 1);
            ret += sum;
        }
        num[now] = ret;

        foreach (var item in es[now])
        {
            if (item.Item1 == pre) continue;
            int down = num[item.Item1];
            int up = N - down;
            ans += Math.Min(down * 2, up * 2) * (long)item.Item2;
            if(num[now] > N / 2  && down <= N / 2)
            {
                //Console.WriteLine(now + " " +item.Item1 + " " + up + " " + down + " " + item.Item2);
                dec = Math.Min(dec, item.Item2);
            }
            if (Math.Min(down * 2, up * 2) == N) need = item.Item2;
        }
        return ret;
    }



}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();

        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}