stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
    #include<bits/stdc++.h>  

    using namespace std;  

    typedef long long LL;  

    const int maxn=1e5+10;  

    int n,d[maxn];  

    LL a[maxn];  

    vector<int> G[maxn];  

    bool flag=1;  

    void dfs(int u,int fu)  

    {  

        LL sum=0,maxs=0;  

        int o=0;  

        for (int i=0;i<G[u].size();i++)  

        {  

            int v=G[u][i];  

            if (v==fu) continue;  

            o++;  

            dfs(v,u);  

            sum+=a[v];  

            maxs=max(maxs,a[v]);  

        }  

        if (o==0) return ;  

        LL x=sum-a[u],y=2*a[u]-sum;  

        if (x<0||y<0||x>min(sum-maxs,sum/2)||(fu==-1&&y)) flag=0;  

        a[u]=y;  

    }  

    int main()  

    {  

        cin>>n;  

        for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);  

        for (int i=1;i<n;i++)  

        {  

            int u,v;scanf("%d%d",&u,&v);  

            G[u].push_back(v);G[v].push_back(u);  

            d[u]++;d[v]++;  

        }  

        int root=0;  

        for (int u=1;u<=n;u++) if (d[u]>1) {root=u;break;}  

        if (root) dfs(root,-1);  

        else flag=(a[1]==a[2]);  

        if (flag) cout<<"YES";  

        else cout<<"NO";  

        return 0;  

    }  

// Faster C++ solution for the same problem (measured speedup: 3.604418978001645x):
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<vector>

using namespace std;

#define MAX 100100

#define ll long long

inline int read()

{

	int x=0;bool t=false;char ch=getchar();

	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();

	if(ch=='-')t=true,ch=getchar();

	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();

	return t?-x:x;

}

void WA(){puts("NO");exit(0);}

struct Line{int v,next;}e[MAX<<1];

int h[MAX],cnt=1,dg[MAX];

inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;++dg[u];}

int n,a[MAX],rt;

void dfs(int u,int ff)

{

	int mx=0;ll sum=0;

	for(int i=h[u];i;i=e[i].next)

	{

		int v=e[i].v;if(v==ff)continue;

		dfs(v,u);mx=max(mx,a[v]);sum+=a[v];

	}

	if(dg[u]==1)return;

	ll mn=min(sum/2,sum-mx);

	if(a[u]>sum)WA();

	else

	{

		if(sum-a[u]>mn)WA();

		else a[u]-=sum-a[u];

	}

}

int main()

{

	n=read();

	for(int i=1;i<=n;++i)a[i]=read();

	if(n==2){puts(a[1]==a[2]?"YES":"NO");return 0;}

	for(int i=1;i<n;++i)

	{

		int u=read(),v=read();

		Add(u,v);Add(v,u);

	}

	for(int i=1;i<=n;++i)

		if(dg[i]>1){dfs(rt=i,0);break;}

	if(a[rt])WA();

	puts("YES");return 0;

}
// Slower C++ solution:
    #include<bits/stdc++.h>  

    using namespace std;  

    typedef long long LL;  

    const int maxn=1e5+10;  

    int n,d[maxn];  

    LL a[maxn];  

    vector<int> G[maxn];  

    bool flag=1;  

    void dfs(int u,int fu)  

    {  

        LL sum=0,maxs=0;  

        int o=0;  

        for (int i=0;i<G[u].size();i++)  

        {  

            int v=G[u][i];  

            if (v==fu) continue;  

            o++;  

            dfs(v,u);  

            sum+=a[v];  

            maxs=max(maxs,a[v]);  

        }  

        if (o==0) return ;  

        LL x=sum-a[u],y=2*a[u]-sum;  

        if (x<0||y<0||x>min(sum-maxs,sum/2)||(fu==-1&&y)) flag=0;  

        a[u]=y;  

    }  

    int main()  

    {  

        cin>>n;  

        for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);  

        for (int i=1;i<n;i++)  

        {  

            int u,v;scanf("%d%d",&u,&v);  

            G[u].push_back(v);G[v].push_back(u);  

            d[u]++;d[v]++;  

        }  

        int root=0;  

        for (int u=1;u<=n;u++) if (d[u]>1) {root=u;break;}  

        if (root) dfs(root,-1);  

        else flag=(a[1]==a[2]);  

        if (flag) cout<<"YES";  

        else cout<<"NO";  

        return 0;  

    }  

