stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE

#define debug(x) cout << #x << ": " << x << endl

#else

#define debug(x)

#endif

using namespace std;

typedef long long ll;

const int MAXN=2e5+7;

const int INF=0x3f3f3f3f;

const int MOD=1e9+7;

int arr[MAXN];



int n,a,b;

bool check(ll x)

{

    ll lazy=b*x;

    ll need=0;

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

    {

        if(arr[i]<=lazy) continue;

        else

        {

            need+=(arr[i]-lazy)/(a-b);

            if((arr[i]-lazy)%(a-b)) need++;

        }

    }

    return need<=x;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    cin>>n>>a>>b;

    int mx=0;

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

        cin>>arr[i];

    int ans=INF;

    int l=1,r=1e9/b+1;

    while(l<=r)

    {

        int mid=l+r>>1;

        if(check(mid))

        {

            ans=min(ans,mid);

            r=mid-1;

        }

        else

            l=mid+1;

    }

    cout<<ans<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define LL long long

LL n,A,B;

LL a[1000005];

bool judge(LL x){

    LL temp=x*B;

    LL sub=A-B;

    LL sum=0;

    for(LL i=n;i>=1;i--){

        if(a[i]>temp){

            if((a[i]-temp)%sub)

                sum+=(a[i]-temp)/sub+1;

            else

                sum+=(a[i]-temp)/sub;

        }

    }

    if(sum<=x)

        return true;

    return false;

}

int main() {

    scanf("%lld%lld%lld",&n,&A,&B);

    LL right=0;

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

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

        right+=a[i]/B+1;

    }

    sort(a+1,a+1+n);

    LL left=1;

    while(left<right){

        LL mid=(left+right)>>1;

        if(judge(mid))

            right=mid;

        else

            left=mid+1;

    }

    printf("%lld\n",left);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

const int maxn = 1e5 + 6;

using ll = long long;

ll n, a, b;

array<ll, maxn> health;

ll l, r;



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    auto check = ([&](auto mid) -> bool {

        ll tot = 0;

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

            ll subDamage = mid * b;

            if (health[i] - subDamage > 0) {

                tot += ceil((double) (health[i] - subDamage) / (a - b)) + 0.1;

            }

        }

        return tot <= mid;

    });

    cin >> n >> a >> b;

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

    l = 1, r = static_cast<ll>(1e9);

    while (l <= r) {

        ll mid = (l + r) >> 1;

        if (check(mid)) {

            r = mid - 1;

        } else {

            l = mid + 1;

        }

    }

    cout << l << "\n";

#ifndef ONLINE_JUDGE

    cout.flush();

#endif

}

// Optimized solution to the same problem (faster):


#include<math.h>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



const int maxn = 100009;

long long n, A, B, h[maxn];

long long l = 0, r = 9e18, mid;



bool check(long long x)

{

    long long res = 0;

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

        if (h[i] > B*x)

            res+= (int)ceil(h[i]-B*x+A-B-1)/(A-B);

    return res <= x;

}

int main()

