All problems

Problem 393

Baseball Game

Easy · 10 pts · Array, Stack, Simulation

Problem

You keep score for a game with unusual rules. You get a list of operations and apply them in order to a record of scores, which starts empty. An integer x records a new score of x. '+' records a new score equal to the sum of the previous two scores. 'D' records a new score equal to double the previous score. 'C' cancels the previous score and removes it from the record. All operations are valid. Print the sum of all scores left in the record.

Input

First line: N, the number of operations. Second line: the N operations separated by spaces.

Output

Print the final sum of the record.

Constraints

1 <= N <= 1000; -30000 <= x <= 30000; all operations are valid

Example input

5
5 2 C D +

Example output

30

Explanation

For 5 2 C D +, the record goes 5 → 5 2 → 5 (C cancels 2) → 5 10 (D doubles 5) → 5 10 15 (+ adds 5 and 10). The sum is 30.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Remove K Adjacent Duplicates Next: Backspace String Compare