// Faster C++ solution for the same problem (measured speedup: 3.5912549615138216x):
#include<bits/stdc++.h>

using namespace std;

int hea[100010],nxt[200010],to[200010],d[100010],tot;

long long v[100010];

void fason(int x,int y)

{

	nxt[++tot]=hea[x];

	hea[x]=tot;

	to[tot]=y;

}

long long dfs(int x,int f,int fa)

{

	if(d[x]==1)return v[x];

	long long s=0;

	for(int i=hea[x];i;i=nxt[i])

	{

		int y=to[i];

		if(y==fa)continue;

		long long t=dfs(y,0,x);

		if(t==-1||t>v[x])return -1;

		s+=t;

	}

	if(f==1)return s==v[x]*2?1:-1;

	if(s>2*v[x]||v[x]-s>0)return -1;

	else return 2*v[x]-s;

}

int main()

{

	int n,x,y;

	scanf("%d",&n);

	for(int i=1;i<=n;i++)scanf("%lld",v+i);

	for(int i=1;i<n;i++)

	{

		scanf("%d%d",&x,&y);

		fason(x,y);

		fason(y,x);

		d[x]++;

		d[y]++;

	}

	if(n==2)

	{

		puts(v[1]==v[2]?"YES":"NO");

		return 0;

	}

	int rt;

	for(int i=1;i<=n;i++)

		if(d[i]>1)

		{

			rt=i;

			break;

		}

	if(dfs(rt,1,0)==1)puts("YES");

	else puts("NO");

	return 0;

}
// Slower C++ solution:
    #include<bits/stdc++.h>  

    using namespace std;  

    typedef long long LL;  

    const int maxn=1e5+10;  

    int n,d[maxn];  

    LL a[maxn];  

    vector<int> G[maxn];  

    bool flag=1;  

    void dfs(int u,int fu)  

    {  

        LL sum=0,maxs=0;  

        int o=0;  

        for (int i=0;i<G[u].size();i++)  

        {  

            int v=G[u][i];  

            if (v==fu) continue;  

            o++;  

            dfs(v,u);  

            sum+=a[v];  

            maxs=max(maxs,a[v]);  

        }  

        if (o==0) return ;  

        LL x=sum-a[u],y=2*a[u]-sum;  

        if (x<0||y<0||x>min(sum-maxs,sum/2)||(fu==-1&&y)) flag=0;  

        a[u]=y;  

    }  

    int main()  

    {  

        cin>>n;  

        for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);  

        for (int i=1;i<n;i++)  

        {  

            int u,v;scanf("%d%d",&u,&v);  

            G[u].push_back(v);G[v].push_back(u);  

            d[u]++;d[v]++;  

        }  

        int root=0;  

        for (int u=1;u<=n;u++) if (d[u]>1) {root=u;break;}  

        if (root) dfs(root,-1);  

        else flag=(a[1]==a[2]);  

        if (flag) cout<<"YES";  

        else cout<<"NO";  

        return 0;  

    }  

// Faster C++ solution for the same problem (measured speedup: 3.606410746803874x):
// luogu-judger-enable-o2

#include<bits/stdc++.h>

#define fo(i,l,r) for(int i=l;i<=r;i++)

#define of(i,l,r) for(int i=l;i>=r;i--)

#define fe(i,u) for(int i=head[u];i;i=e[i].next)

#define el putchar('\n')

#define ta putchar('	')

using namespace std;

typedef long long ll;

typedef pair<int,int> pii;

inline void open(const char *s)

{

	#ifndef ONLINE_JUDGE

	char str[20];

	sprintf(str,"%s.in",s);

	freopen(str,"r",stdin);

//	sprintf(str,"%s.out",s);

//	freopen(str,"w",stdout);

	#endif

}

