COCI '13 Contest 6 #3 Kockice

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 32M

Problem type

Mirko and Slavko are playing with bricks. Both of them have their own pile of bricks. The piles consist of N columns (where N is an odd number). The number of bricks in the i^\text{th} column of Mirko's pile is labeled with m_i and Slavko's pile with s_i.

They have decided to create two equal piles constructed in a way that the heights of columns are strictly descending at first and then strictly ascending (see right image below) and the heights of adjacent columns differ exactly by 1 (see image). The lowest of the columns must have an equal number of columns to the left and to the right of it.

The piles can be modified by removing one brick from the top of some column and throw it out the window (they cannot reuse it) or by taking one brick from the box and place it on the top of some column (there is an infinite amount of bricks in the box). Removing or placing a brick counts as one move.

You have to determine the minimal number of moves so that Mirko and Slavko can rearrange their piles in the described way.

On the left, there is a pile with column heights 3, 2, 0, 1 and 4.
On the right, there is one of the possible final layouts.

Input Specification

The first line of input contains an odd number N. (1 \le N \le 300\,000), the number of columns in both piles.

The second line of input contains N integers m_i (0 \le m_i \le 2^{30}), column heights in Mirko's pile.

The third line of input contains N integers s_i (0 \le s_i \le 2^{30}), column heights in Slavko's pile.

Output Specification

The first and only line of output must contain the minimal number of moves.

Scoring

In test cases worth 40\% of total points, the following will hold: 1 \le N \le 1\,000 and 0 \le m_i, s_i \le 1\,000.

Sample Input 1

3
1 2 3
3 2 2

Sample Output 1

3

Explanation for Sample Output 1

Mirko places two bricks on the top of the first column in his pile and Slavko places one brick on the top of the third column in his pile.

Sample Input 2

5
2 3 0 1 4
3 3 2 3 1

Sample Output 2

10

Comments

There are no comments at the moment.