Under construction

Still being built. Things may change

New Year Festival

Time limit7sMemory limit1024 MB

Summary
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.

nn

Description1\text{Description}_1

⋮\vdots

Descriptionn\text{Description}_n

The first line contains nn, the number of events, where 2≤n≤112 ≤ n ≤ 11.

The description of the ii-th event has the following format.

mm ll

x1x_1 y1y_1

⋮\vdots

xmx_m ymy_m

The integer mm is the number of vertices of the cost function, and ll is the duration of the event. Here 1≤m≤601 ≤ m ≤ 60 and 1≤l≤1081 ≤ l ≤ 10^8.

Each of the next mm lines gives a vertex (xj,yj)(x_j, y_j) of the cost function, where 0≤x1<⋯<xm≤1080 ≤ x_1 < \cdots < x_m ≤ 10^8 and 0≤yj≤1080 ≤ y_j ≤ 10^8. For each 1≤j<m1 ≤ j < m, the slope (yj+1−yj)/(xj+1−xj)(y_{j+1} - y_j)/(x_{j+1} - x_j) is an integer.

The start time tt of the event must satisfy x1≤t≤xmx_1 ≤ t ≤ x_m. For jj with xj≤t≤xj+1x_j ≤ t ≤ x_{j+1}, the cost of the event is yj+(t−xj)×(yj+1−yj)/(xj+1−xj)y_j + (t - x_j) \times (y_{j+1} - y_j)/(x_{j+1} - x_j).

The total number of vertices over all cost functions is at most 6060.

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 (330,380)(330, 380), (380,500)(380, 500), and (170,330)(170, 330) for events 1, 2, and 3 give the minimum total cost without overlaps. The cost of event 1 is 2500+(330−300)×0−2500350−300=10002500 + (330 - 300) \times \frac{0 - 2500}{350 - 300} = 1000. The costs of events 2 and 3 are 0 and 460.

For Sample Input 2, the minimum cost is reached with the pairs (384,544)(384, 544), (104,384)(104, 384), (544,704)(544, 704), and (720,960)(720, 960) 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.

Examples2

  1. Example 1

    Input
    3
    3 50
    300 2500
    350 0
    400 3000
    2 120
    380 0
    400 2400
    4 160
    0 800
    400 0
    450 100
    950 4600
    
    Expected output
    1460
    
  2. Example 2

    Input
    4
    2 160
    384 0
    1000 2464
    3 280
    0 2646
    441 0
    1000 2795
    1 160
    544 0
    2 240
    720 0
    1220 2000
    
    Expected output
    2022