Farthest node in a weighted tree

For every node of a weighted tree with up to 50000 nodes, print its distance to the farthest node.

Medium6TreeDFSInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

A weighted tree is a tree where every edge carries a number giving that edge's length. All lengths are positive.

For each node of the tree, find the largest distance from that node to any other node. The distance between two nodes is the sum of the edge lengths along the path that joins them.

Input

The first line contains the number of nodes NN. (2N500002 \le N \le 50000)

Each of the next N1N-1 lines describes one edge with three positive integers. The first two are the labels of the nodes the edge joins (from 11 to NN), and the third is the length of the edge. The total length of all edges does not exceed 23112^{31}-1. The input always describes a valid tree.

Output

Print exactly NN lines. The kk-th line contains the distance from node kk to the node farthest from it. (k=1,2,,Nk = 1, 2, \dots, N)