406369: GYM102391 H Maximizer

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

H. Maximizertime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

Maximizer has two permutations $$$A=[a_1,a_2,\cdots,a_N]$$$ and $$$B=[b_1,b_2,\cdots,b_N]$$$. Both $$$A, B$$$ have length $$$N$$$ and consists of distinct integers from $$$1$$$ to $$$N$$$.

Maximizer wants to maximize the sum of differences of each element, $$$\sum_{i=1}^{N} |a_i - b_i|$$$. But he can only swap two adjacent elements in $$$A$$$. Precisely, he can only swap $$$a_i$$$ and $$$a_{i+1}$$$ for some $$$i$$$ from $$$1$$$ to $$$N-1$$$. He can swap as many times as he wants.

What is the minimum number of swaps required for maximizing the difference sum?

Input

The first line contains an integer $$$N$$$. ($$$1 \leq N \leq 250 000$$$)

The second line contains $$$N$$$ integers $$$a_1,a_2,\cdots,a_N$$$ ($$$1 \leq a_i \leq N$$$).

The third line contains $$$N$$$ integers $$$b_1,b_2,\cdots,b_N$$$ ($$$1 \leq b_i \leq N$$$).

Each of $$$[a_1,a_2,\cdots,a_N]$$$ and $$$[b_1,b_2,\cdots,b_N]$$$ is a permutation. In other words, it is consisted of distinct integers from $$$1$$$ to $$$N$$$.

Output

Print an integer, the minimum number of swaps required for maximizing the difference sum.

ExamplesInput
3
1 2 3
1 2 3
Output
2
Input
4
3 4 1 2
3 2 4 1
Output
3

加入题单

算法标签: