Universal Cup Judging System

Universal Cup

Limite de temps : 2 s Limite de mémoire : 1024 MB Points totaux : 100 Difficulté: [afficher]
Statistiques

给定 $n$ 个编号为 $1$ 到 $n$ 的元素。元素 $i$ 具有权值 $w_i$ 和颜色 $c_i$。每个元素还有一个指向其他某个元素的指针 $a_i$。

初始时,元素 $s$ 的颜色为 $1$,而所有其他元素的颜色均为 $0$。更正式地说,$c_s = 1$,且对于所有 $i \neq s$ ($1 \le i \le n$),$c_i = 0$。

你可以进行任意次数的以下操作:

  • 以 $p_i$ 的代价执行赋值 $c_i \leftarrow c_{a_i}$。

你的得分等于操作后所有颜色为 $1$ 的元素的权值之和减去操作的总代价。

求你能获得的最大得分。

输入格式

第一行包含两个整数 $n, s$ ($1 \le s \le n \le 5 \times 10^3$),分别表示元素个数和初始颜色为 $1$ 的元素。

第二行包含 $n$ 个整数 $w_1, w_2, \dots, w_n$ ($-10^9 \le w_i \le 10^9$),表示元素的权值。

第三行包含 $n$ 个整数 $p_1, p_2, \dots, p_n$ ($0 \le p_i \le 10^9$),表示改变每个元素颜色的代价。

第四行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n, a_i \neq i$)。

输出格式

输出一行,包含一个整数,表示答案。

样例

输入格式 1

3 1
-1 -1 2
1 0 0
3 1 2

输出格式 1

1

说明

在第一个样例中,你可以依次执行以下操作:

  1. 以 $p_2$ 的代价执行 $c_2 \leftarrow c_{a_2}$,此时 $c = [1, 1, 0]$;
  2. 以 $p_1$ 的代价执行 $c_1 \leftarrow c_{a_1}$,此时 $c = [0, 1, 0]$;
  3. 以 $p_3$ 的代价执行 $c_3 \leftarrow c_{a_3}$,此时 $c = [0, 1, 1]$;
  4. 以 $p_2$ 的代价执行 $c_2 \leftarrow c_{a_2}$,此时 $c = [0, 0, 1]$。

操作完成后,只有元素 $3$ 的颜色为 $1$,因此你的得分等于 $w_3 - (p_2 + p_1 + p_3 + p_2) = 1$。可以证明,不可能获得大于 $1$ 的得分。

输入格式 2

10 8
36175808 53666444 14885614 -14507677
-92588511 52375931 -87106420 -7180697
-158326918 98234152
17550389 45695943 55459378 18577244
93218347 64719200 84319188 34410268
20911746 49221094
8 1 2 2 8 8 4 7 8 4

输出格式 2

35343360

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.