P18122 [ICPC 2026 Seoul R] Circuit X

Description

You are leading a large circuit-design project. The project is nearly complete, and your final task is to finish its most important module, called Circuit X. Circuit X consists of three layers: an upper layer, a middle layer, and a lower layer. Each layer of Circuit X consists of $n$ pins placed horizontally, from left to right. The pins in each of the upper and lower layers are labeled with a permutation of the integers from $1$ to $n$. The pins in the middle layer are unlabeled. To complete Circuit X, for each $1\le i\le n$, connect the upper-layer pin labeled $i$ to a middle-layer pin with a straight wire, and connect that same middle-layer pin to the lower-layer pin labeled $i$ with another straight wire. Thus, exactly $2n$ wires are used. Do not forget that each pin in the middle layer must be connected to one and only one pin in the upper layer and to one and only one in the lower layer. The figure below shows an example of Circuit X in which $n=5$. On the left, the current configuration of Circuit X is shown, where the upper-layer pins are labeled $(1,3,5,2,4)$ from left to right, and the lower-layer pins are labeled $(4,1,2,3,5)$. The right part shows a possible completion of Circuit X. ![](https://cdn.luogu.com.cn/upload/image_hosting/epa0bnym.webp) There are many possible ways to complete Circuit X, depending on how the labeled pins are matched to the middle-layer pins. You need to be very careful because your project will be evaluated by the complexity of the final form of Circuit X. The complexity of Circuit X is determined by the number of crossings among wires. Specifically, for a fixed completion of Circuit X, let $\chi^{\wedge}$ be the number of pairs of crossing wires between the upper and middle layers, and $\chi^{\vee}$ be the number of pairs of crossing wires between the middle and lower layers. Then the complexity of Circuit X is defined to be $a\chi^{\wedge}+b\chi^{\vee}$, where $a$ and $b$ are two predefined integer parameters. In the above example, we have $\chi^{\wedge}=3$ and $\chi^{\vee}=5$, so the complexity of Circuit X will be $3a+5b$ depending on $a$ and $b$. Note that the complexity is not necessarily positive, since $a$ and $b$ may be zero or negative. To obtain the best evaluation for your project, you need to minimize the complexity of Circuit X. Given the orders of the labels of pins in the upper and lower layers of Circuit X and two integer parameters $a$ and $b$, write a program to output the minimum possible complexity of Circuit X.

Input Format

Your program is to read from standard input. The input consists of three lines. The first line consists of three integers, $n$, $a$, and $b$ ($2\le n\le 1{,}000{,}000$; $-10{,}000\le a,b\le 10{,}000$), where $n$ is the number of pins in each layer of Circuit X, and $a$, $b$ are integer parameters for the complexity of Circuit X. The second line consists of a permutation of integers $\{1,2,\ldots,n\}$, which represents the labels of the $n$ pins in the upper layer of Circuit X from left to right. The third line consists of another permutation of integers $\{1,2,\ldots,n\}$, which represents the labels of the $n$ pins in the lower layer of Circuit X from left to right.

Output Format

Your program is to write to standard output. Print exactly one line consisting of a single integer that represents the minimum possible complexity of the input instance of Circuit X.