inline int rd()

{

	static int x,f;

	x=0;f=1;

	char ch=getchar();

	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;

	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';

	return f>0?x:-x;

}

const int N=100010;

int n,d[N],head[N],tot=0;

ll a[N];

struct edge{int v,next;}e[N<<1];



inline void add(int u,int v){e[++tot]=edge{v,head[u]};head[u]=tot;++d[u];}



ll dfs(int u,int fat)

{

	if(d[u]==1)return a[u];

	ll sum=0,mx=0;

	fe(i,u){

		int v=e[i].v;if(v==fat)continue;

		ll hh=dfs(v,u);

		if(hh==-1)return -1;

		sum+=hh;mx=max(mx,hh);

	}

	if(sum<a[u])return -1;

	if(mx<sum-mx&&sum-a[u]>sum/2)return -1;

	if(mx>sum-mx&&sum-a[u]>sum-mx)return -1;

	return (ll)a[u]-(sum-a[u]);

}



int main()

{

	n=rd();fo(i,1,n)a[i]=rd();

	fo(i,2,n){

		int x=rd(),y=rd();

		add(x,y);add(y,x);

	}

	if(n==2)return puts(a[1]^a[2]?"NO":"YES"),0;

	int rt=0;

	fo(i,1,n)if(d[i]>1){rt=i;break;}

	puts(dfs(rt,0)==0?"YES":"NO");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define int long long

#define ll long long

#define pb push_back

#define mp make_pair

#define fi first

#define se second

using namespace std;



const int N = 1e5+6;

int n, ar[N]; bool ok = true;

vector <int> adj[N];



int dfs(int v, int p){

  if (p != -1 && adj[v].size() == 1) return ar[v];

  int sum = 0, mx = 0;

  for (auto u : adj[v]){

    if (u == p) continue;

    int tmp = dfs(u, v);

    sum += tmp;

    mx = max(mx, tmp);

  }

  if (sum > ar[v]*2 || ar[v] > sum || mx > ar[v]) ok = false;

  return ar[v]*2 - sum;

}



signed main(){

	cin >> n;

	for (int i=1; i<=n; i++) cin >> ar[i];

	if (n == 2){

		if (ar[1] == ar[2]) cout << "YES"; else cout << "NO";

		return 0;

	}

	for (int i=1; i<n; i++){

		int u, v; cin >> u >> v;

		adj[u].pb(v);

		adj[v].pb(u);

	}

	int d;

	bool ans = false;

	for (int i=1; i<=n; i++){

		if (adj[i].size() >= 2){

			d = i;

			break;

		} 

	}

	ans = (dfs(d, -1) == 0 && ok);

	if (ans) cout << "YES"; else cout << "NO";

}

// Faster C++ solution for the same problem (measured speedup: 18.56425016126082x):
#include<cstdio>

const int N=100005;

int n,v[N],he[N],to[N*2],nx[N*2],d[N],fa[N];

inline void wk(int w,int x,int y)

{to[w]=y,nx[w]=he[x],he[x]=w;}

inline int imx(int x,int y){return x>y?x:y;}

int dfs(int x)

{

	int a=0,b=0,c=0;

	for(int i=he[x],y;i;i=nx[i])if((y=to[i])!=fa[x])

	{c=1,fa[y]=x;if(!dfs(y))return 0;a+=v[y],b=imx(b,v[y]);}

	if(!c)return 1;

	if(v[x]>a||v[x]<a-v[x]||a-v[x]>a-b)return 0;//

	v[x]-=a-v[x];return 1;

}

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;++i)

		scanf("%d",&v[i]);

	for(int i=1,x,y;i<n;++i)

		scanf("%d%d",&x,&y),++d[x],++d[y],

		wk(i*2,x,y),wk(i*2+1,y,x);

	if(n==2){v[1]==v[2]?printf("YES"):printf("NO");return 0;}

	for(int i=1;i<=n;++i)if(d[i]==1)

	{dfs(i)?printf("YES"):printf("NO");return 0;}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define int long long

#define ll long long

#define pb push_back

#define mp make_pair

#define fi first

