#P1912A. Accumulator Apex
Accumulator Apex
No submission language available for this problem.
Description
Allyn is playing a new strategy game called "Accumulator Apex". In this game, Allyn is given the initial value of an integer , referred to as 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 if the resulting is non-negative. Allyn can end the game at any moment. The goal of the game is to get the largest possible value of the accumulator . Please help Allyn find the largest possible value of the accumulator they can get in this game.
The first line of the input contains two integers and () — the initial value of the accumulator and the number of lists. The next lines contain the description of lists: an integer () followed on the same line by elements of the list in the order from left to right. Each element of lists does not exceed by the absolute value, and the total size of all lists does not exceed .
The sole line of the output should contain the largest value of the accumulator Allyn can get.
Input
The first line of the input contains two integers and () — the initial value of the accumulator and the number of lists. The next lines contain the description of lists: an integer () followed on the same line by elements of the list in the order from left to right. Each element of lists does not exceed by the absolute value, and the total size of all lists does not exceed .
Output
The sole line of the output should contain the largest value of the accumulator Allyn can get.
Note
In the first input, we start with . Then, we can take the first integer from the first list and get — adding the next integer from the first list we get . After that, we can add the integers from the second list and obtain . Finally, we can add the integers from the third list and obtain .
In the second input, we can add the first integer from the second list and get . Then, by adding the elements from the first list, we get . We cannot add more integers to increase .