New Year Festival
Time limit7sMemory limit1024 MB
Schedule n non-overlapping events of fixed lengths so that the sum of their piecewise linear costs, each a function of start time, is minimized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Sorting
- Solved
- No attempts yet
Problem
The ICPC (Incredibly Colossal and Particularly Comfortable) Theater holds several traditional events to celebrate the New Year.
Each event has a fixed duration that cannot be changed. You choose the start time of each event freely, but no two events may overlap. An event may start at the moment another event ends.
The start time of an event affects its cost. The cost of an event is a continuous piecewise linear function (a polygonal line) of its start time. Different events may have different cost functions.
Schedule all the events so that the total cost is minimized.
Input
The input consists of a single test case in the following format.
The first line contains , the number of events, where .
The description of the -th event has the following format.
The integer is the number of vertices of the cost function, and is the duration of the event. Here and .
Each of the next lines gives a vertex of the cost function, where and . For each , the slope is an integer.
The start time of the event must satisfy . For with , the cost of the event is .
The total number of vertices over all cost functions is at most .
Output
Output the minimum possible total cost in a single line.
It is guaranteed that at least one schedule without overlaps exists. The answer is always an integer.
Hint
For Sample Input 1, the (start time, end time) pairs , , and for events 1, 2, and 3 give the minimum total cost without overlaps. The cost of event 1 is . The costs of events 2 and 3 are 0 and 460.
For Sample Input 2, the minimum cost is reached with the pairs , , , and for the four events.

Figure K.1. Cost functions in Sample Input 1 and a sample schedule

Figure K.2. Cost functions in Sample Input 2 and a sample schedule
In Figures K.1 and K.2, the polylines in the top figure are cost functions, and the rectangles in the bottom figure show event durations in a schedule with minimum total cost.