#P9716. [EC Final 2022] Coloring

[EC Final 2022] Coloring

题目描述

You are given nn elements numbered from 11 to nn. Element ii has value wiw_i and color cic_i. Each element also has a pointer aia_i to some other element.

Initially, the color of element ss is 11, while the color of all the other elements is 00. More formally, cs=1c_s=1 and ci=0c_i=0 for all isi\neq s (1in)(1 \le i \le n).

You can perform the following operation for any number of times:

  • Assign cicaic_i\leftarrow c_{a_i} at a cost of pip_i.

Your score is equal to the sum of values of all the elements with color 11 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,sn,s (1sn5×1031 \leq s\le n \leq 5\times 10^3) - the number of elements and the element with color 11 initially.

The second line contains nn integers w1,w2,,wnw_1,w_2,\dots,w_n (109wi109-10^9\le w_i\le 10^9) - the value of the elements.

The third line contains nn integers p1,p2,,pnp_1,p_2,\dots,p_n (0pi1090\le p_i\le 10^9) - the cost of changing the color of each element.

The fourth line contains nn integers a1,a2,,ana_1,a_2,\dots,a_n (1ain1\le a_i\le n, aiia_i\neq i).

输出格式

Output one integer representing the answer in one line.

题目大意

【题目描述】

给定 nn 个元素,编号从 11nn。元素 ii 的值为 wiw_i,颜色为 cic_i。每个元素还有一个指针 aia_i 指向另一个元素。

最初,元素 ss 的颜色为 11,而所有其他元素的颜色都为 00。更正式地说,对于所有 isi\neq s (1in)(1 \le i \le n),有 cs=1c_s=1ci=0c_i=0

你可以任意多次执行以下操作:

  • 以代价 pip_icicaic_i\leftarrow c_{a_i}

你的得分等于所有颜色为 11 的元素值的总和减去操作的总代价。

找出你能够获得的最大可能得分。

【输入格式】

第一行包含两个整数 nnss1sn5×1031 \leq s\le n \leq 5\times 10^3- 元素数量以及初始颜色为 11 的元素。

第二行包含 nn 个整数 w1,w2,,wnw_1,w_2,\dots,w_n109wi109-10^9\le w_i\le 10^9- 元素的值。

第三行包含 nn 个整数 p1,p2,,pnp_1,p_2,\dots,p_n0pi1090\le p_i\le 10^9- 改变每个元素颜色的代价。

第四行包含 nn 个整数 a1,a2,,ana_1,a_2,\dots,a_n1ain1\le a_i\le n, aiia_i\neq i)。

【输出格式】

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

【样例解释】

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

  • 以代价 p2p_2c2ca2c_2\leftarrow c_{a_2},然后 c=[1,1,0]c=[1,1,0]
  • 以代价 p1p_1c1ca1c_1\leftarrow c_{a_1},然后 c=[0,1,0]c=[0,1,0]
  • 以代价 p3p_3c3ca3c_3\leftarrow c_{a_3},然后 c=[0,1,1]c=[0,1,1]
  • 以代价 p2p_2c2ca2c_2\leftarrow c_{a_2},然后 c=[0,0,1]c=[0,0,1]

操作后,只有元素 33 的颜色为 11,因此你的得分等于 w3(p2+p1+p3+p2)=1w_3-(p_2+p_1+p_3+p_2)=1。可以证明无法获得大于 11 的得分。

翻译来自于:ChatGPT

3 1
-1 -1 2
1 0 0
3 1 2
1
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
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 c2ca2c_2\leftarrow c_{a_2} at a cost of p2p_2, then c=[1,1,0]c=[1,1,0];
  • Assign c1ca1c_1\leftarrow c_{a_1} at a cost of p1p_1, then c=[0,1,0]c=[0,1,0];
  • Assign c3ca3c_3\leftarrow c_{a_3} at a cost of p3p_3, then c=[0,1,1]c=[0,1,1];
  • Assign c2ca2c_2\leftarrow c_{a_2} at a cost of p2p_2, then c=[0,0,1]c=[0,0,1].

After the operations, only the color of element 33 is 11, so your score is equal to w3(p2+p1+p3+p2)=1w_3-(p_2+p_1+p_3+p_2)=1. It can be shown that it is impossible to obtain a score greater than 11.