logo AlgoBeat OnlineJudge
登录 注册

#209284. [EC Final 2022] Coloring

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given elements numbered from to . Element has value and color . Each element also has a pointer to some other element.

Initially, the color of element is , while the color of all the other elements is . More formally, and for all .

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

  • Assign at a cost of .

Your score is equal to the sum of values of all the elements with color 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 () the number of elements and the element with color initially.

The second line contains integers () the value of the elements.

The third line contains integers () the cost of changing the color of each element.

The fourth line contains integers (, ).

输出格式

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 at a cost of , then ;
  • Assign at a cost of , then ;
  • Assign at a cost of , then ;
  • Assign at a cost of , then .

After the operations, only the color of element is , so your score is equal to . It can be shown that it is impossible to obtain a score greater than .