Accumulator Apex
Time limit3sMemory limit2048 MB
Given a starting value x and k lists, take elements from the front of each list and add them to x, keeping x non-negative, to maximize the final x.
- Level
Hard8 of 10
- Topics
- Greedy, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Allyn is playing a new strategy game called "Accumulator Apex". In this game, Allyn is given the initial value of an integer , called the accumulator, and lists of integers. Allyn can make multiple turns. On each turn, Allyn can withdraw the leftmost element from any non-empty list and add it to the accumulator , as long as the resulting is non-negative. Allyn can end the game at any moment. The goal is to get the largest possible value of the accumulator . Find the largest value of Allyn can reach.
Input
The first line contains two integers and (, ), the initial value of the accumulator and the number of lists. The next lines describe the lists. Each line starts with an integer (), followed on the same line by the elements of the list from left to right. Each element has an absolute value of at most , and the total size of all lists is at most .
Output
Print the largest value of the accumulator that Allyn can get, on a single line.
Hint
In the first input, starts at 1. Allyn takes the first integer from the first list to get , then takes the next integer 2 from the same list to get . After that, Allyn adds the integers from the second list to get , and then the integers from the third list to get . In the second input, Allyn adds the first integer from the second list to get , then adds the elements of the first list to get . No more integers can be added to increase .