Evaluate Reverse Polish Notation

Practice this coding question on Coding Gurukul.

Problem statement

A compact calculator receives an expression in postfix form. Numbers appear before the operation that combines them, allowing the calculator to evaluate the expression with a stack instead of precedence rules or parentheses. Your task Evaluate the Reverse Polish Notation expression. Supported operators are +, -, * and /. Division truncates toward zero. Important details On an operator, pop the right operand first and then the left operand. The expression is valid and never divides by zero. A minus sign inside a numeric token denotes a negative integer, while a token consisting only of - is subtraction. Worked example Tokens 8 3 - 2 * first compute 8 - 3 = 5, then 5 * 2 = 10. Print 10.

Input format

First line: n. Next line: n space-separated tokens.

Output format

Print the final integer result.

Constraints

1 <= n <= 10000; numeric tokens are between -200 and 200; all intermediate results fit signed 32-bit integers.

Sample input

5
8 3 - 2 *

Sample output

10

Sign in to run or submit a solution in C++, Java or Python.