This page is still under construction.

Parts of this page are still being built. What you see may change.

Rectangles Inside Rectangle

Time limit1sMemory limit512 MB

Summary
Pick a non-overlapping subset of axis-parallel rectangles that each touch the left or right side of a big rectangle, maximizing total weight.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Intervals, Array
Solved
No attempts yet

Problem

Bobo has a large rectangle with lower left and upper right corners at (0,0)(0, 0) and (w,106)(w, 10^6). He also has nn small axis-parallel rectangles inside the large rectangle. The weight of the ii-th rectangle is v_iv\_i. For each rectangle, either its left border or its right border (but not both) coincides with the left or right side of the large rectangle.

Bobo would like to choose a subset of small rectangles in such a manner that the rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). Among all the possibilities, he wants the one with the maximum possible sum of weights.

Input

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains two integers nn and ww (1≤n≤20001 \leq n \leq 2000, 2≤w≤1062 \leq w \leq 10^6) denoting the number of small rectangles and the width of the large rectangle.

The ii-th of the following nn lines contains five integers type_i\mathit{type}\_i, l_il\_i, a_ia\_i, b_ib\_i and v_iv\_i (type_i∈{0,1}\mathit{type}\_i \in \{0, 1\}, 0≤a_i<b_i≤1060 \leq a\_i < b\_i \leq 10^6, 1≤l_i<w1 \leq l\_i < w, 0≤v_i≤1060 \leq v\_i \leq 10^6) where v_iv\_i is the weight of the ii-th rectangle. Here, type_i=0\mathit{type}\_i = 0 means the lower left and upper right corner of the ii-th rectangle are (0,a_i)(0, a\_i) and (l_i,b_i)(l\_i, b\_i), while type_i=1\mathit{type}\_i = 1 means the lower left and upper right corner of the ii-th rectangle are (w−l_i,a_i)(w - l\_i, a\_i) and (w,b_i)(w, b\_i).

It is guaranteed that for all 1≤i<j≤n1 \leq i < j \leq n, a_i≠a_ja\_i \ne a\_j, a_i≠b_ja\_i \ne b\_j, b_i≠a_jb\_i \ne a\_j and b_i≠b_jb\_i \ne b\_j. Additionally, the sum of all nn does not exceed 20002000.

Output

For each test case, output an integer which denotes the maximum sum of weights.

Hint

For the third test, Bobo can choose the 33-rd, 44-th and 55-th rectangles.

Examples1

  1. Example 1

    Input
    3 10
    0 3 1 6 12
    0 3 3 4 100
    1 9 2 5 11
    3 10
    0 3 1 6 12
    0 1 3 4 5
    1 9 2 5 11
    6 5
    1 1 17 32 4
    0 3 1 18 7
    1 3 4 8 12
    1 2 15 20 14
    1 1 30 33 16
    1 4 2 16 13
    
    Expected output
    100
    16
    42