Problem p03809 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) ((a).begin(), (a).end())
#define rall(a) ((a).rbegin(), (a).rend())
#define each(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define reach(it, o) for (auto it = (o).rbegin(); it != (o).rend(); ++it)
#define fi first
#define se second

#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define dprt(fmt, ...)                                                         \
  if (opt_debug) {                                                             \
    fprintf(stderr, fmt, ##__VA_ARGS__);                                       \
  }
#define darr(a)                                                                \
  if (opt_debug) {                                                             \
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \
    cerr << endl;                                                              \
  }
#define darr_range(a, f, t)                                                    \
  if (opt_debug) {                                                             \
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \
    cerr << endl;                                                              \
  }
#define dvec(v)                                                                \
  if (opt_debug) {                                                             \
    copy(all(v), ostream_iterator<int>(cerr, " "));                            \
    cerr << endl;                                                              \
  }
#define darr2(a)                                                               \
  if (opt_debug) {                                                             \
    rep(__i, (arrsz(a))) { darr((a)[__i]); }                                   \
  }
#define WAIT()                                                                 \
  if (opt_debug) {                                                             \
    string _wait_;                                                             \
    cerr << "(hit return to continue)" << endl;                                \
    getline(cin, _wait_);                                                      \
  }
#define dump(x)                                                                \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;           \
  }
// dump vector elements in [s, e)
#define dumpv(v, s, e)                                                         \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #v << " = ";                          \
    REP(__i, s, e) { cerr << v[__i] << "\t"; }                                 \
    cerr << endl;                                                              \
  }
#define dumpl(x)                                                               \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl;            \
  }
#define dumpf()                                                                \
  if (opt_debug) {                                                             \
    cerr << __PRETTY_FUNCTION__ << endl;                                       \
  }
#define where()                                                                \
  if (opt_debug) {                                                             \
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \
         << "]" << endl;                                                       \
  }
#define dumpb(bit, digits)                                                     \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #bit << " = ";                        \
    for (int __i = digits - 1; __i >= 0; __i--) {                              \
      cerr << static_cast<bool>(bit & (1 << __i));                             \
      if (__i % 4 == 0) {                                                      \
        cerr << " ";                                                           \
      }                                                                        \
    }                                                                          \
    cerr << endl;                                                              \
  }

// operator override for debug
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (typeof(v.begin()) itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const double PI = (1 * acos(0.0));
const double INF = 1e9;
const double EPS = 1e-9;
const double mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

bool opt_debug = true;

vi g[100010];
int a[100010];

ll dfs(int v, int par) {
  ll sum = (g[v].size() <= 1) ? a[v] : 0;
  ll ma = sum;
  each(u, g[v]) {
    if (*u != par) {
      ll cur = dfs(*u, v);
      if (cur < 0)
        return -1;
      if (ma < cur)
        ma = cur;
      sum += cur;
    }
  }
  ll lim = 2ll * a[v] - sum;
  if (lim < (ma - (sum - ma)) || lim > sum)
    return -1;
  return lim;
}

int main() {
  ios_base::sync_with_stdio(0);
  finput("./input");
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  cout << ((dfs(0, -1) == 0) ? "YES" : "NO") << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) ((a).begin(), (a).end())
#define rall(a) ((a).rbegin(), (a).rend())
#define each(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define reach(it, o) for (auto it = (o).rbegin(); it != (o).rend(); ++it)
#define fi first
#define se second

#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define dprt(fmt, ...)                                                         \
  if (opt_debug) {                                                             \
    fprintf(stderr, fmt, ##__VA_ARGS__);                                       \
  }
#define darr(a)                                                                \
  if (opt_debug) {                                                             \
    copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " "));               \
    cerr << endl;                                                              \
  }
#define darr_range(a, f, t)                                                    \
  if (opt_debug) {                                                             \
    copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " "));              \
    cerr << endl;                                                              \
  }
#define dvec(v)                                                                \
  if (opt_debug) {                                                             \
    copy(all(v), ostream_iterator<int>(cerr, " "));                            \
    cerr << endl;                                                              \
  }
#define darr2(a)                                                               \
  if (opt_debug) {                                                             \
    rep(__i, (arrsz(a))) { darr((a)[__i]); }                                   \
  }
#define WAIT()                                                                 \
  if (opt_debug) {                                                             \
    string _wait_;                                                             \
    cerr << "(hit return to continue)" << endl;                                \
    getline(cin, _wait_);                                                      \
  }
#define dump(x)                                                                \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;           \
  }
// dump vector elements in [s, e)
#define dumpv(v, s, e)                                                         \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #v << " = ";                          \
    REP(__i, s, e) { cerr << v[__i] << "\t"; }                                 \
    cerr << endl;                                                              \
  }
#define dumpl(x)                                                               \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl;            \
  }
#define dumpf()                                                                \
  if (opt_debug) {                                                             \
    cerr << __PRETTY_FUNCTION__ << endl;                                       \
  }
#define where()                                                                \
  if (opt_debug) {                                                             \
    cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__     \
         << "]" << endl;                                                       \
  }
#define dumpb(bit, digits)                                                     \
  if (opt_debug) {                                                             \
    cerr << " [L" << __LINE__ << "] " << #bit << " = ";                        \
    for (int __i = digits - 1; __i >= 0; __i--) {                              \
      cerr << static_cast<bool>(bit & (1 << __i));                             \
      if (__i % 4 == 0) {                                                      \
        cerr << " ";                                                           \
      }                                                                        \
    }                                                                          \
    cerr << endl;                                                              \
  }

// operator override for debug
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (typeof(v.begin()) itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const double PI = (1 * acos(0.0));
const double INF = 1e9;
const double EPS = 1e-9;
const double mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

bool opt_debug = true;

vi g[100010];
int a[100010];

ll dfs(int v, int par) {
  ll sum = (g[v].size() <= 1) ? a[v] : 0;
  ll ma = sum;
  each(u, g[v]) {
    if (*u != par) {
      ll cur = dfs(*u, v);
      if (cur < 0)
        return -1;
      if (ma < cur)
        ma = cur;
      sum += cur;
    }
  }
  ll lim = 2ll * a[v] - sum;
  if (lim < (ma - (sum - ma)) || lim > sum)
    return -1;
  return lim;
}

int main() {
  ios_base::sync_with_stdio(0);
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  cout << ((dfs(0, -1) == 0) ? "YES" : "NO") << endl;
  return 0;
}