Sot-tteok-sot-tteok
Time limit2sMemory limit1024 MB
Given horizontal food segments, find the largest total length of foods on one vertical line that alternate between sausage and rice cake from bottom to top.
- Level
Medium7 of 10
- Topics
- Segment tree, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
sausages or rice cakes are being grilled. The distance between two adjacent horizontal lines and the distance between two adjacent vertical lines of the grill are both , and the horizontal and vertical lines are perpendicular to each other. Number the horizontal lines from the bottom and the vertical lines from the left. The -th food lies on horizontal line and spans vertical lines through . The length of the -th food is , and no two foods overlap. The grill is large enough to grill all of the foods.

You want to make a sot-tteok-sot-tteok by threading one skewer of zero thickness along a vertical line of the grill. The skewer must hold at least one food, and the foods on it must alternate between sausage and rice cake in the order they are threaded. The first food may be either a sausage or a rice cake.

The size of your skewer is the sum of the lengths of the foods on it. Since the value grows with the size, you want the largest possible skewer. However, you cannot remove any sausages or rice cakes before making the skewer. You must thread every food that lies on one particular vertical line of the grill. You may not move foods or add new ones.
Write a program that takes the food information and prints the largest skewer you can make.
Input
The first line contains , the total number of sausages and rice cakes.
Each of the next lines contains , , , and for the -th food, separated by spaces.
If , the food is a sausage. If , the food is a rice cake. The -th food lies on horizontal line and spans vertical lines through .
Output
On the first line, print the maximum size of a sot-tteok-sot-tteok you can make, which is the largest sum of the lengths of the threaded foods. If you cannot make one, print .