#K51572. Spreadsheet Formula Evaluation

    ID: 29117 Type: Default 1000ms 256MiB

Spreadsheet Formula Evaluation

Spreadsheet Formula Evaluation

You are given a single arithmetic expression that represents a simplified spreadsheet formula. The expression may include basic arithmetic operators: addition (+), subtraction (-), multiplication (*), and division (/), as well as parentheses to enforce precedence. Cell references in the expression are represented by an uppercase letter followed by one or two digits (for example, A1, B12). You are also provided with a table of cell values. When evaluating the expression, each cell reference should be replaced by its corresponding integer value from the table. If a cell reference is not found in the table, its value defaults to 0. It is guaranteed that the formula is valid and that the result of all operations is an integer.

The task is to evaluate the given formula and output its result.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains a string representing the formula.
  2. The second line contains an integer n indicating the number of cell-value pairs.
  3. The following n lines each contain a cell reference and its integer value separated by space.

For example:

(A1 + B2) * (C3 - D4) / E5
5
A1 1
B2 2
C3 9
D4 4
E5 3

outputFormat

Output the evaluated result of the formula as an integer to standard output (stdout).

## sample
A1 + B2
2
A1 1
B2 2
3