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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define F first
#define S second
#define pb push_back

typedef long long ll;
typedef pair <int, int> pii;
typedef long double ld;
typedef vector <ll> vi;

const int N = 2e6 + 1;
const ll INF = 1e18 + 9;
const int B = 1e9 + 7;

int n;
int a[N], d[N];
vi g[N];
int c[N];

int dfs(int x, int p = -1) {
    if (g[x].size() == 1) {
        c[x] = 1;
        return a[x];
    }
    int s = 0;
    for (auto to : g[x])
        if (to != p) {
            s += dfs(to, x);
            c[x] += c[to];
        }
    if (c[x] == 1) {
        if (s != a[x]) {
            cout << "NO\n";
            exit(0);
        }
    }
    else {
        if (s < a[x] || a[x] < s - a[x] || s < 2 * (s - a[x])) {
            cout << "NO\n";
            exit(0);
        }
    }
    return a[x] - (s - a[x]);
}

int main() {
    #ifdef black
    freopen("in", "r", stdin);
    #endif // black
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    forn(i, 1, n)
        cin >> a[i];
    if (n == 2) {
        cout << (a[1] == a[2] ? "YES\n" : "NO\n");
        return 0;
    }
    forn(i, 1, n - 1) {
        int f, t;
        cin >> f >> t;
        g[f].pb(t);
        g[t].pb(f);
    }
    queue <int> q;
    forn(i, 1, n)
        if (g[i].size() == 1) {
            d[i] = 1;
            q.push(i);
        }
    while (q.size()) {
        int x = q.front();
        q.pop();
        for (auto to : g[x]) {
            if (!d[to]) {
                d[to] = d[x] + 1;
                q.push(to);
            }
        }
    }
    int r = 1;
    forn(i, 2, n)
        if (d[r] < d[i]) r = i;
    cout << (dfs(r) == 0 ? "YES\n" : "NO\n");

    return 0;
}