Maximum Sum
Time limit1sMemory limit128 MB
Given n boxes of numbered balls, pick at most one ball per box in order to form a non-decreasing sequence with maximum possible sum.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
You are given a sequence of boxes. Each box contains several balls, and every ball has a whole number written on it.
You choose some of the boxes (one, several, or all of them) and take exactly one ball from each chosen box, keeping the boxes in their original order. Arranging the taken balls in that order gives a sequence of numbers. Consider only the choices for which this sequence is non-decreasing (each number is at least the previous one), and compute the largest possible sum of the taken numbers.
Input
The first line contains . Each of the following lines describes one box: it begins with the number of balls in that box, followed by the numbers written on those balls.
Output
Output a single integer: the maximum sum described above.
Constraints
. Each box contains at least one ball and no more than balls. Every number written on a ball is between and , inclusive.