#C5589. Determine the Final State of a Toy
Determine the Final State of a Toy
Determine the Final State of a Toy
You are given a toy whose state is initially 0. The toy processes a series of instructions. Each instruction is in one of the following forms:
- ADD x: Add the integer x to the current state.
- SUB x: Subtract the integer x from the current state.
- MUL x: Multiply the current state by the integer x.
The process can be mathematically represented as follows. Let \( S_0 = 0 \) be the initial state. Then, for each instruction \( i \) (\( 1 \le i \le n \)) with operation \( op_i \) and value \( x_i \):
- If \( op_i = \texttt{ADD} \), then \( S_i = S_{i-1} + x_i \).
- If \( op_i = \texttt{SUB} \), then \( S_i = S_{i-1} - x_i \).
- If \( op_i = \texttt{MUL} \), then \( S_i = S_{i-1} \times x_i \).
Your task is to compute the final state \( S_n \) after processing all the instructions.
inputFormat
The input is read from standard input (stdin) and consists of:
- The first line contains an integer n representing the number of instructions.
- The following n lines each contain a string representing an instruction in the format described above.
outputFormat
Output to standard output (stdout) a single integer representing the final state of the toy after processing all instructions.
## sample5
ADD 5
MUL 3
SUB 2
ADD 10
MUL -1
-23
</p>