C. Almost Tree Cut

Time limit: 1s
Memory limit: 256MB
Input:
Output:

You are given a connected undirected graph with nn nodes and nn edges. Each node ii has an associated cost aia_i.

A cut of the given graph is obtained by removing some edges in order to split the graph into exactly 22 disjoint connected subgraphs.

If the sum of the costs of the nodes from the first subgraph is s1s_1 and the sum of the costs of the nodes from the second subgraph is s2s_2, then the cost of the cut is equal to s1s2|s_1-s_2|, where x|x| denotes the absolute value of xx.

What is the smallest possible cost of a cut of the given graph?

Input

The first line of input contains a single integer nn (3n21053 \le n \le 2 \cdot 10^5) — the number of nodes and edges of the given graph.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ai1091 \le a_i \le 10^9) — the associated costs of the nn nodes.

The following nn lines contain the edges of the graph. Each line contains two integers, uu and vv (1u,vn,uv1 \le u,v \le n, u \neq v) — the endpoints of an edge.

It is guaranteed that there are no multiple edges and that the given graph is connected.

Output

Output a single integer, the minimum possible cost of a cut.

Example 1

stdin

3
1 7 3
1 2
2 3
3 1

stdout

3

Note

If we delete the edges (1,2)(1,2) and (2,3)(2,3), the resulting subgraphs will be {1,3}\{1,3\} and {2}\{2\}. The cost of this cut is equal to a1+a3a2=47=3|a_1+a_3-a_2|=|4-7|=3.

Example 2

stdin

5
1 7 3 3 6
3 5
5 4
1 4
4 2
2 5

stdout

2

Example 3

stdin

8
5 7 9 12 13 19 7 16
1 2
2 3
3 1
1 4
4 5
4 6
3 7
7 8

stdout

0

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