[EC Final 2022] Coloring

题意翻译

**【题目描述】** 给定 $n$ 个元素,编号从 $1$ 到 $n$。元素 $i$ 的值为 $w_i$,颜色为 $c_i$。每个元素还有一个指针 $a_i$ 指向另一个元素。 最初,元素 $s$ 的颜色为 $1$,而所有其他元素的颜色都为 $0$。更正式地说,对于所有 $i\neq s$ $(1 \le i \le n)$,有 $c_s=1$ 和 $c_i=0$。 你可以任意多次执行以下操作: - 以代价 $p_i$ 将 $c_i\leftarrow c_{a_i}$。 你的得分等于所有颜色为 $1$ 的元素值的总和减去操作的总代价。 找出你能够获得的最大可能得分。 **【输入格式】** 第一行包含两个整数 $n$ 和 $s$($1 \leq s\le n \leq 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$)。 **【输出格式】** 输出一行一个整数,表示答案。 **【样例解释】** 在第一个样例中,你可以依次执行以下操作: - 以代价 $p_2$ 将 $c_2\leftarrow c_{a_2}$,然后 $c=[1,1,0]$; - 以代价 $p_1$ 将 $c_1\leftarrow c_{a_1}$,然后 $c=[0,1,0]$; - 以代价 $p_3$ 将 $c_3\leftarrow c_{a_3}$,然后 $c=[0,1,1]$; - 以代价 $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$ 的得分。 翻译来自于:[ChatGPT](https://chatgpt.com/)

题目描述

You are given $n$ elements numbered from $1$ to $n$. Element $i$ has value $w_i$ and color $c_i$. Each element also has a pointer $a_i$ to some other element. Initially, the color of element $s$ is $1$, while the color of all the other elements is $0$. More formally, $c_s=1$ and $c_i=0$ for all $i\neq s$ $(1 \le i \le n)$. You can perform the following operation for any number of times: - Assign $c_i\leftarrow c_{a_i}$ at a cost of $p_i$. Your score is equal to the sum of values of all the elements with color $1$ after the operations minus the sum of costs of the operations. Find the maximum possible score you can obtain.

输入输出格式

输入格式


The first line contains two integers $n,s$ ($1 \leq s\le n \leq 5\times 10^3$) $-$ the number of elements and the element with color $1$ initially. The second line contains $n$ integers $w_1,w_2,\dots,w_n$ ($-10^9\le w_i\le 10^9$) $-$ the value of the elements. The third line contains $n$ integers $p_1,p_2,\dots,p_n$ ($0\le p_i\le 10^9$) $-$ the cost of changing the color of each element. The fourth line contains $n$ integers $a_1,a_2,\dots,a_n$ ($1\le a_i\le n$, $a_i\neq i$).

输出格式


Output one integer representing the answer in one line.

输入输出样例

输入样例 #1

3 1
-1 -1 2
1 0 0
3 1 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

说明

(There won’t be extra line breakers in the actual test cases.) In the first sample, you can successively perform the following operations: - Assign $c_2\leftarrow c_{a_2}$ at a cost of $p_2$, then $c=[1,1,0]$; - Assign $c_1\leftarrow c_{a_1}$ at a cost of $p_1$, then $c=[0,1,0]$; - Assign $c_3\leftarrow c_{a_3}$ at a cost of $p_3$, then $c=[0,1,1]$; - Assign $c_2\leftarrow c_{a_2}$ at a cost of $p_2$, then $c=[0,0,1]$. After the operations, only the color of element $3$ is $1$, so your score is equal to $w_3-(p_2+p_1+p_3+p_2)=1$. It can be shown that it is impossible to obtain a score greater than $1$.