#K49942. Arithmetic Expression Evaluation

    ID: 28754 Type: Default 1000ms 256MiB

Arithmetic Expression Evaluation

Arithmetic Expression Evaluation

You are given an arithmetic expression as a string which consists of lowercase alphabetic variables, integers, the addition operator + and the multiplication operator *.

If the expression contains variables, they should be substituted with the provided integer values in the order of their first appearance. For example, in the expression a + 2 * b + 3 with values [5, 10], the variable a is replaced by 5 and b by 10.

The expression is evaluated following the standard operator precedence where multiplication has higher precedence than addition. In mathematical terms, the evaluation follows the rule:

\(\text{result} = \sum (\text{term})\) where each term is either a number or a product of numbers, i.e., \(\text{term} = \prod (\text{factor})\).

Your task is to compute and output the result of the evaluated expression.

inputFormat

The input is given via stdin and consists of two lines:

  • The first line contains a non-empty string representing the arithmetic expression. The expression may contain spaces.
  • The second line contains zero or more space-separated integers representing the values for the variables in the order of their first appearance. If there are no variables or values, this line will be empty.

outputFormat

Output the integer result after evaluating the expression. The output should be printed on a single line to stdout.

## sample
a+2*b+3
5 10
28