Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

vector<long long int> sizes;
vector<vector<int>> edges;
int dfs(const int now) {
  long long int num = sizes[now];
  for (auto e : edges[now]) {
    num += dfs(e);
  }
  return sizes[now] = num;
}
long long int dfs2(const int now) {
  vector<long long> v;
  long long int ans = 0;
  for (auto e : edges[now]) {
    v.emplace_back(sizes[e]);
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < v.size(); ++i) {
    ans += sum;
    sum += v[i];
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  edges.resize(N);
  sizes.resize(N);
  vector<int> roots;
  vector<int> nums(N);
  vector<vector<int>> revedges(N);
  for (int i = 0; i < N; ++i) {
    int M, L;
    cin >> M >> L;
    sizes[i] = M;
    if (L != -1) {
      edges[L].push_back(i);
      revedges[i].push_back(L);
      nums[L]++;
    } else {
      roots.emplace_back(i);
    }
  }
  queue<int> que;
  for (int i = 0; i < nums.size(); ++i) {
    if (!nums[i])
      que.push(i);
  }
  while (!que.empty()) {
    const int now = que.front();
    que.pop();
    long long int num = sizes[now];
    for (auto e : edges[now]) {
      num += sizes[e];
    }
    for (auto re : revedges[now]) {
      nums[re]--;
      if (!nums[re])
        que.push(re);
    }
    sizes[now] = num;
  }
  long long int ans = 0;

  for (int i = 0; i < N; ++i) {

    ans += dfs2(i);
  }
  cout << ans << endl;
  return 0;
}

Error: 12

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

vector<long long int> sizes;
vector<vector<int>> edges;
int dfs(const int now) {
  long long int num = sizes[now];
  for (auto e : edges[now]) {
    num += dfs(e);
  }
  return sizes[now] = num;
}
long long int dfs2(const int now) {
  vector<long long> v;
  long long int ans = 0;
  for (auto e : edges[now]) {
    v.emplace_back(sizes[e]);
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < v.size(); ++i) {
    ans += sum;
    sum += v[i];
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  edges.resize(N);
  sizes.resize(N);
  vector<int> roots;
  vector<int> nums(N);
  vector<vector<int>> revedges(N);
  for (int i = 0; i < N; ++i) {
    int M, L;
    cin >> M >> L;
    sizes[i] = M;
    if (L != -1) {
      edges[L].push_back(i);
      revedges[i].push_back(L);
      nums[i]++;
    } else {
      roots.emplace_back(i);
    }
  }
  queue<int> que;
  for (int i = 0; i < nums.size(); ++i) {
    if (!nums[i])
      que.push(i);
  }
  while (!que.empty()) {
    const int now = que.front();
    que.pop();
    long long int num = sizes[now];
    for (auto e : edges[now]) {
      num += sizes[e];
    }
    for (auto re : revedges[now]) {
      nums[re]--;
      if (!nums[re])
        que.push(re);
    }
    return sizes[now] = num;
  }
  long long int ans = 0;
  for (int i = 0; i < roots.size(); ++i) {
    dfs(roots[i]);
  }
  for (int i = 0; i < N; ++i) {

    ans += dfs2(i);
  }
  cout << ans << endl;
  return 0;
}