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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define N 1000
#define INF 0x3f3f3f

using namespace std;

const int MOD = 1e9 + 7, maxn = 2 * 1e6 + 5;
int t, n, ce, fir[maxn], m[maxn], l[maxn], in[maxn], lis[maxn];
long long ans;

struct graph {
  int next;
  int y;
} e[maxn];

void add(int x, int y) {
  e[++ce].y = y;
  e[ce].next = fir[x];
  fir[x] = ce;
}

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}

void work() {
  queue<int> q;
  for (int i = 0; i < n; i++)
    if (in[i] == 0)
      q.push(i);
  while (!q.empty()) {
    int temp = q.front();
    // cout<<' '<<temp<<endl;
    int count = 0;
    for (int p = fir[temp]; p; p = e[p].next) {
      int y = e[p].y;
      lis[count++] = m[y];
    }
    sort(lis, lis + count);
    for (int i = 0; i < count; i++)
      ans += (long long)lis[i] * (count - i - 1);
    //	cout<<ans<<endl;
    if (l[temp] != -1) {
      m[l[temp]] += m[temp];
      in[l[temp]]--;
      if (in[l[temp]] == 0) {
        q.push(l[temp]);
      }
    }
    q.pop();
  }
}

int main() {
  // freopen("in.txt", "r", stdin);
  while (scanf("%d", &n) != EOF) {
    memset(fir, 0, sizeof(fir));
    memset(in, 0, sizeof(in));
    ce = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &m[i], &l[i]);
      if (l[i] != -1) {
        in[l[i]]++;
        add(l[i], i);
      }
    }
    ans = 0;
    work();
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma comment(linker, "/STACK:1024000000, 1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef int ll;
const int N = 2e6 + 5;

struct sil {
  int last, k;
} edge[N];
int top, n, root[N];
ll cost[N], a[N], cnt;
long long ans;
bool flag[N];
bool cmp(const ll a, const ll b) { return a > b; }
void addedge(int x, int y) {
  edge[++top].last = root[x];
  edge[top].k = y;
  root[x] = top;
}

void qS(int l, int r) {
  int i = l, j = r;
  ll x = a[(l + r) / 2];
  while (i <= j) {
    while (a[i] > x)
      ++i;
    while (a[j] < x)
      --j;
    if (i <= j) {
      a[0] = a[i];
      a[i] = a[j];
      a[j] = a[0];
      ++i;
      --j;
    }
  }
  if (l < j)
    qS(l, j);
  if (i < r)
    qS(i, r);
}

void dis(int k) {
  cnt++;
  while (cnt > 2123456)
    puts("111");
  if (root[k] == -1)
    return;
  for (int i = root[k]; i != -1; i = edge[i].last) {
    int j = edge[i].k;
    dis(j);
    cost[k] += cost[j];
  }
  a[0] = 0;
  for (int i = root[k]; i != -1; i = edge[i].last)
    a[++a[0]] = cost[edge[i].k];
  sort(a + 1, a + a[0] + 1, cmp);
  for (int i = 2; i <= a[0]; ++i)
    ans += (ll)(i - 1) * a[i];
}

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  scanf("%d", &n);
  cnt = 0;
  memset(root, -1, sizeof root);
  memset(flag, 0, sizeof flag);
  top = 0;
  int k;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &cost[i], &k);
    if (k == -1)
      flag[i] = true;
    else
      addedge(k, i);
  }
  ans = 0;
  for (int i = 0; i < n; ++i)
    if (flag[i])
      dis(i);
  printf("%lld\\n", ans);
  return 0;
}