stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<stdio.h>

#define EPS 1e-9

#define PI acos(-1.0)

#define INF 0x3f3f3f3f

#define LL long long

const int MOD = 1E9+7;

const int N = 1000000+5;

const int dx[] = {-1,1,0,0,-1,-1,1,1};

const int dy[] = {0,0,-1,1,-1,1,-1,1};

using namespace std;



LL n,A,B;

LL a[N];

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() {

    while(scanf("%lld%lld%lld",&n,&A,&B)!=EOF)

{

    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;

}


// Faster C++ solution for the same problem (measured speedup: 5.167645864435401x):
#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;

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



using namespace std;

using ll = long long;

const int maxn = 1e5+7;



int n, a, b;

ll h[maxn];

bool check(ll x){

	ll sum=0;

	ll sub=a-b;

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

		if(x*b<h[i]){

			ll tmp=h[i]-x*b;

			sum += (tmp-1)/sub+1;

		}

	}

	return sum<=x;

}

int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0);

	cin >> n >> a >> b;

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

		cin >> h[i];

	}

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

	ll l=1, r=1e9+7;

	while(l<r){

		ll mid = l+r>>1;

		if(check(mid)){

			r=mid;

		}else{

			l=mid+1;

		}

	}

	cout << r;

}

// Faster C++ solution for the same problem (measured speedup: 5.299123250371764x):
#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;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <sstream>

#include <cstring>

#include <vector>

#include <algorithm>

#include <cmath>

#include <iomanip>

#include <queue>

#include <stack>

using namespace std;

typedef long long ll;

const int mod=1e9+7;

const int maxn=1e5+10;

ll heal[maxn];

ll A,B;

int N;

bool check(ll x)

{

    ll cnt=0;

    ll gap;

    //cout<<x<<"!"<<endl;

    for(int i=N-1;i>=0;i--)

    {

        //cout<<heal[i]<<' '<<cnt<<endl;

        if(x*B>=heal[i])return true;

        gap=heal[i]-x*B;

       //cout<<"g"<<gap<<endl;

        cnt+=gap/A;

        if(gap%A)cnt++;

        //cout<<cnt<<endl;

        if(cnt>x)return false;

    }

    return true;

}

int main()

{

    cin>>N>>A>>B;

    A-=B;

    for(int i=0;i<N;i++)cin>>heal[i];

    sort(heal,heal+N);

    ll l,r,mid;

    l=1;r=N*heal[N-1]/B+1;

    while(l<r)

    {

        mid=(l+r)>>1;

        if(check(mid))r=mid;

        else l=mid+1;

    }

    cout<<l<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.240752643249671x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.292730953848671x):
#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;

}
// Slower C++ solution:
#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

}

// Faster C++ solution for the same problem (measured speedup: 5.296851512949163x):


#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;

}
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 5.1590121683902055x):
#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;

}
// Slower C++ solution:
#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);

}

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

#include<math.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;

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

            if (cnt > m) break;

        }

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

        else r = m - 1, ans = m;

    }

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

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.7947088599535195x):
#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;

}
// 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.5904997415524518x):
#include <iostream>

#include <cstdio>

using namespace std;

typedef long long LL;

const int maxn = 1e5 + 10;

LL v[maxn];

 

// edge

int cnt, h[maxn];

struct edge

{

    int to, pre;

} e[maxn<<1];

void add(int from, int to)

{

    cnt++;

    e[cnt].pre = h[from];

    e[cnt].to = to;

    h[from] = cnt;

}

 

int deg[maxn], ok;

LL dfs(int x, int f)

{

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

    LL ret = v[x] + v[x];

    for(int i = h[x]; i; i = e[i].pre)

    {

        int to = e[i].to;

        if(to == f) continue;

        LL tmp = dfs(to, x);

        if(tmp > v[x]) ok = 0;

        if(tmp > ret) ok = 0;

        ret -= tmp;

    }

    if(ret > v[x]) ok = 0;

    return ret;

}

 

int main(void)

