Under construction

Still being built. Things may change

Parking Lot

Interview

Time limit2sMemory limit512 MB

Summary
Simulate a single-line parking lot where each arriving vehicle takes the first free gap from the entrance that fits, and total the fees for vehicles that park.
Level

Medium6 of 10

Topics
Simulation, Implementation, Array, Intervals
Solved
No attempts yet

Problem

A parking lot uses a strip of land where vehicles have to be kept in a single line, one behind another. The fee is fixed at 10 reais per parked vehicle and is charged at the entrance, regardless of the size of the vehicle and how long it stays. The lot is very busy, so not every vehicle that arrives gets a place to park.

When a vehicle arrives, the attendant first decides whether there is room for that vehicle. He walks the lot on foot, from the entrance to the far end, looking for a space that is free and whose length is greater than or equal to the length of the vehicle. To save time and effort, the attendant picks the first suitable space he finds, that is, the space closest to the entrance.

Once the space is found, the attendant walks back to the entrance, takes the vehicle and parks it at the beginning of that space. If the attendant finds no suitable space, the vehicle does not enter the lot and no fee is charged. After it is parked, a vehicle is not moved until the moment it leaves the lot.

The owner of the lot wants to know whether the attendants have charged the parked vehicles correctly. Given the list of arrivals and departures, write a program that determines the expected total revenue.

Input

The input holds several test cases. The first line of a test case contains two integers CC and NN (1≤C≤10001 \le C \le 1000, 1≤N≤100001 \le N \le 10000). CC is the length of the parking lot in metres and NN is the total number of events, counting arrivals and departures. Each of the next NN lines describes one arrival or one departure.

A line for an arrival contains the letter C followed by two integers PP and QQ (1000≤P≤99991000 \le P \le 9999, 1≤Q≤10001 \le Q \le 1000), separated by single blanks. PP is the licence plate of the vehicle and QQ is its length. A line for a departure contains the letter S followed by one integer PP, separated by a single blank, where PP is the licence plate of the vehicle. The events are given in chronological order.

At the start of each test case the lot is empty. A vehicle leaves the lot only if it is really parked, and the plate of an arriving vehicle is never equal to the plate of a vehicle already parked. The input ends at end of file.

Output

For each test case print one line holding an integer, the revenue of the parking lot in reais.

Examples5

  1. Example 1

    Input
    10 7
    C 1234 5
    C 1111 4
    C 2222 4
    C 4321 3
    S 1111
    C 2002 6
    C 4321 3
    30 10
    C 1000 10
    C 1001 10
    C 1002 10
    S 1000
    S 1002
    C 1003 20
    S 1001
    C 1004 20
    S 1004
    C 1005 30
    20 10
    C 1234 20
    C 5678 1
    S 1234
    C 1234 20
    C 5678 1
    S 1234
    C 5678 1
    C 1234 20
    C 5555 1
    S 5678
    
    Expected output
    30
    50
    40
    
  2. Example 2

    Input
    1 1
    C 1000 1
    
    Expected output
    10
    
  3. Example 3

    Input
    1 3
    C 1000 2
    C 1000 1000
    C 9999 7
    
    Expected output
    0
    
  4. Example 4

    Input
    10 7
    C 1000 3
    C 1001 2
    C 1002 3
    S 1001
    C 1003 2
    C 1004 2
    C 1005 1
    
    Expected output
    50
    
  5. Example 5

    Input
    9 8
    C 1000 3
    C 1001 3
    C 1002 3
    S 1000
    S 1002
    C 1003 4
    S 1001
    C 1004 9
    
    Expected output
    40