Balanced Flow Tree#12237

Balanced Flow Tree

1 second
256 megabytes
Medium

You are given an undirected tree with nodes labeled through . Each node has an integer value b_u assigned to it. Your task is to orient (assign directions to) each edge of the tree so that, for every node , the following condition holds:

\text{outdeg}(u) - \text{indeg}(u) = b_u

where:

It is guaranteed that: |b_u| \le \deg(u), and \sum_{u=1}^n b_u = 0 where is the degree of in the undirected tree.

If there exists an orientation of edges that satisfies the above conditions, print YES followed by any valid orientation. Otherwise, print NO.

Input

The first line contains an integer (1 \le n \le 2 \cdot 10^5) — the number of nodes.

The second line contains integers b_1, b_2, \dots, b_n (|b_u| \le \deg(u), and \sum_{u=1}^n b_u = 0).

The next n-1 lines each contain two integers and (1 \le x, y \le n, x \neq y), denoting an undirected edge between nodes and . It is guaranteed that these edges form a tree.

Output

If a valid orientation exists, print:

  • The word YES on the first line.

  • Then n-1 lines, each containing two integers — indicating a directed edge from to .

If no valid orientation exists, print NO.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

For the first testcase:

Scoring

  • Subtask ( points): (n \le 16)

  • Subtask ( points): (n \le 1000)

  • Subtask ( points): Tree is a simple path

  • Subtask ( points): Tree is a star

  • Subtask ( points): No additional constraints.