Language: C++

#include <bits/stdc++.h>
using namespace std;
vector<int> parent(vector<vector<int>> &E, int r){
	int N = E.size();
	vector<int> p(N, -1);
	queue<int> Q;
	Q.push(r);
	while (!Q.empty()){
		int v = Q.front();
		Q.pop();
		for (int w : E[v]){
			if (p[w] == -1){
				p[w] = v;
				Q.push(w);
			}
		}
	}
	return p;
}
vector<vector<int>> child_2(vector<int> &p, int r){
	int N = p.size();
	vector<vector<int>> c(N);
	for (int i = 0; i < N; i++){
		if (i != r){
			c[p[i]].push_back(i);
		}
	}
	return c;
}
void dfs(vector<long long> &A, vector<vector<int>> &c, int v, bool &ok){
  if (c[v].empty()){
    return;
  }
  long long sum = 0;
  long long M = 0;
  for (int w : c[v]){
    dfs(A, c, w, ok);
    sum += A[w];
    M = max(M, A[w]);
  }
  long long tmp = A[v] * 2 - sum;
  long long pair_cnt = max(sum - A[v], 0);
  /*
  cout << "v = " << v << endl;
  cout << "sum = " << sum << endl;
  cout << "pair = " << pair_cnt << endl;
  cout << "max = " << M << endl;
  */
  long long pair_cnt_2 = min(sum - M, sum / 2);
  if (pair_cnt_2 < pair_cnt){
    //cout << "not ok" << endl;
    ok = false;
  }
  A[v] = tmp;
}
int main(){
  int N;
  cin >> N;
  vector<long long> A(N + 1, 0);
  for (int i = 1; i <= N; i++){
    cin >> A[i];
  }
  if (N == 2){
    if (A[1] == A[2]){
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
    return 0;
  }
  vector<vector<int>> E(N + 1);
  for (int i = 0; i < N - 1; i++){
    int a, b;
    cin >> a >> b;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  int r = 0;
  for (int i = 1; i <= N; i++){
    if (E[i].size() != 1){
      E[0].push_back(i);
      E[i].push_back(0);
      r = i;
      break;
    }
  }
  N++;
  vector<int> p = parent(E, 0);
  vector<vector<int>> c = child_2(p, 0);
  bool ok = true;
  dfs(A, c, 0, ok);
  if (ok && A[r] == 0){
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Compiler output:
a.cc: In function 'void dfs(std::vector<long long int>&, std::vector<std::vector<int> >&, int, bool&)':
a.cc:42:27: error: no matching function for call to 'max(long long int, int)'
   42 |   long long pair_cnt = max(sum - A[v], 0);
      |                        ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:42:27: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   42 |   long long pair_cnt = max(sum - A[v], 0);
      |                        ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:42:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   42 |   long long pair_cnt = max(sum - A[v], 0);
      |                        ~~~^~~~~~~~~~~~~~~