Under construction

Still being built. Things may change

Explosive Wiring

Time limit2sMemory limit512 MB

Summary
Pick a set of polyline wires so that each chosen wire touches exactly one other chosen wire, maximizing the total usefulness of the set.
Level

Medium7 of 10

Topics
Geometry, Graph, Dynamic programming
Solved
No attempts yet

Problem

You are designing the wiring for a new kind of computer chip. Unfortunately, the wires are made of a strange material that explodes under the wrong conditions. You have a set of wires that you can install on the chip, and each wire has a usefulness value.

Assume the chip lies on the x-axis. Each wire connects two different points on the x-axis and is described by a 2D polyline that connects those two points. A polyline is a sequence of points joined by straight line segments. All wires lie above the x-axis except where they touch the chip at their first and last points. In addition, all x-coordinates of each wire's polyline stay within the interval formed by that polyline's first and last points.

Two wires interfere if their polylines touch or intersect at one or more points. A wire does not interfere with itself. A set of wires is safe if every wire in the set interferes with exactly one other wire in the set. The utility of a set is the sum of its wires' usefulness values. Given a set of wires to choose from, find the largest utility over all safe subsets.

Input

The first line contains a single integer NN (1 ≤ NN ≤ 150), the number of wires.

Each of the next NN lines describes one wire. A line starts with two integers kk (0 ≤ kk ≤ 100 000), the usefulness of the wire, and pp (3 ≤ pp ≤ 10), the number of points in the polyline. Then come pp pairs of integers x1,y1,x2,y2,…,xp,ypx_1, y_1, x_2, y_2, \ldots, x_p, y_p (0 ≤ xix_i, yiy_i ≤ 100 000), which define the polyline (x1,y1)(x_1, y_1)-(x2,y2)(x_2, y_2)-...-(xp,yp)(x_p, y_p).

It is guaranteed that x1<xpx_1 < x_p and x1≤xi≤xpx_1 ≤ x_i ≤ x_p for 2≤i<p2 ≤ i < p. Also, y1=yp=0y_1 = y_p = 0 and yi>0y_i > 0 for 2≤i<p2 ≤ i < p. All points in the input are distinct.

Output

Print the largest utility over all safe subsets of the given wires.

Examples3

  1. Example 1

    Input
    1
    1 3 0 0 1 1 2 0
    
    Expected output
    0
    
  2. Example 2

    Input
    5
    1 3 0 0 50 50 100 0
    1 3 45 0 50 40 110 0
    1 3 10 0 20 10 30 0
    1 3 20 0 30 10 40 0
    1 3 35 0 45 10 50 0
    
    Expected output
    4
    
  3. Example 3

    Input
    5
    1 3 0 0 50 50 100 0
    1 3 45 0 50 40 110 0
    1 3 10 0 20 10 30 0
    1 3 20 0 30 10 40 0
    10 3 35 0 45 10 50 0
    
    Expected output
    11