You are given a data structure called an Abstract Syntax Tree. You need to find the expression and the value of the expression defined by the AST. The tree consists of nodes.
Input data
The first line contains numbers representing the values of the variables (the first number represents the value of the variable , the second , ).
The second line contains a natural number .
The next lines describe each node. If a line contains a natural number or a letter of the Latin alphabet, then node contains a constant or a variable, respectively. Otherwise, line contains a string from the set , followed by a number representing the number of children of node , followed by a sequence of natural numbers representing the children of node .
Output data
The first line will contain a string representing the expression defined by the AST, and the second line will contain an integer representing the value of the expression from the first line.
Constraints and clarifications
- It is ensured that the result fits in the data type in the C++ language
Example 1
stdin
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3
+ 2 2 3
1
2
stdout
1+2
3
Explanation
As we know, .
Example 2
stdin
3 1 5 6 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7
() 1 2
+ 2 3 4
a
() 1 5
+ 2 6 7
c
d
stdout
(a+(c+d))
14
Explanation
Example 3
stdin
1 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15
- 2 2 3
250
* 2 4 5
() 1 11
* 2 7 8
* 2 10 9
() 1 6
4
10
d
+ 2 12 13
* 2 14 15
c
a
b
stdout
250-(a*b+c)*(d*10)*4
50
Explanation
Note, node in the image below contains the symbol -
, not +
. It is a mistake.