alexjercan/bugnet huggingface dataset:
Problem p01563 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int H, W, dp[2][1 << 16], I;
string s[128];
bool B(int x, int i) { return (x >> i) & 1; }
int bc(int x) { return x == 0 ? 0 : __builtin_popcount(x); }
int main() {
  cin >> H >> W;
  rep(i, H) cin >> s[i];
  rep(b, 1 << W) dp[0][b] = -1;
  dp[0][0] = 0;
  rep(i, H) {
    rep(j, W) {
      rep(b, 1 << W) dp[1 - I][b] = -1;
      rep(b, 1 << W) {
        if (dp[I][b] < 0)
          continue;
        // put
        int x = bc(b & ((1 << j) - 1));
        if (x < s[i].size()) {
          int pl = 0;
          if (i > 0 && B(b, j) &&
              s[i - 1][s[i - 1].size() + x - bc(b)] == s[i][x])
            pl += 2; // tate
          if (j > 0 && B(b, j - 1) && s[i][x] == s[i][x - 1])
            pl += 2; // yoko
          chmax(dp[1 - I][b | (1 << j)], dp[I][b] + pl);
        }
        // dont put
        chmax(dp[1 - I][b & (~(1 << j))], dp[I][b]);
      }
      I = 1 - I;
    }
    rep(b, 1 << W) if (bc(b) != s[i].size()) dp[I][b] = -1;
  }
  int ans = 0;
  rep(i, 1 << W) chmax(ans, dp[I][i]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int H, W, dp[2][1 << 16], I;
string s[16];
bool B(int x, int i) { return (x >> i) & 1; }
int bc(int x) { return x == 0 ? 0 : __builtin_popcount(x); }
int main() {
  cin >> H >> W;
  rep(i, H) cin >> s[i];
  rep(b, 1 << W) dp[0][b] = -1;
  dp[0][0] = 0;
  rep(i, H) {
    rep(j, W) {
      rep(b, 1 << W) dp[1 - I][b] = -1;
      rep(b, 1 << W) {
        if (dp[I][b] < 0)
          continue;
        // put
        int x = bc(b & ((1 << j) - 1));
        if (x < s[i].size()) {
          int pl = 0;
          if (i > 0 && B(b, j) &&
              s[i - 1][s[i - 1].size() + x - bc(b)] == s[i][x])
            pl += 2; // tate
          if (j > 0 && B(b, j - 1) && s[i][x] == s[i][x - 1])
            pl += 2; // yoko
          chmax(dp[1 - I][b | (1 << j)], dp[I][b] + pl);
        }
        // dont put
        chmax(dp[1 - I][b & (~(1 << j))], dp[I][b]);
      }
      I = 1 - I;
    }
    rep(b, 1 << W) if (bc(b) != s[i].size()) dp[I][b] = 0;
  }
  int ans = 0;
  rep(i, 1 << W) chmax(ans, dp[I][i]);
  cout << ans << endl;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
// #include <unordered_map>
#define N 1000010
// #define lson x<<1
// #define rson x<<1|1
// #define mid ((lt[x].l+lt[x].r)/2)
// #define ID(x, y) ((x)*m+(y))
// #define CHECK(x, y) ((x)>=0 && (x)<n && (y)>=0 && (y)<m)
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
const LL INF = 0x3f3f3f3f;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("F:/in.txt", "r", stdin);
  // freopen("F:/my.txt","w",stdout);
#endif // ONLINE_JUDGE
}
LL n, ans;
LL d[N];
int w[N];
vector<LL> G[N];
vector<LL> lis[N];
queue<LL> que;
int main() {
  //    Open();
  scanf("%lld", &n);
  for (LL i = 0; i < n; i++) {
    LL l;
    scanf("%lld%lld", &w[i], &l);
    if (l != -1)
      G[i].push_back(l), d[l]++;
  }
  for (LL i = 0; i < n; i++) {
    if (d[i] == 0)
      que.push(i);
  }
  while (!que.empty()) {
    LL u = que.front();
    que.pop();
    for (LL i = 0; i < G[u].size(); i++) {
      LL v = G[u][i];
      lis[v].push_back(w[u]);
      w[v] += w[u];
      d[v]--;
      if (d[v] == 0)
        que.push(v);
    }
  }
  ans = 0;
  for (LL i = 0; i < n; i++) {
    sort(lis[i].begin(), lis[i].end());
    LL siz = lis[i].size();
    for (LL j = siz - 2, cnt = 1; j >= 0; j--, cnt++) {
      ans += lis[i][j] * cnt;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
// #include <unordered_map>
#define N 1000010
// #define lson x<<1
// #define rson x<<1|1
// #define mid ((lt[x].l+lt[x].r)/2)
// #define ID(x, y) ((x)*m+(y))
// #define CHECK(x, y) ((x)>=0 && (x)<n && (y)>=0 && (y)<m)
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
const LL INF = 0x3f3f3f3f;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("F:/in.txt", "r", stdin);
  // freopen("F:/my.txt","w",stdout);
#endif // ONLINE_JUDGE
}
LL n, ans;
LL w[N], l[N], sum[N];
vector<LL> G[N];
LL lis[N];
LL dfs(LL u, LL fa) {
  sum[u] = w[u];
  for (LL i = 0; i < G[u].size(); i++) {
    LL v = G[u][i];
    if (v == fa)
      continue;
    sum[u] += dfs(v, u);
  }
  LL tail = 0;
  for (LL i = 0; i < G[u].size(); i++) {
    LL v = G[u][i];
    if (v == fa)
      continue;
    lis[tail++] = sum[v];
  }
  sort(lis, lis + tail);
  for (LL i = tail - 2, cnt = 1; i >= 0; i--, cnt++) {
    ans += lis[i] * cnt;
  }
  return sum[u];
}
int main() {
  //    Open();
  scanf("%lld", &n);
  for (LL i = 0; i < n; i++) {
    scanf("%lld%lld", &w[i], &l[i]);
    if (l[i] != -1)
      G[l[i]].push_back(i);
  }
  ans = 0;
  for (LL i = 0; i < n; i++) {
    if (l[i] == -1)
      dfs(i, -1);
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long LL;

int main() {
  int N;
  while (cin >> N) {
    vector<int> M(N);
    vector<int> L(N);
    vector<vector<int>> child(N);
    vector<int> degree(N);

    REP(i, N) {
      cin >> M[i] >> L[i];
      if (L[i] != -1) {
        child[L[i]].push_back(i);
        degree[L[i]]++;
      }
    }

    queue<int> que;
    REP(i, N) if (degree[i] == 0) que.push(i);

    vector<LL> cost(N);
    vector<int> weight(N);

    while (!que.empty()) {
      int u = que.front();
      que.pop();
      if (L[u] != -1 && --degree[L[u]] == 0) {
        que.push(L[u]);
      }
      vector<int> ws;
      cost[u] = 0;
      for (int c : child[u]) {
        ws.push_back(weight[c]);
        cost[u] += cost[c];
      }
      sort(ws.begin(), ws.end());
      LL tmp = 0;
      for (int w : ws) {
        cost[u] += tmp;
        tmp += w;
      }
      weight[u] = M[u] + accumulate(ws.begin(), ws.end(), 0LL);
    }

    LL ans = 0;
    REP(u, N) if (L[u] == -1) ans += cost[u];
    cout << ans << endl;
  }
  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long LL;

int main() {
  int N;
  while (cin >> N) {
    vector<int> M(N);
    vector<int> L(N);
    vector<vector<int>> child(N);
    vector<int> degree(N);

    REP(i, N) {
      cin >> M[i] >> L[i];
      if (L[i] != -1) {
        child[L[i]].push_back(i);
        degree[L[i]]++;
      }
    }

    queue<int> que;
    REP(i, N) if (degree[i] == 0) que.push(i);

    vector<LL> cost(N);
    vector<int> weight(N);

    while (!que.empty()) {
      int u = que.front();
      que.pop();
      if (--degree[L[u]] == 0) {
        que.push(L[u]);
      }
      vector<int> ws;
      for (int c : child[u]) {
        ws.push_back(weight[c]);
      }
      sort(ws.begin(), ws.end());
      cost[u] = 0;
      LL tmp = 0;
      for (int w : ws) {
        cost[u] += tmp;
        tmp += w;
      }
      weight[u] = M[u] + accumulate(ws.begin(), ws.end(), 0LL);
    }

    LL ans = 0;
    REP(u, N) if (L[u] == -1) ans += cost[u];
    cout << ans << endl;
  }
  return 0;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef long long ll;
vector<int> G[1000000];
vector<int> topo;
ll l[1000000], memo[1000000], ans = 0, out[1000000], p[1000000];
queue<int> S;
ll calc(int v) {
  if (memo[v] != -1)
    return memo[v];
  vector<ll> vc;
  for (ll u : G[v])
    vc.pb(calc(u));
  sort(all(vc));
  ll sum = 0, now = 0;
  for (ll k : vc) {
    sum += now;
    now += k;
  }
  ans += sum;
  return memo[v] = now + l[v];
}
int main() {
  int n;
  cin >> n;
  rep(i, n) memo[i] = -1;
  rep(i, n) {
    cin >> l[i] >> p[i];
    if (p[i] != -1) {
      G[p[i]].pb(i);
      out[p[i]]++;
    }
  }
  rep(i, n) if (out[i] == 0) S.push(i);
  while (!S.empty()) {
    int v = S.front();
    S.pop();
    topo.pb(v);
    if (p[v] != -1) {
      out[p[v]]--;
      if (out[p[v]] == 0)
        S.push(p[v]);
    }
  }
  rep(i, n) calc(topo[i]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef long long ll;
vector<int> G[1000000];
ll l[1000000], memo[1000000], ans = 0;
ll calc(int v) {
  if (memo[v] != -1)
    return memo[v];
  vector<ll> vc;
  for (ll u : G[v])
    vc.pb(calc(u));
  sort(all(vc));
  ll sum = 0, now = 0;
  for (ll k : vc) {
    sum += now;
    now += k;
  }
  ans += sum;
  return memo[v] = now + l[v];
}
int main() {
  int n;
  cin >> n;
  rep(i, n) memo[i] = -1;
  rep(i, n) {
    int p;
    cin >> l[i] >> p;
    if (p != -1)
      G[p].pb(i);
  }
  rep(i, n) calc(i);
  cout << ans << endl;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[1100000];
int par[1100000];
int len[1100000];
long long tl[1100000];
vector<long long> tmp[1100000];
int dep[1100000];
pair<int, int> ev[1100000];

int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    if (~q) {
      g[q].push_back(i);
      par[i] = -1;
    }
    len[i] = p;
  }
  long long res = 0;
  queue<int> Q;
  for (int i = 0; i < a; i++)
    if (~par[i])
      Q.push(i);
  while (Q.size()) {
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < g[at].size(); i++) {
      dep[g[at][i]] = dep[at] + 1;
      Q.push(g[at][i]);
    }
  }
  for (int i = 0; i < a; i++)
    ev[i] = make_pair(-dep[i], i);
  std::sort(ev, ev + a);
  for (int i = 0; i < a; i++) {
    int n = ev[i].second;
    int b = dep[n];
    tmp[b].clear();
    long long ret = 0;
    for (int i = 0; i < g[n].size(); i++) {
      tmp[b].push_back(tl[g[n][i]]);
      tl[n] += tl[g[n][i]];
    }
    tl[n] += len[n];
    std::sort(tmp[b].begin(), tmp[b].end());
    long long now = 0;
    for (int i = 0; i < tmp[b].size(); i++) {
      ret += now;
      now += tmp[b][i];
    }
    res += ret;
  }
  printf("%lld\\n", res);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[1100000];
int par[1100000];
int len[1100000];
long long tl[1100000];
vector<long long> tmp[1100000];
int dep[1100000];
stack<int> S;
inline long long solve() {
  int a = S.top();
  int b = dep[a];
  tmp[b].clear();
  long long ret = 0;
  for (int i = 0; i < g[a].size(); i++) {
    S.push(g[a][i]);
    ret += solve();
    S.pop();
    tmp[b].push_back(tl[g[a][i]]);
    tl[a] += tl[g[a][i]];
  }
  tl[a] += len[a];
  std::sort(tmp[b].begin(), tmp[b].end());
  long long now = 0;
  for (int i = 0; i < tmp[b].size(); i++) {
    ret += now;
    now += tmp[b][i];
  }
  return ret;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    if (~q) {
      g[q].push_back(i);
      par[i] = -1;
    }
    len[i] = p;
  }
  long long ret = 0;
  queue<int> Q;
  for (int i = 0; i < a; i++)
    if (~par[i])
      Q.push(i);
  while (Q.size()) {
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < g[at].size(); i++) {
      dep[g[at][i]] = dep[at] + 1;
      Q.push(g[at][i]);
    }
  }
  for (int i = 0; i < a; i++)
    if (~par[i]) {
      S.push(i);
      ret += solve();
      S.pop();
    }
  printf("%lld\\n", ret);
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

int main() {
  int n;
  scanf("%d", &n);
  static vector<int> G[1000000], G_rev[1000000];
  static int cost[1000000];
  rep(u, n) {
    int v;
    scanf("%d%d", cost + u, &v);
    if (v != -1) {
      G[v].push_back(u);
      G_rev[u].push_back(v);
    }
  }

  vector<int> topo; // topological order
  queue<int> Q;
  rep(u, n) if (G_rev[u].empty()) {
    Q.push(u);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      topo.push_back(v);
      rep(i, G[v].size()) Q.push(G[v][i]);
    }
  }

  ll ans = 0;
  for (int i = (int)topo.size() - 1; i >= 0; i--) {
    int u = topo[i];

    vector<int> C(G[u].size()); // 頂点 u の子たちの cost
    rep(j, G[u].size()) C[j] = cost[G[u][j]];
    sort(C.begin(), C.end());

    ll sum = 0;
    rep(j, C.size()) {
      ans += sum;
      sum += C[j];
    }
    cost[u] += sum;
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

int main() {
  int n;
  scanf("%d", &n);
  static vector<int> G[160000], G_rev[160000];
  static int cost[160000];
  rep(u, n) {
    int v;
    scanf("%d%d", cost + u, &v);
    if (v != -1) {
      G[v].push_back(u);
      G_rev[u].push_back(v);
    }
  }

  vector<int> topo; // topological order
  queue<int> Q;
  rep(u, n) if (G_rev[u].empty()) {
    Q.push(u);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      topo.push_back(v);
      rep(i, G[v].size()) Q.push(G[v][i]);
    }
  }

  ll ans = 0;
  for (int i = (int)topo.size() - 1; i >= 0; i--) {
    int u = topo[i];

    vector<int> C(G[u].size()); // 頂点 u の子たちの cost
    rep(j, G[u].size()) C[j] = cost[G[u][j]];
    sort(C.begin(), C.end());

    ll sum = 0;
    rep(j, C.size()) {
      ans += sum;
      sum += C[j];
    }
    cost[u] += sum;
  }

  printf("%lld\\n", ans);

  return 0;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
  for (int i = 0; i < g[v].size(); i++)
    l[v] += DFS(g[v][i]);
  return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d%d", &l[i], &b);
    if (b != -1) {
      g[b].push_back(i);
      vis[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      S[0] = i;
      stage[0] = 0;
      top = 0;
      while (top != -1) {
        int cur = S[top];
        if (stage[top] == g[cur].size()) {
          top--;
          if (top != -1) {
            l[S[top]] += l[S[top + 1]];
            // printf("%d-%d\\n",S[top],S[top+1]);
          }
        } else {
          S[top + 1] = g[cur][stage[top]];
          stage[top]++;
          stage[++top] = 0;
        }
      }
    }
  for (int i = 0; i < n; i++)
    sort(g[i].begin(), g[i].end(), cmp);
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = 0; j < g[i].size(); j++) {
      ans += sum;
      sum += l[g[i][j]];
    }
  }
  // for (int i=0;i<n;i++) printf("%d ",l[i]);
  // printf("\\n");
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
  for (int i = 0; i < g[v].size(); i++)
    l[v] += DFS(g[v][i]);
  return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d%d", &l[i], &b);
    if (b != -1) {
      g[b].push_back(i);
      vis[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      S[0] = i;
      stage[0] = i;
      top = 0;
      while (top != -1) {
        int cur = S[top];
        if (stage[top] == g[cur].size()) {
          top--;
          if (top != -1) {
            l[S[top]] += l[S[top + 1]];
            // printf("%d-%d\\n",S[top],S[top+1]);
          }
        } else {
          S[top + 1] = g[cur][stage[top]];
          stage[top]++;
          stage[++top] = 0;
        }
      }
    }
  for (int i = 0; i < n; i++)
    sort(g[i].begin(), g[i].end(), cmp);
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = 0; j < g[i].size(); j++) {
      ans += sum;
      sum += l[g[i][j]];
    }
  }
  // for (int i=0;i<n;i++) printf("%d ",l[i]);
  // printf("\\n");
  printf("%lld\\n", ans);
  return 0;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

vector<int> children[1234567];
int parents[1234567];
int M[1234567];
vector<int> l[1234567];
long long c[1234567];
int sl[1234567];

pair<int, long long> dfs(int v) {
  vector<int> l;
  long long c = 0;
  for (auto e : children[v]) {
    auto r = dfs(e);
    c += r.second;
    l.push_back(r.first);
  }
  sort(begin(l), end(l));
  int sl = M[v];
  for (int i = 0; i < l.size(); i++) {
    c += l[l.size() - 1 - i] * 1LL * i;
    sl += l[i];
  }
  return make_pair(sl, c);
}

int main() {
  int N;
  cin >> N;
  vector<int> roots;
  for (int i = 0; i < N; i++) {
    int L;
    cin >> M[i] >> L;
    parents[i] = L;
    if (L == -1) {
      roots.push_back(i);
    } else {
      children[L].push_back(i);
    }
  }
  vector<int> to;
  for (auto e : roots) {
    stack<pair<int, int>> stk;
    stk.push(make_pair(e, 0));
    while (!stk.empty()) {
      auto c = stk.top();
      stk.pop();
      if (children[c.first].size() == c.second) {
        to.push_back(c.first);
      } else {
        stk.push(make_pair(c.first, c.second + 1));
        stk.push(make_pair(children[c.first][c.second], 0));
      }
    }
  }
  for (auto e : to) {
    sort(begin(l[e]), end(l[e]));
    sl[e] = M[e];
    for (int i = 0; i < l[e].size(); i++) {
      c[e] += l[e][l[e].size() - 1 - i] * 1LL * i;
      sl[e] += l[e][i];
    }
    if (parents[e] != -1) {
      l[parents[e]].push_back(sl[e]);
      c[parents[e]] += c[e];
    }
  }
  long long ans = 0;
  for (auto e : roots) {
    ans += c[e];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

vector<int> children[1234567];
int M[1234567];

vector<int> l[1234567];
long long c[1234567];
int sl[1234567];

pair<int, long long> dfs(int v) {
  vector<int> &l = ::l[v];
  long long &c = ::c[v];
  for (auto e : children[v]) {
    auto r = dfs(e);
    c += r.second;
    l.push_back(r.first);
  }
  sort(begin(l), end(l));
  int &sl = ::sl[v];
  sl = M[v];
  for (int i = 0; i < l.size(); i++) {
    c += l[l.size() - 1 - i] * 1LL * i;
    sl += l[i];
  }
  return make_pair(sl, c);
}

int main() {
  int N;
  cin >> N;
  vector<int> roots;
  for (int i = 0; i < N; i++) {
    int L;
    cin >> M[i] >> L;
    if (L == -1) {
      roots.push_back(i);
    } else {
      children[L].push_back(i);
    }
  }
  long long ans = 0;
  for (auto e : roots) {
    ans += dfs(e).second;
  }
  cout << ans << endl;
}
Problem p01570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef long long ll;

ll len[1000001];
vector<int> child[1000001];

void bfs(int v, vector<int> &order) {
  queue<int> q;
  q.push(v);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    order.push_back(v);
    for (int i = 0; i < child[v].size(); ++i)
      q.push(child[v][i]);
  }
}

int main() {
  int n, m;
  cin >> n;
  vector<int> root;
  for (int i = 0; i < n; ++i) {
    cin >> len[i] >> m;
    if (m != -1)
      child[m].push_back(i);
    else
      root.push_back(i);
  }
  vector<int> order;
  ll ans = 0;
  for (int i = 0; i < root.size(); ++i)
    bfs(root[i], order);
  reverse(order.begin(), order.end());
  for (int i = 0; i < order.size(); ++i) {
    int v = order[i];
    const int n = child[v].size();
    vector<ll> l(n);
    for (int p = 0; p < n; ++p)
      l[p] = len[child[v][p]];
    sort(l.begin(), l.end());
    for (int p = 0; p < n; ++p) {
      ans += (n - p - 1) * l[p];
      // cout<<v<<" "<<l[p]<<" "<<(n-p-1) * l[p]<<endl;
      len[v] += l[p];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

int len[160001];

ll rec(int v, const vector<vector<int>> &child) {
  const int n = child[v].size();
  if (n == 0)
    return 0;
  ll ret = 0;
  vector<int> l(n);
  for (int i = 0; i < n; ++i) {
    l[i] = len[child[v][i]];
    ret += rec(child[v][i], child);
  }
  sort(l.begin(), l.end());
  for (int i = 0; i < l.size(); ++i)
    ret += (n - i - 1) * l[i];
  return ret;
}

int main() {
  int n, m;
  cin >> n;
  vector<vector<int>> child(n, vector<int>());
  vector<int> root;
  for (int i = 0; i < n; ++i) {
    cin >> len[i] >> m;
    if (m != -1)
      child[m].push_back(i);
    else
      root.push_back(i);
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    if (child[i].size() == 0)
      continue;
    vector<int> l(n);
    for (int j = 0; j < child[i].size(); ++j)
      l[j] = len[child[i][j]];
    sort(l.begin(), l.end());
    for (int j = 0; j < l.size(); ++j) {
      ans += (l.size() - j - 1) * l[j];
    }
  }
  cout << ans << endl;
}