0 #CSPJ2019B. 公交换乘 (Bus Transfer)

公交换乘 (Bus Transfer)

Description

City B, a famous city, has introduced a preferential plan for changing from subways to buses in order to encourage people to use public transportation:

  1. After taking the subway once, you will get a discount ticket, valid for 4545 minutes. You can use this ticket during its valid period to take a bus whose fare does not exceed the ticket's fare for free. A valid period means that the difference between the time you take the bus and the time you take the subway is less than or equal to 4545 minutes, which is: tbustsubway45t_{bus} - t_{subway} \le 45.
  2. The discount ticket can be stacked up to multiple tickets, which means you can take the subway several times and then use one of the tickets to take the bus.
  3. When taking the bus, if you can use a ticket, you will definitely use it. If there are multiple tickets that meet the conditions, you will use the earliest one.

Given a list of recent public transportation used by Xiaoxuan, help him calculate how much he needs to pay.

Input Format

The first line contains a single integer nn, which is the number of rides in the list.

Each of the following nn lines contains 33 integers, seperated by spaces. The 11st integer in the iith line represents the type of transportation that Xiaoxuan used, where 00 means the subway, and 11 means the bus. The 22nd integer represents the priceiprice_i of the iith ticket. The 33rd integer represents the time tit_i when he used this transportation in minutes (calculated from 00).

It is guaranteed that the list will be given in increasing order of the time when Xiaoxuan used the ride, and no two records will appear in the same minute.

Output Format

Print a single line containing how much Xiaoxuan needs to pay.

6
0 10 3
1 5 46
0 12 50
1 3 96
0 5 110
1 6 135
36

First, you took a subway at time 33 for 1010 yuan.
Next, you took a bus at time 4646. You will use the ticket obtained from the first ride, so you don't have to pay.
The third line shows that you took a subway at time 5050 for a cost of 1212 yuan.
In the fourth ride, you took a bus at time 9696, and since it has been more than 4545 minutes since the last time you took the subway, you will have to pay 33 yuan.
In the fifth ride, you took a subway at time 110110 for a cost of 55 yuan.
At the last ride, you took the bus at time 135135, but you can't use the last ticket because the bus's fare is higher than the fare of your only valid ticket (6>5)(6>5). So you have to pay 66 yuan.

In total, you'll need to pay 10+12+3+5+6=3610 + 12 + 3 + 5 + 6 = 36 yuan.

6
0 5 1
0 20 16
0 7 23
1 18 31
1 4 38
1 7 68
32

First, you took a subway at time 11 for 55 yuan.
Next, you took another subway at time 1616 for 2020 yuan.
The third line shows that you took another subway at time 2323 for a cost of 77 yuan.
In the fourth ride, you took a bus at time 3131 with a cost of 1818 yuan, and the only valid ticket is the one you got after taking the second subway, so you will use it to take this bus without any cost.
In the fifth ride, you took a bus at time 3838 with a cost of 44 yuan. You have two valid tickets to use so you will use the first one which you got from taking the subway at time 11.
At the last ride, you took the bus at time 6868, and you can use the last ticket to avoid paying 77 yuan.

In total, you'll need to pay 5+20+7=325 + 20 + 7 = 32 yuan.

Constraints

For 30%30\% of the data, n1000,ti106n \le 1000, t_i \le 10^6.
For another 15%15\% of the data, ti107t_i \le 10^7, and all tickets' prices are equal.
For another 15%15\% of the data, ti109t_i \le 10^9, and all tickets' prices are equal.
For 100%100\% of the data, n105,ti109,1pricei1000n \le 10^5, t_i \le 10^9, 1 \le price_i \le 1000.