Problem p03809 (C++)

Buggy submission (status: Runtime Error):
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n;
const int maxN = 1e5 + 10;
vector<int> g[maxN];
ll a[maxN];
void dfs(int v, int p) {
  vector<ll> in_leaves;
  for (int to : g[v]) {
    if (to == p)
      continue;
    dfs(to, v);
    in_leaves.emplace_back(a[to]);
  }
  sort(in_leaves.begin(), in_leaves.end());
  if (in_leaves.empty())
    return;
  if (in_leaves.size() == 1) {
    if (a[in_leaves[0]] != a[v]) {
      cout << "NO\n";
      exit(0);
    }
  }
  //    cout << " HI " << v << endl;
  ll our_val = a[v];
  ll c = 0;
  for (ll &it : in_leaves) {
    c += it;
  }
  sort(in_leaves.begin(), in_leaves.end());
  ll s_l = c - our_val;
  ll s_f = our_val - s_l;
  if (s_l < 0 || s_f < 0) {
    cout << "NO\n";
    exit(0);
  }
  if (c - in_leaves.back() < s_l) {
    cout << "NO\n";
    exit(0);
  }
  a[v] = s_f;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (n == 2) {
    if (a[1] != a[2])
      cout << "NO\n";
    else
      cout << "YES\n";
    return 0;
  }
  int root = 1;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].size() != 1)
      root = i;
  }
  dfs(root, -1);
  if (a[root] == 0) {
    cout << "YES\n";
  } else {
    cout << "NO\n";
  }
  return 0;
}

Fixed submission:
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n;
const int maxN = 1e5 + 10;
vector<int> g[maxN];
ll a[maxN];
void dfs(int v, int p) {
  vector<ll> in_leaves;
  for (int to : g[v]) {
    if (to == p)
      continue;
    dfs(to, v);
    in_leaves.emplace_back(a[to]);
  }
  sort(in_leaves.begin(), in_leaves.end());
  if (in_leaves.empty())
    return;
  if (in_leaves.size() == 1) {
    if (in_leaves[0] != a[v]) {
      cout << "NO\n";
      exit(0);
    }
  }
  //    cout << " HI " << v << endl;
  ll our_val = a[v];
  ll c = 0;
  for (ll &it : in_leaves) {
    c += it;
  }
  sort(in_leaves.begin(), in_leaves.end());
  ll s_l = c - our_val;
  ll s_f = our_val - s_l;
  if (s_l < 0 || s_f < 0) {
    cout << "NO\n";
    exit(0);
  }
  if (c - in_leaves.back() < s_l) {
    cout << "NO\n";
    exit(0);
  }
  a[v] = s_f;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (n == 2) {
    if (a[1] != a[2])
      cout << "NO\n";
    else
      cout << "YES\n";
    return 0;
  }
  int root = 1;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].size() != 1)
      root = i;
  }
  dfs(root, -1);
  if (a[root] == 0) {
    cout << "YES\n";
  } else {
    cout << "NO\n";
  }
  return 0;
}