#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.

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.