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 F first
#define S second
#define pi M_PI
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

ll a[111111];
vector<int> v[111111];
bool f=1;

P dfs(int x,int p) {
  vector<P> g;
  bool ff=0;
  rep(i,v[x].size()) {
    if(v[x][i]!=p) {
      g.pb(dfs(v[x][i],x));
      ff=1;
    }
  }
  priority_queue<PP> que;
  ll xx=0,yy=0;
  rep(i,g.size()) {
    if(g[i].S+g[i].F) que.push(PP(1,P(g[i].S+g[i].F,i)));
    xx+=g[i].F;
    yy+=g[i].S;
  }
  while(que.size()>1) {
    PP p1=que.top();que.pop();
    PP p2=que.top();que.pop();
    if(!p1.F) break;
    ll z=max(0LL,min(xx+yy-a[x],min(p1.S.F,p2.S.F)));
    if(!z) break;
    p1.S.F-=z;
    p2.S.F-=z;
    yy-=z*2;
    a[x]-=z;
    g[p1.S.S].S-=z;
    if(p2.F) g[p2.S.S].S-=z;
    else g[p2.S.S].F-=z;
    if(p1.S.F) que.push(p1);
    if(p2.S.F) que.push(p2);
  }
  P q=P(0,0);
  rep(i,g.size()) q.F+=g[i].F+g[i].S;
  q.F=min(q.F,a[x]);
  q.S=a[x]-q.F;
  if(ff&&q.S) f=0;
  return q;
}
void Main() {
  int n;
  R n;
  rep(i,n) R a[i];
  rep(i,n-1) {
    int x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(0,-1);
  if(!f) pr("NO");
  else pr("YES");
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}