QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 2048 MB Puntuación total: 100

#6253. 呼吁观众

Estadísticas

你是即将到来的 Bordfite Arena 职业游戏竞赛的负责人。你邀请了一群选手,现在正在为决定胜者的淘汰赛安排比赛。

众所周知,Bordfite Arena 是一款非常看重技术、几乎不涉及运气的游戏。这意味着无论有多少名选手进行一场 Bordfite Arena 比赛,技术最好的选手总是会获胜!因此,锦标赛的获胜者早已注定,你对此感到有些担忧。你该如何取悦观众呢?

你开始了一项简短的调查,以找出观众感兴趣的内容。结果并不令人意外:当观众看到技术高超的选手进行比赛时,他们会感到最兴奋。每进行一场比赛,观众从比赛中获得的快乐程度就是参赛选手技术水平之和。锦标赛中观众获得的总快乐程度是所有比赛中获得的快乐程度之和。这是一个非常有用的信息,因为你当然希望锦标赛结束时观众尽可能地感到快乐。

此外,你花了一些时间询问人们他们喜欢什么样的淘汰赛赛制。结果发现,他们并不喜欢传统的二叉树淘汰赛赛程,而是更喜欢一种特定的、看起来很奇怪的有根树,因此你决定采用这种赛制。这意味着你需要完成的最后一步是将选手分配到给定树的叶子节点上,使得在整个锦标赛过程中,观众获得的快乐程度最大化。

输入格式

  • 第一行包含整数 $3 \le n \le 10^5$ 和 $1 \le k \le n - 1$,分别表示树的节点数和选手人数。节点编号为 $0$ 到 $n - 1$,$0$ 是树的根节点。
  • 第二行包含 $k$ 个整数 $0 \le a_1, \dots, a_k \le 10^9$,表示选手的技术水平。
  • 接下来有 $n - 1$ 行,其中第 $i$ 行($1 \le i \le n - 1$)包含节点 $i$ 的父节点 $0 \le p_i < i$。

保证该树恰好有 $k$ 个叶子节点,且不存在只有一个子节点的节点。

输出格式

  • 输出观众从本次锦标赛中可能获得的最大快乐程度。

样例

样例输入 1

5 3
5 4 3
0
0
1
1

样例输出 1

17

样例输入 2

11 7
30 5 15 1 3 100 50
0
0
1
0
2
5
2
5
5
1

样例输出 2

454

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.