#P12406. 「CZOI-R3」消除序列

    ID: 13467 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树树状数组洛谷原创O2优化洛谷比赛

「CZOI-R3」消除序列

Problem Description

There are two permutations a,ba, b of length nn. You can perform the following operations any number of times:

  • Cyclically shift aa left by one position. If a10a_1 \neq 0 before the operation, it costs xx.
  • Cyclically shift aa right by one position. If a10a_1 \neq 0 before the operation, it costs yy.
  • Swap xx and yy. It costs zz.
  • If a1=b1a_1 = b_1, cyclically shift bb left by one position, and set a1=0a_1 = 0. This costs nothing.

Find the minimum total cost to make ai=0a_i = 0 for all 1in1 \le i \le n. It is guaranteed that the goal can always be achieved by some sequence of operations.

\dag : Suppose before a cyclic left shift the sequence is a1,a2,,an1,ana_1, a_2, \cdots, a_{n-1}, a_n, then after the operation it becomes a2,,an1,an,a1a_2, \cdots, a_{n-1}, a_n, a_1. Suppose before a cyclic right shift the sequence is a1,a2,,an1,ana_1, a_2, \cdots, a_{n-1}, a_n, then after the operation it becomes an,a1,a2,,an1a_n, a_1, a_2, \cdots, a_{n-1}.

Input Format

The first line contains four integers n,x,y,zn, x, y, z.

The second line contains nn integers, representing the permutation aa.

The third line contains nn integers, representing the permutation bb.

Output Format

Output one integer in one line, representing the answer.

2 1 1 1
1 2
2 1
1
5 4 3 2
1 4 3 2 5
5 1 4 2 3
3

Hint

Constraints

This problem uses bundled testdata.

  • Subtask #1 (10 pts10\text{ pts}): n10n \le 10.
  • Subtask #2 (25 pts25\text{ pts}): x=y=zx = y = z.
  • Subtask #3 (25 pts25\text{ pts}): n103n \le 10^3. Depends on Subtask #1.
  • Subtask #4 (40 pts40\text{ pts}): No special properties. Depends on Subtask #2 #3.

For 100%100\% of the testdata: 1n1061 \le n \le 10^6, and a,ba, b are permutations of length nn. 1x,y,z1061 \le x, y, z \le 10^6.

Translated by ChatGPT 5