PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000269. Xếp tạ theo cặp (Free Weights)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Xếp tạ theo cặp (Free Weights)

Free Weights

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Two racks initially contain nn dumbbells each. Across both racks, every mass occurs exactly twice. A dumbbell can roll to a neighboring empty position on its own rack without lifting, or be lifted to any empty position on either rack. Both ends have unlimited room. Arrange each identical pair next to one another on the same rack while minimizing the greatest mass lifted. Final rack sizes may differ.

Input

The first line contains nn. Each of the next two lines contains nn masses in left-to-right order.

Output

Print the minimum possible maximum lifted mass, or 0 if no lifting is required.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; 1≤wj,i≤1001\le w_{j,i}\le100; #{(j,i):wj,i=v}=2\#\{(j,i):w_{j,i}=v\}=2 (v∈{wj,i}v\in\{w_{j,i}\}; j∈{1,2}j\in\{1,2\}; 1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 12≤n≤10612\le n\le10^6; n≡0(mod2)n\equiv0\pmod2; 1≤wj,i≤1091\le w_{j,i}\le10^9; #{i:wj,i=v}=2\#\{i:w_{j,i}=v\}=2 (v∈{wj,i:1≤i≤n}v\in\{w_{j,i}:1\le i\le n\}; j∈{1,2}j\in\{1,2\}); {w1,i}∩{w2,i}=∅\{w_{1,i}\}\cap\{w_{2,i}\}=\varnothing.
  • Subtask 3 (50 points): 1≤n≤1061\le n\le10^6; 1≤wj,i≤1091\le w_{j,i}\le10^9; #{(j,i):wj,i=v}=2\#\{(j,i):w_{j,i}=v\}=2 (v∈{wj,i}v\in\{w_{j,i}\}; j∈{1,2}j\in\{1,2\}; 1≤i≤n1\le i\le n).

Examples

Input 1

5
2 1 8 2 8
9 9 4 1 4

Output 1

2

Explanation

Lifting masses 1 and 2 leaves adjacent heavy pairs 8, 9 and 4. At capacity 1 the first rack retains 2,8,2,8, which cannot be paired by rolling.

Input 2

2
7 7
4 4

Output 2

0

Explanation

Both pairs are already adjacent.