Two Dishes
Time limit5sMemory limit1024 MB
Interleave two chains of timed steps so that each step finishing before its deadline adds its score, and find the maximum total score.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Graph, Shortest path
- Solved
- No attempts yet
Problem
Chef Vitaro is taking part in a cooking contest. Contestants must cook IOI Donburi and JOI Curry.
IOI Donburi has steps. Step () takes exactly minutes. At first, he can only run step 1. To run step (), he must have finished step .
JOI Curry has steps. Step () takes exactly minutes. At first, he can only run step 1. To run step (), he must have finished step .
He must concentrate on each step. Once a step starts, no other step can run until it ends. After a step ends, he may start a step of the other dish. There are no breaks from the start of the contest until both dishes are finished.
In this contest, each contestant receives an art score by these rules.
- If he finishes step of IOI Donburi () within minutes from the start, he gets points. may be negative.
- If he finishes step of JOI Curry () within minutes from the start, he gets points. may be negative.
Vitaro wants to maximize his art score. Given the number of steps, the time each step takes, and the art score information, find the maximum art score Vitaro can get.
Input
The first line contains and .
Each of the next lines contains , , and .
Each of the following lines contains , , and .
All values are integers.
Output
Print one line containing the maximum art score Vitaro can get.
Constraints
- .
- .
- ().
- ().
- ().
- ().
- ().
- ().