Page not loading? Try clicking here.
Placeholder

#1221

Postfix Notation 1s 32MB

Problems

The calculation method we normally use is called Infix Notation, where the operator is placed between operands. For example, in A + B, the + operator is between operands A and B.

In computer science, Postfix Notation (also called Reverse Polish Notation) is often used. In Postfix Notation, the operator is after the operands, so A B + represents A + B.

For example, in Infix Notation, the expression (5+8)*2 requires parentheses to indicate that 5+8 should be calculated first, because * has higher precedence than +.

However, the same expression in Postfix Notation is written as 5 8 + 2 *. In Postfix Notation, the operator precedence is clearly defined without parentheses, which is why many software calculators use it.

Your task is to write a program that calculates the result of an expression given in Postfix Notation.

Note: Division by zero does not occur in the input.


Input

The first line contains an integer M (3 ≤ M ≤ 11), the total number of operators and operands.

The second line contains M tokens separated by a single space. Each token is either:

  • An operand X (0 ≤ X ≤ 9)

  • An operator among *, /, +, -

The input is guaranteed to represent a complete expression, so there are no missing operands or operators.


Output

Calculate the result of the Postfix expression.

For division, discard the fractional part and output the integer quotient. For example, 8 3 / results in 2.


Example #1

3

2 3 +
5

Example #2

3

9 3 /
3

Example #3

5

5 8 + 2 *
26

Example #4

9

9 2 3 4 * + 3 - -
-2


Source

JUNGOL

You must sign in to write code.