Language: C++

#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);

int n;
vector<vi> G;
vi used;
vll A;

ll dfs(int v, bool root = true){
    used[v] = true;

    if(not root and (int)G[v].size() == 1){
        return A[v];
    }

    priority_queue<ll> q;
    ll sum = 0;
    for(auto& nv : G[v]){
        if(used[nv]) continue;
        ll cur = dfs(nv, false);
        if(cur == -1) return -1;
        sum += cur;
        q.push(cur);
    }
    if(sum < A[v]) return -1;

    ll ret = A[v];
    while(sum > ret and q.size() >= 2){
        ll a = q.top(); q.pop();
        ll b = q.top(); q.pop();

        ll diff = min(b, sum - ret);
        a -= diff;
        b -= diff;
        sum -= 2 * diff;
        ret -= diff;

        if(a > 0) q.push(a);
        if(b > 0) q.push(b);
    }

    return ret;
}

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;
    A = vll(n); for(auto& e : A) cin >> e;

    G = vector<vi>(n);
    rep(i, n - 1){
        int a, b; cin >> a >> b; a--, b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    used = vi(n);

    int r = -1;
    rep(v, n){
        if(G[v].size() == 1){
            r = v;
            break;
        }
    }
    assert(r >= 0);

    cout << (dfs(r) == A[r] ? "YES":"NO") << endl;

    return 0;
}


Compiler output:
a.cc:26:10: error: 'vector' does not name a type
   26 | using vi=vector<int>;
      |          ^~~~~~
a.cc:27:11: error: 'vector' does not name a type
   27 | using vll=vector<ll>;
      |           ^~~~~~
a.cc:30:19: error: 'acos' was not declared in this scope
   30 | const double PI = acos(-1.0);
      |                   ^~~~
a.cc:43:1: error: 'random_device' does not name a type
   43 | random_device rd;
      | ^~~~~~~~~~~~~
a.cc:44:1: error: 'mt19937' does not name a type
   44 | mt19937 mt(rd());
      | ^~~~~~~
a.cc:45:1: error: 'uniform_int_distribution' does not name a type
   45 | uniform_int_distribution<int> dice(1,6);
      | ^~~~~~~~~~~~~~~~~~~~~~~~
a.cc:46:1: error: 'uniform_real_distribution' does not name a type
   46 | uniform_real_distribution<double> score(0.0,10.0);
      | ^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:49:1: error: 'vector' does not name a type
   49 | vector<vi> G;
      | ^~~~~~
a.cc:50:1: error: 'vi' does not name a type; did you mean 'void'?
   50 | vi used;
      | ^~
      | void
a.cc:51:1: error: 'vll' does not name a type; did you mean 'll'?
   51 | vll A;
      | ^~~
      | ll
a.cc: In function 'll dfs(int, bool)':
a.cc:54:5: error: 'used' was not declared in this scope
   54 |     used[v] = true;
      |     ^~~~
a.cc:56:26: error: 'G' was not declared in this scope
   56 |     if(not root and (int)G[v].size() == 1){
      |                          ^
a.cc:57:16: error: 'A' was not declared in this scope
   57 |         return A[v];
      |                ^
a.cc:60:5: error: 'priority_queue' was not declared in this scope
   60 |     priority_queue<ll> q;
      |     ^~~~~~~~~~~~~~
a.cc:1:1: note: 'std::priority_queue' is defined in header '<queue>'; this is probably fixable by adding '#include <queue>'
  +++ |+#include <queue>
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
a.cc:60:22: error: expected primary-expression before '>' token
   60 |     priority_queue<ll> q;
      |                      ^
a.cc:60:24: error: 'q' was not declared in this scope
   60 |     priority_queue<ll> q;
      |                        ^
a.cc:62:20: error: 'G' was not declared in this scope
   62 |     for(auto& nv : G[v]){
      |                    ^
a.cc:69:14: error: 'A' was not declared in this scope
   69 |     if(sum < A[v]) return -1;
      |              ^
a.cc:71:14: error: 'A' was not declared in this scope
   71 |     ll ret = A[v];
      |              ^
a.cc:76:19: error: 'min' was not declared in this scope
   76 |         ll diff = min(b, sum - ret);
      |                   ^~~
a.cc: In function 'int main()':
a.cc:90:5: error: 'cin' was not declared in this scope
   90 |     cin.tie(0);
      |     ^~~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
a.cc:91:5: error: 'ios' has not been declared
   91 |     ios::sync_with_stdio(false);
      |     ^~~
a.cc:94:5: error: 'A' was not declared in this scope
   94 |     A = vll(n); for(auto& e : A) cin >> e;
      |     ^
a.cc:94:9: error: 'vll' was not declared in this scope; did you mean 'll'?
   94 |     A = vll(n); for(auto& e : A) cin >> e;
      |         ^~~
      |         ll
a.cc:96:5: error: 'G' was not declared in this scope
   96 |     G = vector<vi>(n);
      |     ^
a.cc:96:9: error: 'vector' was not declared in this scope
   96 |     G = vector<vi>(n);
      |         ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
a.cc:96:16: error: 'vi' was not declared in this scope
   96 |     G = vector<vi>(n);
      |                ^~
a.cc:97:9: error: 'i' was not declared in this scope
   97 |     rep(i, n - 1){
      |         ^
a.cc:1:28: note: in definition of macro 'rep'
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                            ^~~~~~~~~~~
a.cc:1:40: error: '_range' was not declared in this scope; did you mean '_rrange'?
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                                        ^~~~~~
a.cc:97:5: note: in expansion of macro 'rep'
   97 |     rep(i, n - 1){
      |     ^~~
a.cc:1:47: error: '_rep' was not declared in this scope; did you mean 'rep'?
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                                               ^~~~
a.cc:97:5: note: in expansion of macro 'rep'
   97 |     rep(i, n - 1){
      |     ^~~
a.cc:1:52: error: expected primary-expression before ')' token
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                                                    ^
a.cc:97:5: note: in expansion of macro 'rep'
   97 |     rep(i, n - 1){
      |     ^~~
a.cc:1:18: error: '_overload' was not declared in this scope
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                  ^~~~~~~~~
a.cc:97:5: note: in expansion of macro 'rep'
   97 |     rep(i, n - 1){
      |     ^~~
a.cc:102:5: error: 'used' was not declared in this scope
  102 |     used = vi(n);
      |     ^~~~
a.cc:105:9: error: 'v' was not declared in this scope
  105 |     rep(v, n){
      |         ^
a.cc:1:28: note: in definition of macro 'rep'
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                            ^~~~~~~~~~~
a.cc:1:52: error: expected primary-expression before ')' token
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
      |                                                    ^
a.cc:105:5: note: in expansion of macro 'rep'
  105 |     rep(v, n){
      |     ^~~
a.cc:111:5: error: 'assert' was not declared in this scope
  111 |     assert(r >= 0);
      |     ^~~~~~
a.cc:1:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
  +++ |+#include <cassert>
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
a.cc:113:5: error: 'cout' was not declared in this scope
  113 |     cout << (dfs(r) == A[r] ? "YES":"NO") << endl;
      |     ^~~~
a.cc:113:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:113:46: error: 'endl' was not declared in this scope
  113 |     cout << (dfs(r) == A[r] ? "YES":"NO") << endl;
      |                                              ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)