Problem:
Score : 700 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Currently, there are A_i stones placed on vertex i.
Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:

Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices.
Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.

Note that the operation cannot be performed if there is a vertex with no stone on the path.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i,b_i ≦ N

0 ≦ A_i ≦ 10^9

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.

Sample Input 1

5
1 2 1 1 2
2 4
5 2
3 2
1 3

Sample Output 1

YES

All the stones can be removed, as follows:

Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.

Select vertices 1 and 5. Then, there is no stone on any vertex.

Sample Input 2

3
1 2 1
1 2
2 3

Sample Output 2

NO

Sample Input 3

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

Sample Output 3

YES

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskC {
		boolean ok;

		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			List<Integer>[] adj = new List[n];
			for (int i = 0; i < n; i++) {
				adj[i] = new ArrayList<>();
			}
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}
			for (int i = 0; i < n - 1; i++) {
				int u = in.nextInt() - 1;
				int v = in.nextInt() - 1;
				adj[u].add(v);
				adj[v].add(u);
			}
			if (n == 2) {
				out.println(a[0] == a[1] ? "YES" : "NO");
				return;
			}
			int root = -1;
			for (int i = 0; i < n; i++) {
				if (adj[i].size() > 1) {
					root = i;
					break;
				}
			}
			if (root < 0) {
				throw new AssertionError();
			}
			ok = true;
			// System.out.println("root = " + root);
			long excess = dfs(root, -1, adj, a);
			out.println(ok && excess == 0 ? "YES" : "NO");
		}

		private long dfs(int v, int parent, List<Integer>[] adj, int[] a) {
			List<Long> e = new ArrayList<>();
			long sum = 0;
			for (int u : adj[v]) {
				if (u == parent) {
					continue;
				}
				long cur = dfs(u, v, adj, a);
				e.add(cur);
				sum += cur;
			}
			if (e.size() == 0) {
				return a[v];
			}
			long p = sum - a[v]; // pairs
			long s = a[v] - p; // singles
			if (p < 0 || s < 0) {
				ok = false;
				return 0;
			}
			if (p > maxPairs(e)) {
				ok = false;
				return 0;
			}
			return s;
		}

		private long maxPairs(List<Long> a) {
			long free = 0;
			long pairs = 0;
			for (long x : a) {
				long s = Math.min(x, free);
				x -= s;
				free -= s;
				if (x % 2 != 0) {
					++free;
					--x;
				}
				long d = Math.min(x, 2 * pairs);
				x -= d;

				pairs += s;
				pairs += d / 2;
				free += x;
			}
			return pairs;
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

	}
}