Rectangles Inside Rectangle
Time limit1sMemory limit512 MB
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 and . He also has small axis-parallel rectangles inside the large rectangle. The weight of the -th rectangle is . 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 and (, ) denoting the number of small rectangles and the width of the large rectangle.
The -th of the following lines contains five integers , , , and (, , , ) where is the weight of the -th rectangle. Here, means the lower left and upper right corner of the -th rectangle are and , while means the lower left and upper right corner of the -th rectangle are and .
It is guaranteed that for all , , , and . Additionally, the sum of all does not exceed .
Output
For each test case, output an integer which denotes the maximum sum of weights.
Hint
For the third test, Bobo can choose the -rd, -th and -th rectangles.