Language: C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <set>
using namespace std;

vector<vector<int>> G,GG;
vector<bool> seen;
vector<long long> a;
vector<long long> b;

void dfs(int u){
  seen[u]=true;
  for(int v:G[u]){
    if(!seen[v]){
      GG[u].push_back(v);
      dfs(v);
    }
  }
}

bool res;

long long dfs2(int v){
  if(GG[v].size()==0){
    b[v]=a[v];
    return b[v];
  }
  vector<long long> kk;
  for(int u:GG[v]){
    kk.push_back(dfs2(u));
  }
  long long tmp=0;
  for(long long t:kk)tmp+=t;
  // if(tmp<a[v])res=false;
  long long pr=tmp-a[v];
  int si=kk.size();
  if(si>1){
    sort(kk.begin(),kk.end());
    while(pr>0&&kk[si-2]>0){
      long long di = min(kk[si-2],pr);
      di=max(1,di/2);
      kk[si-2]-=di;
      kk[si-1]-=di;
      pr-=di;
      sort(kk.begin(),kk.end());
    }
  }
  if(pr>0)res=false;
  b[v]=2*a[v]-tmp;
  if(b[v]<0)res=false;
  return b[v];
}

int main(int argc, char const *argv[]) {
  long long n;cin>>n;
  a = vector<long long>(n);
  b = vector<long long>(n);
  for(int i=0;i<n;++i)cin>>a[i];
  G=vector<vector<int>>(n);
  GG=vector<vector<int>>(n);
  for(int i=0;i<n-1;++i){
    int u,v;cin>>u>>v;
    u--;v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  seen=vector<bool>(n);
  res = true;
  dfs(0);
  dfs2(0);
  if(res)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;



  return 0;
}


Compiler output:
a.cc: In function 'long long int dfs2(int)':
a.cc:47:13: error: no matching function for call to 'max(int, long long int)'
   47 |       di=max(1,di/2);
      |          ~~~^~~~~~~~
In file included from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 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:47:13: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   47 |       di=max(1,di/2);
      |          ~~~^~~~~~~~
/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,
                 from a.cc:5:
/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:47:13: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   47 |       di=max(1,di/2);
      |          ~~~^~~~~~~~