CF1148B Born This Way
Description
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C.
There are $ n $ flights from A to B, they depart at time moments $ a_1 $ , $ a_2 $ , $ a_3 $ , ..., $ a_n $ and arrive at B $ t_a $ moments later.
There are $ m $ flights from B to C, they depart at time moments $ b_1 $ , $ b_2 $ , $ b_3 $ , ..., $ b_m $ and arrive at C $ t_b $ moments later.
The connection time is negligible, so one can use the $ i $ -th flight from A to B and the $ j $ -th flight from B to C if and only if $ b_j \ge a_i + t_a $ .
You can cancel at most $ k $ flights. If you cancel a flight, Arkady can not use it.
Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $ k $ flights. If you can cancel $ k $ or less flights in such a way that it is not possible to reach C at all, print $ -1 $ .
Input Format
N/A
Output Format
N/A
Explanation/Hint
Consider the first example. The flights from A to B depart at time moments $ 1 $ , $ 3 $ , $ 5 $ , and $ 7 $ and arrive at B at time moments $ 2 $ , $ 4 $ , $ 6 $ , $ 8 $ , respectively. The flights from B to C depart at time moments $ 1 $ , $ 2 $ , $ 3 $ , $ 9 $ , and $ 10 $ and arrive at C at time moments $ 2 $ , $ 3 $ , $ 4 $ , $ 10 $ , $ 11 $ , respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $ 4 $ , and take the last flight from B to C arriving at C at time moment $ 11 $ .
In the second example you can simply cancel all flights from A to B and you're done.
In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.