#P5661. Calculate Total Transit Cost with Discount Tickets

    ID: 18889 Type: Default 1000ms 256MiB

Calculate Total Transit Cost with Discount Tickets

Calculate Total Transit Cost with Discount Tickets

City B has introduced an incentive for using public transportation. When a passenger rides the subway, they earn a discount ticket that can be used for a free bus ride if the bus ride fare does not exceed the subway fare. The discount ticket is valid for 45 minutes from the start time of the subway ride. Formally, if a bus ride starts at time \(t_{bus}\) and the corresponding subway ride started at \(t_{subway}\), then the discount ticket is valid if:

[ t_{bus} - t_{subway} \leq 45 ]

Additional rules are as follows:

  • Discount tickets can be accumulated. That is, a passenger may ride the subway several times and accumulate multiple discount tickets.
  • When taking a bus, if one or more valid discount tickets are available (i.e. the ticket’s subway fare is at least the bus fare and the ticket is still valid), the ticket with the earliest subway ride issuance is used automatically. If no valid discount ticket exists, the bus fare must be paid in full.

You are given a series of transit records. Each record consists of the time (in minutes), the type of transit (subway or bus), and the fare. For a subway ride, the passenger always pays the fare and receives a discount ticket (with a value equal to the subway fare). For a bus ride, if a valid discount ticket is available, it is used to cover the cost and no fare is paid; otherwise, the bus fare is paid.

Your task is to compute the total cost incurred by the passenger.

inputFormat

The first line contains an integer n (1 ≤ n ≤ 105), the number of transit records. Each of the following n lines contains a transit record with three items separated by spaces:

  • An integer t (0 ≤ t ≤ 109) representing the start time in minutes.
  • A string type which is either subway or bus.
  • An integer fare (1 ≤ fare ≤ 104) representing the fare for that ride.

The records are given in chronological order.

outputFormat

Output a single integer: the total cost incurred by the passenger across all transit records.

sample

3
10 subway 20
20 bus 15
100 bus 10
30