#define se second

using namespace std;



const int N = 1e5+6;

int n, ar[N]; bool ok = true;

vector <int> adj[N];



int dfs(int v, int p){

  if (p != -1 && adj[v].size() == 1) return ar[v];

  int sum = 0, mx = 0;

  for (auto u : adj[v]){

    if (u == p) continue;

    int tmp = dfs(u, v);

    sum += tmp;

    mx = max(mx, tmp);

  }

  if (sum > ar[v]*2 || ar[v] > sum || mx > ar[v]) ok = false;

  return ar[v]*2 - sum;

}



signed main(){

	cin >> n;

	for (int i=1; i<=n; i++) cin >> ar[i];

	if (n == 2){

		if (ar[1] == ar[2]) cout << "YES"; else cout << "NO";

		return 0;

	}

	for (int i=1; i<n; i++){

		int u, v; cin >> u >> v;

		adj[u].pb(v);

		adj[v].pb(u);

	}

	int d;

	bool ans = false;

	for (int i=1; i<=n; i++){

		if (adj[i].size() >= 2){

			d = i;

			break;

		} 

	}

	ans = (dfs(d, -1) == 0 && ok);

	if (ans) cout << "YES"; else cout << "NO";

}

// Faster C++ solution for the same problem (measured speedup: 3.585573323521806x):
#include <bits/stdc++.h>

#define il inline

#define int long long

const int N=1e5+5;



int n,a[N],du[N];

int to[N+N],nx[N+N],hd[N],sze;



il void add(int u,int v){to[++sze]=v,nx[sze]=hd[u],hd[u]=sze,du[v]++;}



il bool dfs(int u,int F)

{

	int i,v,x=0,y=0,o=0;

	for (i=hd[u]; i; i=nx[i]) if ((v=to[i])!=F)

	{

		if (!dfs(v,u)) return 0;

		o=1,x+=a[v],y=std::max(y,a[v]);

	}

	if (!o) return 1;

	if (a[u]>x||a[u]<x-a[u]||a[u]<y) return 0;

	return a[u]-=x-a[u],1;

}



signed main()

{

	scanf("%lld",&n); int i,u,v;

	for (i=1; i<=n; i++) scanf("%lld",a+i);

	for (i=1; i<n; i++) scanf("%lld%lld",&u,&v),add(u,v),add(v,u);

	if (n==2) return puts(a[1]==a[2]?"YES":"NO"),0;

	

	for (i=1; i<=n; i++) if (du[i]==1) return puts(dfs(i,0)?"YES":"NO"),0;

	

	return 0;

}
// Slower C++ solution:
//Created Time:2019年12月19日 星期四 21时02分54秒

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <vector>

#define N 100005



using namespace std;



int n;

int val[N];



vector < int > G[N], s[N];



void dfs(int ,int);



int main(){

	scanf("%d", &n);

	for(int i = 1; i <= n; ++i) scanf("%d", val + i);

	for(int i = 1; i < n; ++i){

		int u, v; scanf("%d%d", &u, &v);

		G[u].push_back(v); G[v].push_back(u);

	}

	if(n == 2){

		if(val[1] != val[2]) puts("NO");

		else puts("YES");

		return 0;

	}

	for(int i = 1; i <= n; ++i){

		if(G[i].size() == 1) continue;

		dfs(i, 0);

		if(val[i]) puts("NO");

		else puts("YES");

		return 0;

	}

	return 0;

}



void dfs(int u, int fa){

	int sz = G[u].size(); long long sum = 0;

	if(sz == 1) return ;

	for(int i = 0; i < sz; ++i){

		int v = G[u][i]; if(v == fa) continue;

		dfs(v, u); sum += val[v]; s[u].push_back(val[v]);

	}

	if(sum < val[u] || val[u] * 2 < sum){ puts("NO"); exit(0); }

	long long x = sum - val[u]; sz -= (fa != 0); int pos = sz - 1; val[u] -= x;

	sort(s[u].begin(), s[u].end());

	for(int i = 0; i < sz && x && i < pos; ++i){

		if(x >= s[u][i]){

			if(s[u][pos] >= s[u][i]) s[u][pos] -= s[u][i];

			else s[u][i] -= s[u][pos--], s[u][pos] -= s[u][i];

			x -= s[u][i];

		}

		else{

			if(s[u][pos] > x) s[u][pos] -= x;

			else s[u][pos - 1] -= x - s[u][pos];

			x = 0;

		}

	}

	if(x && pos == sz - 1){puts("NO"); exit(0);}

	return ;

}


