Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define y1 y11
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define NAME ""

using namespace std;

typedef long long ll;
typedef long double ld;

const ld PI = acos(-1.0);

const int MAXN = 300000;

const ll INF = 2ll * (ll)MAXN * (ll)MAXN;

vector<pair<ll, ll>> ans[MAXN];
int l[MAXN], r[MAXN];
int c[MAXN];
vector<pair<ll, ll>> v1;
vector<pair<ll, ll>> v2;
vector<pair<ll, ll>> n1;
vector<pair<ll, ll>> n2;
pair<ll, ll> tmp[10 * MAXN];
int sz[MAXN];

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    l[i] = r[i] = -1;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p >> c[i];
    if (l[p] != -1)
      r[p] = i;
    else
      l[p] = i;
  }
  ll lg = 0, rg = INF;
  while (lg < rg) {
    ll md = (lg + rg) / 2;
    bool fail = false;
    for (int i = n; i >= 1; i--) {
      if (fail)
        continue;
      ans[i].clear();
      if (l[i] == -1) {
        sz[i] = 1;
        ans[i].pb(mp(0, 0));
        continue;
      }
      v1 = ans[l[i]];
      v2 = ans[r[i]];
      sz[i] = sz[l[i]] + sz[r[i]];
      int c1 = v1.size(), c2 = v2.size();
      n1.clear();
      n2.clear();
      for (int j = 0; j < c1; j++)
        v1[j].fs += c[l[i]], v1[j].sc += c[l[i]];
      for (int j = 0; j < c2; j++)
        v2[j].fs += c[r[i]], v2[j].sc += c[r[i]];
      int p = 0;
      for (int j = 0; j < c1; j++) {
        while ((p < c2) && (v1[j].sc + v2[p].fs <= md))
          p++;
        if (p == 0)
          continue;
        n1.pb(mp(v1[j].fs, v2[p - 1].sc));
      }
      p = 0;
      for (int j = 0; j < c2; j++) {
        while ((p < c1) && (v2[j].sc + v1[p].fs <= md))
          p++;
        if (p == 0)
          continue;
        n2.pb(mp(v2[j].fs, v1[p - 1].sc));
      }
      c1 = n1.size(), c2 = n2.size();
      int tot = c1 + c2;
      int p1 = 0, p2 = 0;
      n1.pb(mp(INF, 0));
      n2.pb(mp(INF, 0));
      for (int j = 0; j < tot; j++) {
        if (n1[p1] < n2[p2])
          tmp[j] = n1[p1++];
        else
          tmp[j] = n2[p2++];
      }
      for (int j = tot - 1; j > 0; j--) {
        if (tmp[j].fs == tmp[j - 1].fs)
          tmp[j - 1].sc = min(tmp[j - 1].sc, tmp[j].sc);
      }
      ll cp = INF;
      for (int j = 0; j < tot; j++) {
        if (tmp[j].sc < cp) {
          cp = tmp[j].sc;
          ans[i].pb(tmp[j]);
        }
      }
      if (ans[i].size() == 0)
        fail = true;
    }
    if (fail)
      lg = md + 1;
    else
      rg = md;
  }
  cout << lg << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define y1 y11
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define NAME ""

using namespace std;

typedef long long ll;
typedef long double ld;

const ld PI = acos(-1.0);

const int MAXN = 300000;

const ll INF = 2ll * (ll)MAXN * (ll)MAXN;

vector<pair<ll, ll>> ans[MAXN];
int l[MAXN], r[MAXN];
int c[MAXN];
vector<pair<ll, ll>> v1;
vector<pair<ll, ll>> v2;
vector<pair<ll, ll>> n1;
vector<pair<ll, ll>> n2;
pair<ll, ll> tmp[10 * MAXN];
int sz[MAXN];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    l[i] = r[i] = -1;
  for (int i = 2; i <= n; i++) {
    int p;
    cin >> p >> c[i];
    if (l[p] != -1)
      r[p] = i;
    else
      l[p] = i;
  }
  ll lg = 0, rg = INF;
  while (lg < rg) {
    ll md = (lg + rg) / 2;
    bool fail = false;
    for (int i = n; i >= 1; i--) {
      if (fail)
        continue;
      ans[i].clear();
      if (l[i] == -1) {
        sz[i] = 1;
        ans[i].pb(mp(0, 0));
        continue;
      }
      v1 = ans[l[i]];
      v2 = ans[r[i]];
      sz[i] = sz[l[i]] + sz[r[i]];
      int c1 = v1.size(), c2 = v2.size();
      n1.clear();
      n2.clear();
      for (int j = 0; j < c1; j++)
        v1[j].fs += c[l[i]], v1[j].sc += c[l[i]];
      for (int j = 0; j < c2; j++)
        v2[j].fs += c[r[i]], v2[j].sc += c[r[i]];
      int p = 0;
      for (int j = 0; j < c1; j++) {
        while ((p < c2) && (v1[j].sc + v2[p].fs <= md))
          p++;
        if (p == 0)
          continue;
        n1.pb(mp(v1[j].fs, v2[p - 1].sc));
      }
      p = 0;
      for (int j = 0; j < c2; j++) {
        while ((p < c1) && (v2[j].sc + v1[p].fs <= md))
          p++;
        if (p == 0)
          continue;
        n2.pb(mp(v2[j].fs, v1[p - 1].sc));
      }
      c1 = n1.size(), c2 = n2.size();
      int tot = c1 + c2;
      int p1 = 0, p2 = 0;
      n1.pb(mp(INF, 0));
      n2.pb(mp(INF, 0));
      for (int j = 0; j < tot; j++) {
        if (n1[p1] < n2[p2])
          tmp[j] = n1[p1++];
        else
          tmp[j] = n2[p2++];
      }
      for (int j = tot - 1; j > 0; j--) {
        if (tmp[j].fs == tmp[j - 1].fs)
          tmp[j - 1].sc = min(tmp[j - 1].sc, tmp[j].sc);
      }
      ll cp = INF;
      for (int j = 0; j < tot; j++) {
        if (tmp[j].sc < cp) {
          cp = tmp[j].sc;
          ans[i].pb(tmp[j]);
        }
      }
      if (ans[i].size() == 0)
        fail = true;
    }
    if (fail)
      lg = md + 1;
    else
      rg = md;
  }
  cout << lg << endl;
  return 0;
}