This page is still under construction.

Parts of this page are still being built. What you see may change.

Stacking Blocks

Interview

Time limit1sMemory limit128 MB

Summary
Reshape both towers into the V-shaped skyline with center height h at the lowest total cost of added and removed blocks.
Level

Medium5 of 10

Topics
Sorting, Prefix sum, Math
Solved
No attempts yet

Problem

Yunhyeong and Donghyeok are playing with blocks. Each of them built one block building of width NN. Column kk of Yunhyeong's building holds YkY_k blocks, and column kk of Donghyeok's building holds DkD_k blocks. The two of them now want to stack and remove blocks until both buildings look exactly the same.

The new building has to take the pacman shape drawn on the right of the picture above. Going from left to right the number of blocks decreases and then increases, two neighboring columns differ by exactly one block, and the column with the fewest blocks sits at the exact center. Since NN is odd, let c=N+12c = \frac{N+1}{2} be the number of the center column. Column kk of a finished building then holds h+∣k−c∣h + |k - c| blocks for some integer h≥0h \ge 0.

To keep the room tidy, a block taken off a building goes straight into the block box. Moving a block to another spot means putting it into the box and then taking it back out to stack it. The box holds infinitely many blocks.

Stacking one block counts as one operation, and removing one block counts as one operation. Write a program that changes both buildings into the shape above with the smallest total number of operations.

Input

The first line contains the width NN of the two buildings. NN is odd.

The second line contains the column heights of Yunhyeong's building, Y1,Y2,…,YNY_1, Y_2, \dots, Y_N, separated by spaces.

The third line contains the column heights of Donghyeok's building, D1,D2,…,DND_1, D_2, \dots, D_N, separated by spaces.

Output

Print the smallest total number of operations that stack or remove a block.

Constraints

  • 1≤N≤300,0001 \le N \le 300{,}000
  • 0≤Yk,Dk≤10120 \le Y_k, D_k \le 10^{12}

Hint

In the first example, stack 2 more blocks on column 1 of Yunhyeong's building and 1 more block on column 3 of Donghyeok's building.

Examples2

  1. Example 1

    Input
    3
    1 2 3
    3 2 2
    
    Expected output
    3
    
  2. Example 2

    Input
    5
    2 3 0 1 4
    3 3 2 3 1
    
    Expected output
    10