Language: C++

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define int long long

typedef pair<int, int> ii;

const int N = 1e5 + 5;
int n, a[N], x[N], y[N], root;
vector<int> graph[N];

void dfs(int u,int p){
    if(graph[u].size() == 1){
        y[u] = a[u];
        return ;
    }
    for(int i = 0; i < graph[u].size(); i++){
        int v = graph[u][i];
        if(v != p)
            dfs(v, u);
    }
    int sum = 0;
    for(int i = 0; i < graph[u].size(); i++){
        int v = graph[u][i];
        if(v != p)
            sum += y[v];
    }
    x[u] = sum - a[u], y[u] = 2 * a[u] - sum;
    if(x[u] >= 0 &&  y[u] >= 0){
    }
    else{
        cout << "NO\n";
        exit(0);
    }
}

signed main() {
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= n - 1; i++){
        int u, v;
        cin >> u >> v;
        graph[u].pb(v);
        graph[v].pb(u);
    }
    if(n == 2){
        if(a[1] != a[2])
            cout << "NO\n";
        else
            cout << "YES\n";
        exit(0);
    }
    int mx = -1, root ;
    for(int i = 1; i <= n; i++){
        if(graph[i].size() >= 2){
            if(graph[i].size() > mx){
                mx = max(mx, graph[i].size());
                root = i;
            }
        }
    }
    dfs(root, root);
    if(y[root] != 0){
        cout << "NO\n";
        exit(0);
    }
    cout << "YES\n";
}


Compiler output:
a.cc: In function 'int main()':
a.cc:65:25: error: no matching function for call to 'max(long long int&, std::vector<long long int>::size_type)'
   65 |                 mx = max(mx, graph[i].size());
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~
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:65:25: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'})
   65 |                 mx = max(mx, graph[i].size());
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~
/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:65:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   65 |                 mx = max(mx, graph[i].size());
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~