Language: C++

#include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;

int N;
LL A[114514];
vector<int> es[114514];

pair<LL, LL> dfs(int v, int p) {
  int num_ch = 0;
  LL l = 0;
  LL r = 0;
  LL s = 0;
  LL ls = 0;
  LL maxa = -1;
  LL maxb = -1;
  LL maxamina = 0x7fffffffffLL;
  LL maxamaxa = -1;;
  for (int u : es[v]) {
    if (p == u) continue;
    LL x, y;
    tie(x, y) = dfs(u, v);
    s += y;
    ls += x;
    maxa = max(maxa, x);
    if (maxa == x) {
      maxamina = min(maxamina, y);
    }
    maxb = max(maxb, y);
    if (maxb == y) {
      maxbmaxa = max(maxbmaxa, x);
    }
    LL nr = y+r;
    LL nl = max(0LL, max(x, l) - min(y, r));
    l = nl;
    r = nr;
    num_ch++;
  }

  if (ls/2 + ls%2 > A[v]) {
    puts("NO");
    exit(0);
  }

  LL mp = 0;
  if (maxa > s-maxamina) mp = maxa - (s-maxamina);

  LL mp2 = s/2;
  if (2*maxbmaxa > s) mp2 = s - maxbmaxa;
  mp2 = A[v] - mp2;

  if (num_ch == 0) return make_pair(A[v], A[v]);
  if (num_ch == 1) {
    if (l <= A[v] && A[v] <= r) return make_pair(A[v], A[v]);
    puts("NO");
    exit(0);
  }
  return make_pair(max(l, max(mp2, mp)), min(r, A[v]));
}

int main() {
  scanf("%d", &N);
  rep(i, N) {
    scanf("%lld", &A[i]);
  }

  rep(i, N-1) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    es[u].eb(v);
    es[v].eb(u);
  }

  vector<int> vs(N);
  rep(i, N) vs[i] = i;
  random_shuffle(all(vs));
  
  int cnt = 0;
  rep(i, N) {
    int v = vs[i];
    if (es[v].size() > 1) {
      cnt++;
      if (dfs(v, v).first != 0) {
        puts("NO");
        exit(0);
      }

      if (cnt > 20) {
        puts("YES");
        exit(0);
      }
    }
  }

  if (N > 2) {
    puts("YES");
    exit(0);
  }

  assert(N == 2);
  if (A[0] == A[1]) puts("YES");
  else puts("NO");
}


Compiler output:
a.cc: In function 'std::pair<long long int, long long int> dfs(int, int)':
a.cc:54:7: error: 'maxbmaxa' was not declared in this scope; did you mean 'maxamaxa'?
   54 |       maxbmaxa = max(maxbmaxa, x);
      |       ^~~~~~~~
      |       maxamaxa
a.cc:72:9: error: 'maxbmaxa' was not declared in this scope; did you mean 'maxamaxa'?
   72 |   if (2*maxbmaxa > s) mp2 = s - maxbmaxa;
      |         ^~~~~~~~
      |         maxamaxa
a.cc: In function 'int main()':
a.cc:101:17: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, vector<int> >]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
  101 |   random_shuffle(all(vs));
      |   ~~~~~~~~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:4492:5: note: declared here
 4492 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~