Logo
(追記) (追記ここまで)

16844번 - Distance Sum 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 512 MB38241878.261%

문제

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.

제한

예제 입력 1

10
4 1
1 1
3 1
3 1
5 1
6 1
6 1
8 1
4 1

예제 출력 1

0
3
3
4
5
7
10
13
16
19

예제 입력 2

15
1 3
12 5
5 2
12 1
7 5
5 1
6 1
12 1
11 1
12 4
1 1
5 5
10 4
1 2

예제 출력 2

0
3
9
13
14
21
22
29
31
37
41
41
47
56
59

힌트

출처

Contest > ICPC Japanese Alumni Group > JAG Summer Camp > JAG Summer Camp 2014 Day 2 H번

Camp > Petrozavodsk Programming Camp > Winter 2015 > Day 2: Makoto Soejima Contest 1, Japanese Grand Prix H번

Contest > Open Cup > 2014/2015 Season > Stage 6: Grand Prix of Japan H번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /