P14619 [2019 KAIST RUN Fall] Maximizer

Description

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 $\textbf{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 Format

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 Format

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