Under construction

Still being built. Things may change

Sot-tteok-sot-tteok

Time limit2sMemory limit1024 MB

Summary
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

NN 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 11, and the horizontal and vertical lines are perpendicular to each other. Number the horizontal lines 1,2,⋅⋅⋅1, 2, \cdot\cdot\cdot from the bottom and the vertical lines 1,2,⋅⋅⋅1, 2, \cdot\cdot\cdot from the left. The ii-th food lies on horizontal line y_iy\_i and spans vertical lines xl_ixl\_i through xr_ixr\_i. The length of the ii-th food is xr_i−xl_i+1xr\_i - xl\_i + 1, 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 NN, the total number of sausages and rice cakes. (1≤N≤250 000)(1 \leq N \leq 250\,000)

Each of the next NN lines contains xl_ixl\_i, xr_ixr\_i, y_iy\_i, and t_it\_i for the ii-th food, separated by spaces. (1≤xl_i≤xr_i≤109;(1 \leq xl\_i \leq xr\_i \leq 10^9; 1≤y_i≤109;1 \leq y\_i \leq 10^9; t_i∈{S,D})t\_i \in \{S, D\})

If t_i=St\_i = S, the food is a sausage. If t_i=Dt\_i = D, the food is a rice cake. The ii-th food lies on horizontal line y_iy\_i and spans vertical lines xl_ixl\_i through xr_ixr\_i.

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

Examples3

  1. Example 1

    Input
    4
    1 5 1 S
    1 3 2 D
    1 5 3 S
    4 13 4 D
    
    Expected output
    13
    
  2. Example 2

    Input
    5
    7 13 10 D
    9 21 8 S
    5 12 14 D
    1 9 3 D
    8 16 11 S
    
    Expected output
    46
    
  3. Example 3

    Input
    7
    2 3 1 S
    1 2 2 D
    3 4 2 S
    2 3 3 D
    1 2 4 D
    3 4 4 S
    2 3 5 S
    
    Expected output
    0