#K49082. Stack-Based Calculator

    ID: 28563 Type: Default 1000ms 256MiB

Stack-Based Calculator

Stack-Based Calculator

You are given a list of tokens representing a Reverse Polish Notation (RPN) expression. The tokens are either integers (which may be negative) or one of the operators: +, -, *, or /. When an operator is encountered, the calculator pops the top two numbers from a stack (the second popped number is the left operand), applies the operator, and pushes the result back onto the stack.

Note that for the division operation, perform integer division with truncation towards zero. That is, given two operands \(a\) and \(b\), the result is computed as \(\text{int}(a/b)\).

After processing all tokens, the final value in the stack is the result of the expression and should be printed.

inputFormat

The input is read from stdin. The first line contains a single integer (n) which is the number of tokens. The second line contains (n) space-separated tokens, each of which is either an integer (possibly negative) or one of the operators: +, -, *, /.

outputFormat

Print the result of the stack-based calculation as a single integer to stdout.## sample

5
2 1 + 3 *
9

</p>