#U2122OG1. Apple Catching G

Apple Catching G

It's raining apples! At certain points in time, some number of apples will hit the number line. At certain points in time, some of Farmer John's cows will arrive on the number line and start catching apples.

If an apple hits the number line without a cow to catch it, it is lost forever. If a cow and an apple arrive at the same time, the cow catches it. Each cow can travel one unit per second. Once a cow catches a single apple, she exits the number line.

If FJ's cows collaborate optimally, how many apples can they catch in total?

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN (1N21051≤N≤2⋅105), the number of times apples hit the number line or FJ's cows appear.The next NN lines each contain four integers qiqi, titi, xixi, and nini (qi{1,2},0ti109**,0xi109,1ni10**3qi∈{1,2},0≤ti≤109,0≤xi≤109,1≤ni≤103).

  • If qi**=**1qi=1, this means that nini of FJ's cows arrive on the number line at time titi at location xixi.
  • If qi**=**2qi=2, this means that nini apples will hit the number line at time titi at location xixi.

It is guaranteed that all of the ordered pairs (ti,xi**)**(ti,xi) are distinct.

OUTPUT FORMAT (print output to the terminal / stdout):

The maximum number of apples FJ's cows may collectively catch.

SAMPLE INPUT:

5
2 5 10 100
2 6 0 3
2 8 10 7
1 2 4 5
1 4 7 6

SAMPLE OUTPUT:

10

In this example, none of the 100100 apples that land at time t=5t=5 may be caught. Here is a way for 1010 apples to be caught:

  • All six of FJ's cows that arrive at time t=4t=4 catch one of the apples that land at time t=8t=8.
  • One of FJ's cows that arrive at time t=2t=2 catches one of the apples that land at time t=8t=8.
  • Three of the remaining cows that arrive at time t=2t=2 catch one of the apples that land at time t=6t=6.

SAMPLE INPUT:

5
2 5 10 100
2 6 0 3
2 8 11 7
1 2 4 5
1 4 7 6

SAMPLE OUTPUT:

9

Here again, none of the apples that land at time t=5t=5 may be caught. Furthermore, none of the cows that arrive at time t=2t=2 may catch any of the apples that land at time t=8t=8. Here is a way for 99 apples to be caught:

  • All six of FJ's cows that arrive at time t=4t=4 catch one of the apples that land at time t=8t=8.
  • Three of the remaining cows that arrive at time t=2t=2 catch one of the apples that land at time t=6