{

    int N;

    scanf("%d", &N);

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

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

    {

        int a, b;

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

        add(a, b); add(b, a);

        deg[a]++; deg[b]++;

    }

    if(N == 2)

    {

        if(v[1] == v[2]) puts("YES");

        else puts("NO");

        return 0;

    }

    int rt = 0;

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

    if(deg[i] != 1) {rt = i; break;}

    ok = 1;

    if(dfs(rt, 0)) ok = 0;

    puts(ok ? "YES" : "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.5914517198563516x):
#include <cstdio>

#include <cstdlib>

#include <iostream>



typedef long long LL;



const int MAXN = 1e5;

int n, ecnt, val[MAXN + 5], head[MAXN + 5], d[MAXN + 5];



struct Edge { int to, nxt; } graph[MAXN * 2 + 5];



inline void link ( const int s, const int t ) {

	graph[++ ecnt] = { t, head[s] };

	head[s] = ecnt;

}



inline int DFS ( const int u, const int f ) {

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

	LL sum = 0; int mx = 0;

	for ( int i = head[u], v; i; i = graph[i].nxt ) {

		if ( ( v = graph[i].to ) ^ f ) {

			int up = DFS ( v, u );

			sum += up, mx = std::max ( mx, up );

		}

	}

	if ( sum - val[u] > std::min ( sum >> 1, sum - mx )

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

	return 2 * val[u] - sum;

	// a+b=val, 2a+b=sum, a<=min{sum/2,sum-mx}

}



int main () {

	scanf ( "%d", &n );

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

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

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

		link ( u, v ), link ( v, u );

	}

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

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

		if ( d[i] > 1 )

			return puts ( DFS ( i, 0 ) ? "NO" : "YES" ), 0;

	return 0;

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

using namespace std;

typedef long long ll;

const int TAM = 100004;



vector <ll> adj [TAM];

ll val [TAM], pt [TAM];

int n;



void form( int x ){

    int nxt;

    for( int i = 0 ; i < adj[x].size() ; i ++ ){

        nxt = adj[x][i];

        if( nxt == pt[x] ) continue;

        pt[nxt] = x;

        //cout << " parent " << nxt << " = " << pt[nxt] << endl ;

        form(nxt);

    }

}



ll dn, up;

void solve ( ll cap , ll sum ){

    dn = sum - cap;

    up = cap - dn;

}



bool pos = 1;



ll find_up( int x ){

    //cout << " IN " << x << endl ;

    if( adj[x].size() == 1 ) return val[x];

    ll sum = 0, cap = val[x];

    int nxt;

    ll v;

    for( int i = 0 ; i < adj[x].size() ; i ++ ){

        nxt = adj[x][i];

        if( nxt == pt[x] ) continue;

        v = find_up(nxt);

        sum += v;

        if( v > cap ) pos = 0;

        //cout << x << " -> " << nxt << " = " << v << endl ;

    }

    solve( cap , sum );

    if( dn + up != cap or dn < 0 or up < 0 ) pos = 0;

    return up;

}



int main(){



    int a, b;

    scanf("%d",&n);

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

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

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

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

        adj[a].push_back(b);

        adj[b].push_back(a);

    }



    if( n == 2 ){

        if( val[1] == val[2] ) printf("YES\n");

        else printf("NO\n");

        return 0;

    }



    int rt;

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

        if( adj[i].size() > 1 ){

            pt [i] = i;

            form(i);

            rt = i;

            break;

        }

    }

    if( !find_up(rt) and pos ) printf("YES\n");

    else printf("NO\n");



    return 0;

}


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

#include <cstdlib>

#include <iostream>



typedef long long LL;



const int MAXN = 1e5;

int n, ecnt, val[MAXN + 5], head[MAXN + 5], d[MAXN + 5];



struct Edge { int to, nxt; } graph[MAXN * 2 + 5];



inline void link ( const int s, const int t ) {

	graph[++ ecnt] = { t, head[s] };

	head[s] = ecnt;

}



inline int DFS ( const int u, const int f ) {

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

	LL sum = 0; int mx = 0;

	for ( int i = head[u], v; i; i = graph[i].nxt ) {

		if ( ( v = graph[i].to ) ^ f ) {

			int up = DFS ( v, u );

			sum += up, mx = std::max ( mx, up );

		}

	}

	if ( sum - val[u] > std::min ( sum >> 1, sum - mx )

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

	return 2 * val[u] - sum;

	// a+b=val, 2a+b=sum, a<=min{sum/2,sum-mx}

}



int main () {

	scanf ( "%d", &n );

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

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

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

		link ( u, v ), link ( v, u );

	}

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

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

		if ( d[i] > 1 )

			return puts ( DFS ( i, 0 ) ? "NO" : "YES" ), 0;

	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.6050619963296233x):
#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;

}