{

    scanf("%lld%lld%lld", &n, &A, &B);

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

    while (l < r)

    {

        mid = (l+r) >> 1;

        if (check(mid)) r = mid;

        else l = mid + 1;

    }

    printf("%lld\n", l);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cmath>

#include<queue>

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#define int long long

using namespace std;



int a,b,n,c[100010],tmp[100010];



int check(int u)

{

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

	{

		tmp[i]=c[i]-u*b;

	}

	int res=0;

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

	{

		if(tmp[i]>0)

		{

			res+=(tmp[i]+a-1)/a;

		}

	}

	return res<=u;

}



signed main()

{

	scanf("%lld%lld%lld",&n,&a,&b);

	a-=b;

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

	{

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

	}

	int l=1,r=1e9+1,mid;

	while(l<r)

	{

	    mid=(l+r)>>1;

		if(check(mid))

		{

			r=mid;

		}

		else

		{

			l=mid+1;

		}

	}

	printf("%lld\n",l);

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#define maxn 100005

#define inf 0x3f3f3f3f

using namespace std;

typedef long int ll;

ll a[maxn];

int main()

{

    int n, A, B;

    scanf("%d %d %d", &n, &A, &B);

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

    int C = A - B, l = 0, r = 1e9, ans;

    while (l <= r){

        int m = (l + r) / 2, cnt = 0;

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

            if ((a[i] - (ll)m * B) <= 0) continue;

            if ((a[i] - (ll)m * B) % C == 0) cnt += (a[i] - (ll)m * B) / C;

            else cnt += (a[i] - (ll)m * B) / C + 1;

            if (cnt > m) break;

        }

        if (cnt > m) l = m + 1;

        else r = m - 1, ans = m;

    }

    printf("%d\n", ans);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef unsigned long long ull;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<double, double> pdd;

const ull mod = 1000000000 + 7;

#define REP(i,n) for(int i=0;i<(int)n;++i)

ll N, A, B;

vector<ll> h(100005);



bool check(ll num){

    ll a = 0;

    REP(i, N){

        ll b = h[i] - B*num;

        if(b>0){

            a += ((b+A-B-1)/(A-B));

        }

    }

    //cout << num << " " << a << " " << (a<=num) <<endl;

    return (a<=num);

}



ll binarySearch(ll start, ll end){

    if(start+1==end) return end;

    ll mid = (start+end)/2;

    if(check(mid)){

        return binarySearch(start, mid);

    }else{

        return binarySearch(mid, end);

    }

}



int main(){

    cin >> N >> A >> B;

    REP(i, N) cin >> h[i];

    cout << binarySearch(-1, 10000000005) << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef unsigned long long ull;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef pair<double, double> pdd;

const ull mod = 1e9 + 7;

#define REP(i,n) for(int i=0;i<(int)n;++i)



//debug

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;



template < typename T >

void vprint(T &v){

	REP(i, v.size()){

		cout << v[i] << " ";

	}

	cout << endl;

}



int main(){

	ll N, A, B;

	cin >> N >> A >> B;

	vector<ll> h(N);

	REP(i, N) cin >> h[i];



	ll l = 0;

	ll u = 1e10;

	while(u-l>1){

		ll m = (l+u)/2;

		ll tmp = 0;

		REP(i, N){

			ll resid = h[i] - B*m;

			if(resid>0) tmp += ((resid+A-B-1)/(A-B));

		}

		if(tmp<=m) u = m;

		else l = m;

	}

	cout << u << endl;

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstdlib>

#include <cstdio>

using namespace std;

inline int read()

{

	int f = 1, x = 0;

	char ch;



	do{

		ch = getchar();

		if (ch == '-')

			f = -1;

	}while(ch < '0' || ch > '9');

	do{

		x = x * 10 + ch - '0';

		ch = getchar();

	}while(ch >= '0' && ch <= '9');

	return f * x;

}

const int N = 1e5;



int n;

int val[N + 1];

struct Edge {

	int to, next;

} edge[(N << 1) + 1];

int start[N + 1], tot;

int d[N + 1], up[N + 1];



inline void addedge(int u, int v)

{

	edge[++tot] = Edge{ v, start[u] };

	start[u] = tot;

	edge[++tot] = Edge{ u, start[v] };

	start[v] = tot;

	d[u]++;

	d[v]++;

	return;

}

inline bool dfs(int u, int fa)

{

	if (d[u] == 1) {

		up[u] = val[u];

		return false;

	}

	int sum = 0, maxx = 0;

	for (int i = start[u]; i; i = edge[i].next) {

		int v = edge[i].to;

		if (v != fa) {

			if (dfs(v, u))

				return true;

			sum += up[v];

			maxx = max(maxx, up[v]);

		}

	}

	up[u] = (val[u] << 1) - sum;

	if (up[u] < 0 || up[u] > val[u] || val[u] < maxx) {

		printf("NO\n");

		return true;

	}

	return false;

}

int main()

{

	n = read();

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

		val[i] = read();

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

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

		addedge(u, v);

	}

	if (n == 2) {

		if (val[1] == val[2])

			printf("YES\n");

		else

			printf("NO\n");

		return 0;

	}



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

		if (d[i] > 1) {

			if (!dfs(i, 0)) {

				if (up[i])

					printf("NO\n");

				else

					printf("YES\n");

			}

			break;

		}

	}



	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
    #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;  

    }  

// Optimized solution to the same problem (faster):
// 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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//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 ;

}


// Optimized solution to the same problem (faster):
//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 ;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem: