Distance Sum

가중치가 있는 트리에서 각 k=1부터 n까지, 정점 v를 적절히 골라 첫 k개 정점까지의 거리 합을 최소로 만드는 값을 구한다.

어려움8트리동적 계획법DFS그리디아직 제출이 없습니다시간 제한4초메모리 제한512 MB

문제

There are n cities and n − 1 roads, and they form a tree. The cities are numbered 1 through n. The city 1 is the root, and for each i the parent of the city i is the city pi, and the distance between i and pi is di. Snuke wants to solve the following problem for each 1 ≤ k ≤ n:

Compute the minimal possible sum of the distances from a certain city to the cities 1, . . . , k:

\[\min_{1 \le v \le n}{\sum_{i=1}^{k}{dist(i,v)}\]

Here dist(u, v) denotes the distance between cities u and v.

입력

First line of the input contains one integer n (1 ≤ n ≤ 2 · 105). Then n − 1 lines follow, i-th of them contains two integers pi+1 and di+1 — parent of a city i + 1 and the distance between i + 1’th city and its parent (1 ≤ pi ≤ n, 1 ≤ di ≤ 2 · 105, the graph represented by pi is a tree).

출력

Print n lines. In the i-th line, print the answer when k = i.