#TP0000067. Sinh ra đã như vậy (Born This Way)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sinh ra đã như vậy (Born This Way)
Born This Way
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Arkady needs to fly from city to city via city . There are flights from to , departing at strictly increasing times , each taking units of time. There are flights from to , departing at strictly increasing times , each taking units of time. A connection is possible when the second flight departs no earlier than the first flight's arrival at .
Arkady always chooses the remaining itinerary that gets him to as early as possible. You may cancel at most flights across the two legs to make that earliest arrival as late as possible. Output this worst-case earliest arrival time, or -1 if you can prevent him from reaching at all.
Input
The first line contains the five integers .
The second line contains strictly increasing departure times .
The third line contains strictly increasing departure times .
Output
Print one integer: the latest possible earliest arrival time at after optimal cancellations. Print -1 if no itinerary can remain.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; .
Examples
Input
4 5 1 1 2
1 3 5 7
1 2 3 9 10
Output
11
Explanation
Flights from depart at and reach at . Flights from depart at and take one unit of time. Cancel the departure at time on the first leg and the departure at time on the second leg. Arkady takes the flight at , reaches at , then must take the flight at and arrives at . No choice of two cancellations can force a later earliest arrival.