// Faster C++ solution for the same problem (measured speedup: 1.816388180704824x):
//Created Time:2019年12月19日 星期四 21时02分54秒

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <vector>

#define N 100005



using namespace std;



int n;

int val[N];



vector < int > G[N];



void dfs(int ,int);



int main(){

	scanf("%d", &n);

	for(int i = 1; i <= n; ++i) scanf("%d", val + i);

	for(int i = 1; i < n; ++i){

		int u, v; scanf("%d%d", &u, &v);

		G[u].push_back(v); G[v].push_back(u);

	}

	if(n == 2){

		if(val[1] != val[2]) puts("NO");

		else puts("YES");

		return 0;

	}

	for(int i = 1; i <= n; ++i){

		if(G[i].size() == 1) continue;

		dfs(i, 0);

		if(val[i]) puts("NO");

		else puts("YES");

		return 0;

	}

	return 0;

}



void dfs(int u, int fa){

	int sz = G[u].size(), mx = 0; long long sum = 0;

	if(sz == 1) return ;

	for(int i = 0; i < sz; ++i){

		int v = G[u][i]; if(v == fa) continue;

		dfs(v, u); sum += val[v];  mx = max(mx, val[v]);

	}

	if(sum < val[u] || val[u] * 2 < sum || mx > val[u]){ puts("NO"); exit(0); }

	long long x = sum - val[u];  val[u] -= x;

	return ;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;

typedef vector<int> vi;

#define TAM 100100



ll A[TAM];

vi G[TAM];



bool ok ( int i, int pi = -1 ) {

  if ( G[i].size() == 1 )

    return true;



  ll sum = 0, maxAj = 0;

  for ( int j : G[i] )

    if ( j != pi ) {

      if ( !ok(j, i) )

        return false;

      sum += A[j];

      maxAj = max ( maxAj, A[j] );

    }



  // x + A[i] = sum

  ll x = sum - A[i];



  if ( x < 0 || maxAj > A[i] )

    return false;



  A[i] -= x;

  return A[i] >= 0;

}



int main() {

  int n;

  scanf ( "%d", &n );

  for ( int i = 1, tmp; i <= n; ++i ) {

    scanf ( "%d", &tmp );

    A[i] = tmp;

  }

  for ( int edges = n-1, a, b; edges--; ) {

    scanf ( "%d%d", &a, &b );

    G[a].pb ( b );

    G[b].pb ( a );

  }



  if ( n == 2 ) {

    puts ( A[1] == A[2] ? "YES" : "NO" );

    return 0;

  }



  int root = 1;

  while ( G[root].size() == 1 ) root++;



  if ( ok(root) && A[root] == 0 ) puts ("YES");

  else puts("NO");

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.570345944573931x):
#include <bits/stdc++.h>

using namespace std;



typedef long long LL;

const int Maxn = 111111;

struct Edge {

	int v, id;

	Edge *nxt;

} pool[Maxn*2], *tree[Maxn], *tail = pool;

inline void addedge(const int &u, const int &v, const int &id) {

	tail->v = v, tail->id = id;

	tail->nxt = tree[u], tree[u] = tail++;

}



int n, a[Maxn], w[Maxn];

bool isleaf[Maxn];



void dfs(int u, int fa, int lastid) {

	w[lastid] = a[u];

	isleaf[u] = true;

	for (Edge *p = tree[u]; p; p = p->nxt) {

		if (p->v != fa) {

			isleaf[u] = false;

			dfs(p->v, u, p->id);

			w[lastid] -= w[p->id];

		}

	}

	if (!isleaf[u]) {

		w[lastid] += a[u];

	}

}

void solve() {

	if (n == 2) {

		if (a[1] == a[2]) {

			puts("YES");

		} else {

			puts("NO");

		}

		return;

	}

	for (int i = 1, cnum; i <= n; ++i) {

		cnum = 0;

		for (Edge *p = tree[i]; p; p = p->nxt) {

			++cnum;

		}

		if (cnum > 1) {

			dfs(i, 0, 0);

			break;

		}

	}

	if (w[0] != 0) {

		puts("NO");

		return;

	}

	for (int i = 1; i < n; ++i) {

		if (w[i] < 0) {

			puts("NO");

			return;

		}

	}

	for (int i = 1; i <= n; ++i) {

		if (!isleaf[i]) {

			LL tot = 0;

			for (Edge *p = tree[i]; p; p = p->nxt) {

				tot += w[p->id];

			}

			for (Edge *p = tree[i]; p; p = p->nxt) {

				if (w[p->id] > tot/2) {

					puts("NO");

					return;

				}

			}

		}

	}

	puts("YES");

}



int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; ++i){

		scanf("%d", &a[i]);

	}

	for (int i = 1, x, y; i < n; ++i) {

		scanf("%d%d", &x, &y);

		addedge(x, y, i), addedge(y, x, i);

	}

	solve();

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;  



