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 void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int V = int.Parse(input[0]);
            int E = V - 1;

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

            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));
            }

            PrintHeight(G);
        }

        static void PrintHeight(Dictionary<int, List<Edge>> G)
        {
            int n = G.Count;

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

                foreach (var e in G[i])
                {
                    T[i].Add(new Edge(e.to, -1));
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < T[i].Count; j++)
                {
                    if (T[i][j].distance < 0) T[i][j].distance = Visit(G, T, i, j);
                }
            }

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

        static int Visit(Dictionary<int, List<Edge>> G, Dictionary<int, List<Edge>> T, int i, int j)
        {
            if (T[i][j].distance >= 0) return T[i][j].distance;

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

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

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