#C4699. Evaluate RPN Expression

    ID: 48265 Type: Default 1000ms 256MiB

Evaluate RPN Expression

Evaluate RPN Expression

You are given an expression in Reverse Polish Notation (RPN), also known as postfix notation. In RPN, every operator follows all of its operands. For example, the expression 3 4 + is interpreted as 3 + 4 and evaluates to 7.

Your task is to evaluate a given RPN expression and output the integer result. The expression will consist of integers and the operators +, -, *, and /. Note that division should truncate toward zero. You may assume that the given RPN expression is valid and does not result in division by zero.

The evaluation follows the rules of a stack-based computation. When an operator is encountered, the two most recent numbers are popped from the stack (the first popped is the second operand) and the operation is performed. The result is then pushed back onto the stack.

For a formal definition, let the tokens be denoted by \(t_1, t_2, \dots, t_n\). If \(t_i\) is an integer, push it onto the stack. If \(t_i\) is an operator (e.g. \(+\)), pop the top two values \(a\) and \(b\) (with \(b\) being the second popped), compute \(a \mathbin{op} b\) and push the result back. The final result is the only value in the stack.

inputFormat

The input consists of a single line containing a space-separated RPN expression.

Example Input: 3 4 +

outputFormat

Output a single integer: the result of evaluating the RPN expression.

Example Output: 7

## sample
3 4 +
7