Problem:
Height of a Tree

Given a tree T with non-negative weight, find the height of each node of the tree. For each node, the height is the distance to the most distant leaf from the node.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

The output consists of n lines. Print the height of each node 0, 1, 2, ..., n-1 in order.

Constraints

1 ≤ n ≤ 10,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5
3
4
5

Solution in C# (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        class Edge
        {
            internal int to, distance;

            public Edge(int to, int distance)
            {
                this.to = to;
                this.distance = distance;
            }
        }

        static Dictionary<int, List<Edge>> G;
        static Dictionary<int, List<int>> T;

        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int V = int.Parse(input[0]);
            int E = V - 1;

            G = new Dictionary<int, List<Edge>>();
            T = new Dictionary<int, List<int>>();
            for (int i = 0; i < V; i++)
            {
                G.Add(i, new List<Edge>());
                T.Add(i, new List<int>());
            }

            for (int i = 0; i < E; i++)
            {
                input = Console.ReadLine().Split(' ');
                int s = int.Parse(input[0]);
                int t = int.Parse(input[1]);
                int w = int.Parse(input[2]);

                G[s].Add(new Edge(t, w));
                G[t].Add(new Edge(s, w));
                T[s].Add(-1);
                T[t].Add(-1);
            }

            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < T[i].Count; j++)
                {
                    if (T[i][j] < 0) T[i][j] = Height(i, j);
                }
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < V; i++)
            {
                int max = 0;
                for (int j = 0; j < T[i].Count; j++)
                {
                    max = Math.Max(max, T[i][j]);
                }
                sb.AppendLine(max.ToString());
            }
            Console.Write(sb);
        }

        static int Height(int i, int j)
        {
            if (T[i][j] >= 0) return T[i][j];

            T[i][j] = G[i][j].distance;

            int next = G[i][j].to;
            for (int k = 0; k < T[next].Count; k++)
            {
                if (G[next][k].to == i) continue;

                T[i][j] = Math.Max(T[i][j], Height(next, k) + G[i][j].distance);
            }
            return T[i][j];
        }
    }
}