#K45597. Maximum Water Distribution Calculation

    ID: 27788 Type: Default 1000ms 256MiB

Maximum Water Distribution Calculation

Maximum Water Distribution Calculation

The community receives water supplies over a series of days. Each day, an event occurs: either a water tank event or a request event. In a tank event, a new water supply arrives with a given capacity. The water available immediately becomes equal to the tank's capacity. In a request event, a specified amount of water is requested from the current tank supply. If the current tank has enough water to meet the full request, the water is dispensed (i.e. deducted from the tank), and the total water distributed increases by that requested amount. Otherwise, the request is ignored.

Formally, if we denote an event by a string in the format "tank X" or "request Y", then:

  • On a tank event, the water available is set to \(X\).
  • On a request event, if the current available water \(W \geq Y\), then the water is dispensed and \(W\) decreases by \(Y\); otherwise, nothing happens.

Your task is to compute the maximum total amount of water that can be distributed according to these events.

inputFormat

The input is read from standard input (stdin) and has the following format:

N
Event_1
Event_2
... 
Event_N

Where:

  • N is an integer (\(1 \leq N \leq 10^5\)) representing the number of events.
  • Each Event is a string of the form "tank X" or "request Y", where X and Y are positive integers.

outputFormat

Output a single integer to standard output (stdout) representing the maximum total amount of water that was successfully distributed.

## sample
5
tank 100
request 50
tank 200
request 150
request 100
200