Roasting Emma is a barista too

Given a weighted tree, compute for every vertex the sum of shortest distances to all other vertices.

Medium5TreeDFSDynamic programmingPrefix sumInterviewNo attempts yetTime limit1.5sMemory limit128 MB

Problem

Emma roasts coffee beans and works as a barista. She has linked her farms into a tree with NN vertices, and the farms are numbered 1 through NN. Each edge means you can travel between those two farms, and the weight of an edge is the travel distance.

Emma wants to pick one farm and set up a roasting facility next to it. To place it well she needs to know how far the other farms are from the facility in total. For every farm, find the sum of the shortest distances from all the other farms to that farm.

Input

The first line contains NN. (1N3×1051 \le N \le 3 \times 10^5)

Each of the next N1N-1 lines contains three integers uu, vv, and dd. Farm uu and farm vv are connected to each other and the distance between them is dd. (1u,vN1 \le u, v \le N, 1d51 \le d \le 5)

The given graph is a tree.

Output

Print NN lines. Line ii contains the sum of the shortest distances from all the other farms to farm ii.