const int maxn = 1e5 + 10;



int n;

vector<int> e[maxn];

long long a[maxn];

bool dfs(int x, int fa) {

	if(e[x].size() == 1) return true;

	long long sum = 0, mx = 0;

	for(auto &it : e[x]) {

		int v = it;

		if(v == fa) continue;

		if(!dfs(v, x)) return false;

		sum += a[v];

		mx = max(mx, a[v]);

	}

	mx = min(sum - mx, sum / 2);

	long long tmp = sum - a[x];

	if(tmp < 0 || tmp > mx) return false;

	a[x] -= tmp;

	if(x == fa) return a[x] == 0;

	return true;

}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

	}

	for(int i = 1; i < n; i++) {

		int x, y;

		scanf("%d%d", &x, &y);

		e[x].push_back(y);

		e[y].push_back(x);

	}

	if(n == 2) {

		a[1] == a[2] ? printf("YES\n") : printf("NO\n");

		return 0;

	}

	int pos = -1;

	for(int i = 1; i <= n; i++) {

		if(e[i].size() != 1) {

			pos = i;

			break;

		}

	}

	dfs(pos, pos) ? printf("YES\n") : printf("NO\n");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.5716614380028817x):
#include <bits/stdc++.h>

using namespace std;



typedef long long LL;

const int Maxn = 111111;

struct Edge {

	int v, id;

	Edge *nxt;

} pool[Maxn*2], *tree[Maxn], *tail = pool;

inline void addedge(const int &u, const int &v, const int &id) {

	tail->v = v, tail->id = id;

	tail->nxt = tree[u], tree[u] = tail++;

}



int n, a[Maxn], w[Maxn];

bool isleaf[Maxn];



void dfs(int u, int fa, int lastid) {

	w[lastid] = a[u];

	isleaf[u] = true;

	for (Edge *p = tree[u]; p; p = p->nxt) {

		if (p->v != fa) {

			isleaf[u] = false;

			dfs(p->v, u, p->id);

			w[lastid] -= w[p->id];

		}

	}

	if (!isleaf[u]) {

		w[lastid] += a[u];

	}

}

void solve() {

	if (n == 2) {

		if (a[1] == a[2]) {

			puts("YES");

		} else {

			puts("NO");

		}

		return;

	}

	for (int i = 1, cnum; i <= n; ++i) {

		cnum = 0;

		for (Edge *p = tree[i]; p; p = p->nxt) {

			++cnum;

		}

		if (cnum > 1) {

			dfs(i, 0, 0);

			break;

		}

	}

	if (w[0] != 0) {

		puts("NO");

		return;

	}

	for (int i = 1; i < n; ++i) {

		if (w[i] < 0) {

			puts("NO");

			return;

		}

	}

	for (int i = 1; i <= n; ++i) {

		if (!isleaf[i]) {

			LL tot = 0;

			for (Edge *p = tree[i]; p; p = p->nxt) {

				tot += w[p->id];

			}

			for (Edge *p = tree[i]; p; p = p->nxt) {

				if (w[p->id] > tot/2) {

					puts("NO");

					return;

				}

			}

		}

	}

	puts("YES");

}



