#P12406. Zero‐Out Permutation with Cyclic Shifts and Cost Swap

    ID: 14508 Type: Default 1000ms 256MiB

Zero‐Out Permutation with Cyclic Shifts and Cost Swap

Zero‐Out Permutation with Cyclic Shifts and Cost Swap

You are given two permutations of length \(n\): \(a\) and \(b\). You can perform the following operations arbitrarily many times:

  • Cyclic left shift of \(a\): Let \(a=[a_1,a_2,\ldots,a_n]\) before the operation, then after the operation \(a=[a_2,a_3,\ldots,a_n,a_1]\). If \(a_1\neq 0\) before the operation, you pay a cost of \(x\); otherwise the shift is free.
  • Cyclic right shift of \(a\): If \(a=[a_1,a_2,\ldots,a_n]\) then after one right shift it becomes \(a=[a_n,a_1,a_2,\ldots,a_{n-1}]\). If \(a_1\neq 0\) before the operation, you pay a cost of \(y\); otherwise the shift is free.
  • Swap the cost parameters \(x\) and \(y\): This operation costs \(z\) points and permanently swaps the roles of \(x\) and \(y\) until you swap again.
  • Removal operation: If \(a_1=b_1\) then you may perform an operation that cyclically left shifts \(b\) (i.e. \(b=[b_2,b_3,\ldots,b_n,b_1]\)) and simultaneously set \(a_1:=0\). This removal does not cost anything.

Your goal is to make every element of \(a\) equal to 0 (i.e. \(a_i=0\) for all \(1\le i\le n\)). It can be shown that this is always achievable via a sequence of the above operations.

Note: All formulas must be written in \(\LaTeX\) format. For example, the cyclic left shift transforms \(a=[a_1,a_2,\ldots,a_n]\) into \(a=[a_2,\ldots,a_n,a_1]\), and similarly for a right shift.

inputFormat

The first line contains four integers \(n, x, y, z\) \( (1\le n\le 10^5,\ 1\le x,y,z\le 10^9) \) --- the size of the permutations and the cost parameters.

The second line contains \(n\) distinct integers \(a_1,a_2,\ldots,a_n\) which is a permutation of \(\{1,2,\ldots,n\}\>.

The third line contains \(n\) distinct integers \(b_1,b_2,\ldots,b_n\) which is a permutation of \(\{1,2,\ldots,n\}\>.

outputFormat

Output a single integer --- the minimum total cost to make all elements of \(a\) equal to 0.

sample

3 2 3 5
3 1 2
1 2 3
2