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

21264번 - Monster Hunter 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB766100.000%

문제

There is a rooted tree with $n$ vertices and the root vertex is 1ドル$. In each vertex, there is a monster. The hit points of the monster in the $i$-th vertex is $hp_i$.

Kotori would like to kill all the monsters. The monster in the $i$-th vertex could be killed if the monster in the direct parent of the $i$-th vertex has been killed. The power needed to kill the $i$-th monster is the sum of $hp_i$ and the hit points of all other living monsters who lives in a vertex $j$ whose direct parent is $i$. Formally, the power equals to $$ hp_i + \sum_{\begin{array}{c}\text{the monster in vertex } j \text{ is alive} \\ \text{and } i \text{ is the direct parent of } j \end{array}} hp_j $$

In addition, Kotori can use some magic spells. If she uses one magic spell, she can kill any monster using 0ドル$ power without any restriction. That is, she can choose a monster even if the monster in the direct parent is alive.

For each $m=0,1,2,\cdots,n,ドル Kotori would like to know, respectively, the minimum total power needed to kill all the monsters if she can use $m$ magic spells.

입력

There are multiple test cases. The first line of input contains an integer $T$ indicating the number of test cases. For each test case:

The first line contains an integer $n$ (2ドル \le n \le 2 \times 10^3$), indicating the number of vertices.

The second line contains $(n-1)$ integers $p_2,p_3,\cdots,p_n$ (1ドル \le p_i < i$), where $p_i$ means the direct parent of vertex $i$.

The third line contains $n$ integers $hp_1,hp_2,\cdots,hp_n$ (1ドル \le hp_i \le 10^9$) indicating the hit points of each monster.

It's guaranteed that the sum of $n$ of all test cases will not exceed 2ドル \times 10^3$.

출력

For each test case output one line containing $(n+1)$ integers $a_0, a_1, \cdots, a_n$ separated by a space, where $a_m$ indicates the minimum total power needed to kill all the monsters if Kotori can use $m$ magic spells.

제한

예제 입력 1

3
5
1 2 3 4
1 2 3 4 5
9
1 2 3 4 3 4 6 6
8 4 9 4 4 5 2 4 1
12
1 2 2 4 5 3 4 3 8 10 11
9 1 3 5 10 10 7 3 7 9 4 9

예제 출력 1

29 16 9 4 1 0
74 47 35 25 15 11 7 3 1 0
145 115 93 73 55 42 32 22 14 8 4 1 0

힌트

출처

Contest > Open Cup > 2020/2021 Season > Stage 9: Grand Prix of Nanjing, Division 1 M번

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

출처

대학교 대회

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

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