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

21882번 - Coins on a tree 서브태스크다국어

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

문제

You have a rooted tree, some vertices have coins lying on them.

You can do the following operation any number of times: take some two vertices $u$ and $v,ドル such that there are no coins at lowest common ancestor of $u$ and $v$ but both vertices $u$ and $v$ have non-zero amount of coins $a_u$ and $a_v$ and put $a_u + a_v$ coins at their lowest common ancestor.

Your goal is to maximize the sum of the number of coins on all vertices.

입력

The first line contains single integer $n$ (2ドル \leq n \leq 10^5$) --- number of vertices of the tree.

The root of the tree is vertex 1ドル$.

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

The third line contains $n$ integers $a_1, a_2, \ldots, a_n,ドル $a_i$ is the number of coins that lie on vertex $i$ (0ドル \leq a_i \leq 10^5$).

출력

Print one integer --- the maximum total amount of coins that you can get by applying the described operations.

제한

서브태스크

번호배점제한
15

$n \leq 5, a_i \leq 1$

25

$n \leq 20$

310

$n \leq 2000, a_i \leq 1$

420

$n \leq 2000$

520

$a_i \leq 1$

640

No additional constraints.

예제 입력 1

3
1 1
0 1 1

예제 출력 1

4

예제 입력 2

5
1 1 3 4
5 3 4 2 4

예제 출력 2

18

힌트

출처

Contest > Innopolis Open in Informatics > Innopolis Open, Second elimination round, 2018-2019 C번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

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

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