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: Runtime Error)

#include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;

int N;
LL A[114514];
vector<int> es[114514];

pair<LL, LL> dfs(int v, int p) {
  int num_ch = 0;
  LL l = 0;
  LL r = 0;
  LL s = 0;
  LL ls = 0;
  LL maxa = -1;
  LL maxb = -1;
  for (int u : es[v]) {
    if (p == u) continue;
    LL x, y;
    tie(x, y) = dfs(u, v);
    s += y;
    ls += x;
    maxa = max(maxa, x);
    maxb = max(maxb, y);
    LL nr = y+r;
    LL nl = max(0LL, max(x, l) - min(y, r));
    l = nl;
    r = nr;
    num_ch++;
  }

  if (ls/2 + ls%2 > A[v]) {
    puts("NO");
    exit(0);
  }

  LL mp = 0;
  if (2*maxa > s) mp = s-maxa;

  LL mp2 = s/2;
  if (2*maxb > s) mp2 = s - maxb;
  mp2 = A[v] - mp2;

  if (num_ch == 0) return make_pair(A[v], A[v]);
  if (num_ch == 1) {
    if (l <= A[v] && A[v] <= r) return make_pair(A[v], A[v]);
    puts("NO");
    exit(0);
  }
  return make_pair(max(l, max(mp2, mp)), min(r, A[v]));
}

int main() {
  scanf("%d", &N);
  rep(i, N) {
    scanf("%lld", &A[i]);
  }

  rep(i, N-1) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    es[u].eb(v);
    es[v].eb(u);
  }
  
  int cnt = 0;
  rep(v, N) {
    if (es[v].size() > 1) {
      cnt++;
      if (dfs(v, v).first != 0) {
        puts("NO");
        exit(0);
      }

      if (cnt > 3) {
        puts("YES");
        exit(0);
      }
    }
  }

  assert(N == 2);
  if (A[0] == A[1]) puts("YES");
  else puts("NO");
}