#BS0000269. Xếp tạ theo cặp (Free Weights)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xếp tạ theo cặp (Free Weights)
Free Weights
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Two racks initially contain 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 . Each of the next two lines contains 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): ; ; (; ; ).
- Subtask 2 (30 points): ; ; ; (; ); .
- Subtask 3 (50 points): ; ; (; ; ).
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.