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 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef struct directedGraph{
  int *vertex;
  int *next;
  int *start;
  int v,e;
  int pointer;
} graph;

graph* newGraph(const int v,const int e){
  graph *g=(graph *)malloc(sizeof(graph));
  g->vertex=(int *)calloc(e,sizeof(int));
  g->next=(int *)calloc(e,sizeof(int));
  g->start=(int *)calloc(v,sizeof(int));
  for(int i=0;i<v;i++) g->start[i]=-1;
  g->v=v;
  g->e=e;
  g->pointer=0;
  return g;
}

void freeGraph(graph *g){
  free(g->vertex);
  free(g->next);
  free(g->start);
  free(g);
  return;
}

void addEdge(graph *g,const int from,const int to){
  const int p=g->pointer;
  g->vertex[p]=to;
  g->next[p]=g->start[from];
  g->start[from]=p;
  g->pointer++;
  return;
}

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

void run(void){
  int n;
  scanf("%d",&n);
  int *a=(int *)calloc(n,sizeof(int));
  int i;
  for(i=0;i<n;i++) scanf("%d",a+i);
  graph *g=newGraph(n,2*n);
  for(i=1;i<n;i++){
    int v,u;
    scanf("%d%d",&v,&u);
    v--;u--;
    addEdge(g,v,u);
    addEdge(g,u,v);
  }
  int *q=(int *)calloc(n,sizeof(int));
  int *parent=(int *)calloc(n,sizeof(int));
  int front=0,last=0;
  q[last++]=0;
  while(last<n){
    int v=q[front++];
    for(int p=g->start[v];p!=-1;p=g->next[p]){
      int u=g->vertex[p];
      if(u==parent[v]) continue;
      parent[u]=v;
      q[last++]=u;
    }
  }
  int *dp=(int *)calloc(n,sizeof(int));
  for(i=n-1;i>=0;i--){
    int v=q[i];
    int max=0;
    int64 sum=0;
    int d=0;
    for(int p=g->start[v];p!=-1;p=g->next[p]){
      int u=g->vertex[p];
      if(u==parent[v]) continue;
      d++;
      sum+=dp[u];
      max=MAX(dp[u],max);
    }
    if(d==0){
      dp[v]=a[v];
      continue;
    }
    if(d==1){
      if(sum!=a[v]){
	exit(1);
	puts("NO");
	return;
      }
      dp[v]=sum;
      continue;
    }
    if(2*max-sum>0){
      int k=2*max-sum;
      if(!(k+(sum-k)/2<=a[v] && a[v]<=sum)){
	puts("NO");
	return;
      }
      dp[v]=a[v]-(sum-k)/2;
    } else {
      if(!((sum+1)/2<=a[v] && a[v]<=sum)){
	puts("NO");
	return;
      }
      dp[v]=a[v]-sum/2;
    }
  }
  int d=0;
  for(i=1;i<n;i++) if(parent[i]==0) d++;
  if((d==1 && dp[0]==a[0]) || dp[0]==0){
    puts("YES");
  } else {
    puts("NO");
  }
}

int main(void){
  run();
  return 0;
}