To Play or not to Play
Time limit4sMemory limit512 MB
Given Vasya's and Petya's play intervals and a boost threshold C, find the maximum final experience Vasya can reach when they schedule their play optimally.
- Level
Hard9 of 10
- Topics
- Intervals, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Vasya and Petya are playing an online game. Like most online games, it has a hero progress system where experience makes heroes stronger. Vasya wants to earn as many experience points as possible. If he plays alone, he gets 1 experience point each second. However, if two players play together and their current experience values differ by at most points, they can boost their progress, and each of them gets 2 experience points each second.
Vasya and Petya are middle school students, so their parents do not let them play all day. Vasya can only play during the intervals , and Petya can only play during the intervals . All times are given in seconds from the current moment.
Vasya is good at math. He has noticed that sometimes it can be profitable not to play alone, because the experience difference could become too big, and progress would not be boosted even when they play together.
The two players want a schedule that gives Vasya the greatest possible final experience. Both start with the same experience. Petya does not care much about his own experience, so he is ready to play when needed to maximize Vasya's experience.
Input
The first line contains integers , , and , where is the number of intervals when Vasya can play, is the number of intervals when Petya can play, and is the maximum experience difference that still gives a progress boost when playing together (, ).
The next lines each contain two integers and , an interval when Vasya can play (, ).
The next lines each contain two integers and , an interval when Petya can play (, ).
Output
Print one integer: the maximum experience Vasya can have in the end if both players try to maximize this value.