CCCGuide

Problem J4: Wait Time

2015 · J4 · Original problem · DMOJ

Problem Description

You exchange text messages with your friends. Since you receive so many messages, you want to measure how long your friends have to wait for your replies.

Your message device records each received and sent message in order using the following two kinds of entries:

Your message device sends and receives messages instantaneously, and for each consecutive pair of entries described above, either

Several rules of message etiquette are always followed:

The wait time for a message is the time that passes between when you receive it and the time you reply to it. If a friend XX received a reply to each message they sent, the total wait time for friend XX is the sum of all wait times for all messages from friend XX. Otherwise, the total wait time for friend XX is −1-1.

Your job is to determine the total wait time for each friend.

Input Specification

The input consists of the integer MM (1≤M≤201 \leq M \leq 20), followed by MM lines, where each line consists of one character (W, R, or S), followed by a space, followed by an integer XX (1≤X≤1001 \leq X \leq 100). These MM lines are the entries described above (in order).

Output Specification

Output one line for each friend that sent a message in the form XX TT where XX is a friend number and TT is the total wait time for friend XX. The lines are in increasing order of the friend numbers.

Sample Input 1

5
R 2
R 3
W 5
S 2
S 3

Output for Sample Input 1

2 6
3 6

Explanation of Output for Sample Input 1

Friend 2 sends a message at time 00 and Friend 3 sends a message at time 11. Friend 2 receives a reply at time 66 and Friend 3 receives a reply at time 77.

Sample Input 2

14
R 12
W 2
R 23
W 3
R 45
S 45
R 45
S 23
R 23
W 2
S 23
R 34
S 12
S 34

Output for Sample Input 2

12 13
23 8
34 2
45 -1

Explanation of Output for Sample Input 2

For Friend 12, a message is received at time 0 and replied to at time 13. For Friend 23, two messages are exchanged, with the first message having a wait time of 6 seconds and the second message having a wait time of 2 seconds. For Friend 34, a message is received at time 10 and replied to at time 12. Friend 45 sends a message which is never replied to.