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: Accepted)


#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip> 
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (4) << f << endl; Prints x.xxxx


using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >

vector< vl > adj(123456);
ll degree[123456]={0};
ll ston[123456];
//std::ios::sync_with_stdio(false);   
int dfs(int cur,int prev){
	ll i,sumi=0,maxi=0;
	if(adj[cur].size()==1)
		return 0;
	rep(i,adj[cur].size()){
		if(adj[cur][i]!=prev){
			dfs(adj[cur][i],cur);
		}
	}
	rep(i,adj[cur].size()){
		if(adj[cur][i]!=prev){
			sumi+=ston[adj[cur][i]];
			maxi=max(maxi,ston[adj[cur][i]]);
		}

	}
	ll x2=sumi-ston[cur];
	ll x1=2*ston[cur] -sumi;
	if(x1<0 || x2<0 || x2>sumi-maxi)
	{
		//cout<<x1<<" "<<maxi<<endl;
		cout<<"NO";
		exit(0);
	}

	ston[cur]=x1;
	if(prev==-1){
		if(ston[cur]){
			cout<<"NO";
		}
		else
			cout<<"YES";
	}
	return 0;
}
int main(){
	std::ios::sync_with_stdio(false);
	ll n,i,u,v;
	cin>>n;
	//int i;
	rep(i,n){
		cin>>ston[i];
	}
	if(n==2){
		if(ston[0]==ston[1])
			cout<<"YES";
		else
			cout<<"NO";
		return 0;
	}
	rep(i,n-1){
		cin>>u>>v;
		u--;
		v--;
		adj[u].pb(v);
		adj[v].pb(u);
		degree[u]++;
		degree[v]++;
	}
	rep(i,n){
		if(degree[i]!=1){
			break;
		}
	}
	//cout<<i<<endl;
	dfs(i,-1);
	//cout<<"YES"<<endl;
}