## DMOPC '17 Contest 2 P6 - Lazy Days

View as PDF

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 64M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Bob is kicking back and relaxing today! He plans to continually watch TV for hours. His TV has two channels each of which has a new show every hour. Every show has a rating from to . Arrays and will represent the ratings of the shows of the first and second channel respectively. Bob wants to watch the shows so that he maximizes the minimum of the ratings of the shows he watches. Additionally, he is lazy, so he doesn't like reaching for his remote. Help Bob find out the most he can get out of his relaxation if he switches channels at most times for . Bob begins on the first channel, and can switch the channel right before any show.

#### Input Specification

The first line will contain one integer representing .

The second line will contain space-separated integers representing array .

The third line will contain space-separated integers representing array .

#### Output Specification

Output lines. On the line, output the maximum possible minimum Bob can achieve if he switches channels at most times.

Maximizing the minimum - consider the minimum of ratings of the TV shows Bob watches. This minimum changes depending on at what times Bob switches the channel. To maximize the minimum is to find the largest such value over all possible ways Bob can switch channels in switches or less.

#### Sample Input 1

6
10 21 15 13 17 15
18 20 18 22 14 19

#### Sample Output 1

10
14
15
17
17
17
17

#### Explanation for Sample 1

For zero switches, Bob can only sit back and watch the entire first channel. With one switch, Bob can change the channel right before the first show and watch the entire second channel. For two switches, Bob changes the channel before the first and fifth shows. For three switches, Bob also changes the channel before the sixth show. Even with more switches, Bob cannot get any better value than .

#### Sample Input 2

5
1 2 1 2 1
2 1 2 1 2

#### Sample Output 2

1
1
1
1
1
2