Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve {
public:
    int N;
    vector<ll> A;
    vector<vector<int>> treeone;
    void input() {
        cin >> N;
        A.resize(N);
        for(int i = 0; i < N; ++i){
            cin >> A[i];
        }
        treeone.resize(N);
        for(int i = 0; i < N - 1; ++i){
            int a,b;
            cin >> a >> b;
            a--,b--;
            treeone[a].push_back(b);
            treeone[b].push_back(a);
        }
    }

    ll dfs(int n, int p){
//        if(treeone[n].size() == 1) return A[n];
        ll ret = 2 * A[n];
        if(treeone[n].size() == 1) ret = A[n];

        for(auto e : treeone[n]){
            if(e != p){
                ret -= dfs(e, n);
            }
        }

        return ret;
    }

    void solve() {
        input();
        ll val = dfs(0,-1);
        if(val == 0){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    Solve().solve();#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve {
public:
    int N;
    vector<ll> A;
    vector<vector<int>> treeone;
    void input() {
        cin >> N;
        A.resize(N);
        for(int i = 0; i < N; ++i){
            cin >> A[i];
        }
        treeone.resize(N);
        for(int i = 0; i < N - 1; ++i){
            int a,b;
            cin >> a >> b;
            a--,b--;
            treeone[a].push_back(b);
            treeone[b].push_back(a);
        }
    }

    ll dfs(int n, int p){
//        if(treeone[n].size() == 1) return A[n];
        ll ret = 2 * A[n];
        if(treeone[n].size() == 1) ret = A[n];

        for(auto e : treeone[n]){
            if(e != p){
                ret -= dfs(e, n);
            }
        }

        return ret;
    }

    void solve() {
        input();
        ll val = dfs(0,-1);
        if(val == 0){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    Solve().solve();


    return 0;
}



    return 0;
}


Compiler output:
a.cc:108:21: error: stray '#' in program
  108 |     Solve().solve();#include <algorithm>
      |                     ^
a.cc: In function 'int main()':
a.cc:108:22: error: 'include' was not declared in this scope
  108 |     Solve().solve();#include <algorithm>
      |                      ^~~~~~~
a.cc:108:31: error: 'algorithm' was not declared in this scope
  108 |     Solve().solve();#include <algorithm>
      |                               ^~~~~~~~~
a.cc:146:1: error: a template declaration cannot appear at block scope
  146 | template<typename T1, typename T2>
      | ^~~~~~~~
a.cc:210:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  210 | int main() {
      |         ^~
a.cc:210:9: note: remove parentheses to default-initialize a variable
  210 | int main() {
      |         ^~
      |         --
a.cc:210:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:210:12: error: a function-definition is not allowed here before '{' token
  210 | int main() {
      |            ^