int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; ++i){

		scanf("%d", &a[i]);

	}

	for (int i = 1, x, y; i < n; ++i) {

		scanf("%d%d", &x, &y);

		addedge(x, y, i), addedge(y, x, i);

	}

	solve();

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef pair<int, int> ii;

typedef pair<int, ii> iii;



const int MAX = 2e5+1;



bool ok = true;

vector<vector<int>> g;

vector<ll> a;



ll go(int u, int p) {

    ll sum = 0;

    bool leaf = true;

    vector<ll> d(g[u].size());

    for(int i = 0; i < g[u].size(); i++) {

        int v = g[u][i];

        if(v != p) {

            leaf = false;

            d[i] = go(v, u);

            sum += d[i];

        }

    }

//    cout << u << " - " << sum << endl;

    if(leaf || (p == -1 && g[u].size() == 1)) {

        if(p == -1) ok &= sum == a[u];

        return a[u];

    }

    ll r = 2*a[u] - sum;

//    cout << u << " " << r << " " << sum << endl;

    if(p != -1) ok &= r >= 0;

    if(p != -1) sum += r;

    ok &= sum == 2*a[u];

    ok &= sum % 2 == 0;

    for(int i = 0; i < g[u].size(); i++) {

        ok &= d[i] <= sum/2;

    }

    ok &= r <= sum/2;

    return r;

}



int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    #ifdef LOCAL

        freopen("input.txt", "r", stdin);

        freopen("output.txt", "w", stdout);

    #else

        #define endl '\n'

    #endif



    int n; cin >> n;

    g.assign(n+1, vector<int>());

    a.assign(n+1, 0);



    for(int i = 1; i <= n; i++) {

        cin >> a[i];

    }



    for(int i = 1; i < n; i++) {

        int a, b; cin >> a >> b;

        g[a].push_back(b);

        g[b].push_back(a);

    }



    go(1, -1);



    if(ok) cout << "YES" << endl;

    else cout << "NO" << endl;



}


// Faster C++ solution for the same problem (measured speedup: 5.389003757169492x):
#include <cstdio>

#include <algorithm>

#define int long long

using namespace std;

typedef long long LL;

const int MAXN = 1e5 + 5;

int n;

int head[MAXN] , to[MAXN << 1] , nxt[MAXN << 1] , cnt , ans , du[MAXN << 1];

LL f[MAXN] , a[MAXN];

void add(int u , int v) {nxt[++cnt] = head[u];head[u] = cnt;to[cnt] = v;}

void dfs(int x , int fa) {

	LL _sum = 0 , mx = 0;

	for (int i = head[x]; i; i = nxt[i]) {

		if(to[i] == fa) continue;

		dfs(to[i] , x);

		if(ans) return;

		mx = max(mx , f[to[i]]);

		_sum += f[to[i]];

	}

	if(!_sum) {

		f[x] = a[x];

		return;

	}

	f[x] = 2ll * a[x] - _sum;

	if(f[x] < 0 || f[x] > a[x] || mx - f[x] > _sum - mx || a[x] < mx) {

		printf("NO");

		exit(0);

	}

}

signed main() {

	scanf("%lld" , &n);

	for (int i = 1; i <= n; ++i) scanf("%lld" , &a[i]);

	if (n == 2) return puts(a[1] == a[2] ? "YES" : "NO"), 0;

	int root = 0;

	for (int i = 1; i < n; ++i) {

		int u , v;

		scanf("%lld %lld" , &u , &v);

		du[u] ++;du[v] ++;

		add(u , v);add(v , u);

		if(du[u] > 1) root = u;

		if(du[v] > 1) root = v;

	}

	dfs(root , 0);

	if(f[root]) printf("NO");

	else printf("YES");

	return 0;

}