RoAlgo Contest #4 (incepatori) | ast

This was the problem page during the contest. Access the current page here.
Time limit: 0.1s Memory limit: 64MB Input: Output:

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 NN nodes.

Input data

The first line contains 2626 numbers representing the values of the variables (the first number represents the value of the variable aa, the second bb, \dots).
The second line contains a natural number NN.
The next NN lines describe each node. If a line ii contains a natural number or a letter of the Latin alphabet, then node ii contains a constant or a variable, respectively. Otherwise, line ii contains a string from the set {+,-,*,(),/}\{\texttt{+}, \texttt{-}, \texttt{*}, \texttt{()}, \texttt{/}\}, followed by a number kik_i representing the number of children of node ii, followed by a sequence of kik_i natural numbers representing the children of node ii.

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

  • 1N1051 \leq N \leq 10^5
  • It is ensured that the result fits in the long long\texttt{long long} 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, 1+2=31 + 2 = 3.

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

(a+(c+d))(3+(5+6))=14(a + (c + d)) \Leftrightarrow (3 + (5 + 6)) = 14

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

250(ab+c)(d10)4250(12+3)(110)4=50250-(a*b+c)*(d*10)*4 \Leftrightarrow 250-(1*2+3)*(1*10)*4 = 50
Note, node 11 in the image below contains the symbol -, not +. It is a mistake.

Log in or sign up to be able to send submissions!