Maximizer

두 순열 A와 B가 주어질 때, 인접한 원소를 교환해 A를 재배열하여 |a_i - b_i|의 합을 최대로 만들고, 그때 필요한 최소 교환 횟수를 구한다.

어려움8그리디정렬조합론수학아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

Maximizer has two permutations A=\[a_1,a_2,,a_N]A=\[a\_1,a\_2,\cdots,a\_N] and B=\[b_1,b_2,,b_N]B=\[b\_1,b\_2,\cdots,b\_N]. Both A,BA, B have length NN and consists of distinct integers from 11 to NN

Maximizer wants to maximize the sum of differences of each element, _i=1Na_ib_i\sum\_{i=1}^{N} |a\_i - b\_i|. But he can only swap two adjacent elements in AA. Precisely, he can only swap a_ia\_i and a_i+1a\_{i+1} for some ii from 11 to N1N-1. He can swap as many times as he wants.

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

입력

The first line contains an integer NN. (1N250,0001 \leq N \leq 250\\,000)

The second line contains NN integers a_1,a_2,,a_Na\_1,a\_2,\cdots,a\_N (1a_iN1 \leq a\_i \leq N).

The third line contains NN integers b_1,b_2,,b_Nb\_1,b\_2,\cdots,b\_N (1b_iN1 \leq b\_i \leq N).

Each of \[a_1,a_2,,a_N]\[a\_1,a\_2,\cdots,a\_N] and \[b_1,b_2,,b_N]\[b\_1,b\_2,\cdots,b\_N] is a permutation. In other words, it is consisted of distinct integers from 11 to NN.

출력

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