Under construction

Still being built. Things may change

Balancing Inversions

Time limit2sMemory limit512 MB

Summary
Given a binary array of length 2N, find the fewest adjacent swaps so that the inversion counts of its first and second halves become equal.
Level

Medium7 of 10

Topics
Array, Greedy, Math
Solved
No attempts yet

Problem

Bessie and Elsie were playing a game on a boolean array AA of length 2N2N (1≤N≤1051 \le N \le 10^5). Bessie's score was the number of inversions in the first half of AA, and Elsie's score was the number of inversions in the second half of AA. An inversion is a pair of entries A[i]=1A[i]=1 and A[j]=0A[j]=0 where i<ji<j. For example, an array consisting of a block of 0s followed by a block of 1s has no inversions, and an array consisting of a block of XX 1s followed by a block of YY 0s has XYXY inversions.

Farmer John has found the game board and wants to know the minimum number of swaps between adjacent elements needed so that the game ends in a tie. Help him compute this number.

Input

The first line contains NN. The next line contains 2N2N integers, each either 0 or 1.

Output

Print the minimum number of adjacent swaps needed to make the scores tied.

Hint

In the example, the first half of the array initially has 1 inversion, and the second half has 3 inversions. After swapping the 5th and 6th bits, both halves have 0 inversions.

Examples1

  1. Example 1

    Input
    5
    0 0 0 1 0 1 0 0 0 1
    
    Expected output
    1