포럼
문제 ICPC00319

A. Appeal to the Audience

설명

You are the director of the upcoming Bordfite Arena Progaming Competition. You have invited a bunch of players and are now setting up the matches for the knockout tournament that will determine the winner. As you may know, Bordfite Arena is a game that heavily rewards skill and very little luck is involved. This implies that whenever any number of players play a game of Bordfite Arena, the most skilled player will always win! Hence the winner of the tournament is already known, and you are a bit worried about this. How will you appease the audience?

You embark on a short quest to find out what the audience finds interesting. No surprises there: people find it most interesting when they see skillful players compete. Whenever a match is played, the happiness the audience gets from a match is the sum of the skill levels of the players. The total happiness the audience gets from the tournament is the sum of the happiness obtained during all matches. This is very useful information, because of course you want the audience to be as happy as possible at the end of the tournament.

Moreover, you invested some time to ask people what kind of knockout format they like. It turns out that instead of the plain old binary tree for the knockout schedule, they prefer a specific weird-looking rooted tree, and so you decide to use that. This means the final step for you to complete is to divide the players over the leaves of the given tree so that over the entire tournament, the happiness of the audience is maximized.

제약
입력 형식
  • The first line contains integers \(3\leq n\leq 10^5\) and \(1 \leq k \leq n-1\), the number of nodes of the tree and the number of players. The nodes are labelled \(0\) through \(n-1\), and \(0\) is the root of the tree.
  • The second line contains \(k\) integers \(0\leq a_1, \dots, a_k\leq 10^9\), denoting the skill values of the players.
  • Then follow \(n-1\) lines, the \(i\)th of which (\(1\leq i \leq n-1\)) contains the parent \(0 \leq p_i < i\) of node \(i\).

It is guaranteed that the tree has exactly \(k\) leaves and that there are no nodes with exactly one child.

출력 형식
  • Output the maximal possible happiness the audience can obtain from this tournament.
예제 1
입력
5 3
5 4 3
0
0
1
1
출력
17
예제 2
입력
11 7
30 5 15 1 3 100 50
0
0
1
0
2
5
2
5
5
1
출력
454
문제 정보

생성자가 기록되지 않았습니다.

출처 ICPC BAPC 2019

평가 및 의견

A. Appeal to the Audience

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 1 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

A. Appeal to the Audience

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8