A three-person summit push team radios altitude-gain readings back to basecamp during their final ascent. Each radio call is logged in order and is either a plain number or one of three relay codes that basecamp uses to keep the log honest over a noisy channel:
v records a fresh altitude-gain reading of v metres.RELAY records a reading equal to the sum of the two most recently logged valid readings (the log is guaranteed to hold at least two valid readings by the time a RELAY call arrives).DOUBLE records a reading equal to twice the most recently logged valid reading (the log is guaranteed to hold at least one valid reading by the time a DOUBLE call arrives).SCRATCH erases the most recently logged valid reading entirely, as if it had never been radioed in (the log is guaranteed to hold at least one valid reading by the time a SCRATCH call arrives).After the whole log has been processed, basecamp wants the sum of every reading still standing in the log. Compute that total.
The first line contains a single integer n (1 <= n <= 1000), the number of radio calls. Each of the next n lines contains one call: either an integer v (-310^4 <= v <= 310^4), or one of the strings RELAY, DOUBLE, SCRATCH. The calls are guaranteed to be valid in the sense described above.
Print a single integer: the sum of all readings remaining in the log after every call has been processed.
v satisfies -310^4 <= v <= 310^4.Example 1
Input
5 5 2 SCRATCH DOUBLE RELAY
Expected
30
Explanation
The log starts empty. 5 -> [5]. 2 -> [5, 2]. SCRATCH removes the 2 -> [5]. DOUBLE records twice the last reading (5*2=10) -> [5, 10]. RELAY records the sum of the last two readings (5+10=15) -> [5, 10, 15]. The final sum is 5+10+15 = 30.
Example 2
Input
8 5 -2 4 SCRATCH DOUBLE 9 RELAY RELAY
Expected
27
Explanation
5 -> [5]. -2 -> [5, -2]. 4 -> [5, -2, 4]. SCRATCH removes the 4 -> [5, -2]. DOUBLE records twice -2 (-4) -> [5, -2, -4]. 9 -> [5, -2, -4, 9]. RELAY records the sum of the last two readings (-4+9=5) -> [5, -2, -4, 9, 5]. RELAY again records the sum of the last two readings (9+5=14) -> [5, -2, -4, 9, 5, 14]. The final sum is 5-2-4+9+5+14 = 27.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →