You are given a tree with $$$n$$$ vertices. Vertex $$$i$$$ has a positive integer weight $$$a_i$$$.
You want to choose positive integers $$$b_1, b_2, \dots, b_n$$$.
Each edge of the tree contains one of the characters <, =, or >. If an edge is written as $$$u\ v\ c$$$, then:
Among all valid assignments, minimize $$$$$$ a_1 b_1 + a_2 b_2 + \dots + a_n b_n. $$$$$$
For the given constraints, it can be proved that the optimal assignment is unique.
The first line contains one integer $$$n$$$ ($$$2 \le n \le 200'000$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1'000'000$$$).
Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ and one character $$$c_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$, $$$c_i$$$ is one of <, =, >), meaning that the tree contains an edge between $$$u_i$$$ and $$$v_i$$$, and the required relation is $$$b_{u_i}\ c_i\ b_{v_i}$$$.
Print the minimum possible value of $$$$$$ a_1 b_1 + a_2 b_2 + \dots + a_n b_n $$$$$$ in the first line.
In the second line, print the unique optimal assignment $$$b_1, b_2, \dots, b_n$$$.
It can be proved that, for the given constraints, the minimum value always fits in a signed $$$64$$$-bit integer.
65 1 4 3 2 61 2 <2 3 >2 4 <4 5 =4 6 >
32 1 2 1 3 3 1
79 2 8 1 7 3 61 2 =2 3 <3 4 >2 5 =5 6 <5 7 >
76 2 2 3 1 2 3 1
The first sample requires:
One optimal assignment is $$$1\ 2\ 1\ 3\ 3\ 1$$$, and its cost is $$$$$$ 5 \cdot 1 + 1 \cdot 2 + 4 \cdot 1 + 3 \cdot 3 + 2 \cdot 3 + 6 \cdot 1 = 32. $$$$$$
| Название |
|---|


