Problem p03941 (C++)

Buggy submission (status: Runtime Error):
// Be Name Khoda
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma optimize O3
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

typedef long long int LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define RETD(x)                                                                \
  {                                                                            \
    cout << fixed << setprecision(15) << x;                                    \
    exit(0);                                                                   \
  }
const ll M = 1e5 + 5, LG = 32, SM = 600 + 5, inf = 1e18 + 5;
ll mod = 1e9 + 7;

ll n;
ll lf[M], rf[M];
ll parv[M];
vector<pll> s[M];
ll V = 0;

bool dfs(ll x) {
  s[x].clear();
  if (lf[x] == -1) {
    s[x].PB({0, 0});
    return 1;
  }
  ll a = lf[x], b = rf[x];
  if (!dfs(a) || !dfs(b))
    return 0;
  For(i, 0, SZ(s[a])) {
    s[a][i].X += parv[a];
    s[a][i].Y += parv[a];
  }
  For(i, 0, SZ(s[b])) {
    s[b][i].X += parv[b];
    s[b][i].Y += parv[b];
  }
  if (SZ(s[a]) > SZ(s[b]))
    swap(a, b);
  vector<pll> sx;
  For(i, 0, SZ(s[a])) {
    ll l = -1, r = SZ(s[b]);
    while (r - l > 1) {
      ll mid = (l + r) >> 1;
      if (s[a][i].X + s[b][mid].Y <= V)
        r = mid;
      else
        l = mid;
    }
    if (r != SZ(s[b]))
      sx.PB({s[b][r].X, s[a][i].Y});
    l = -1, r = SZ(s[b]);
    while (r - l > 1) {
      ll mid = (l + r) >> 1;
      if (s[a][i].Y + s[b][mid].X <= V)
        l = mid;
      else
        r = mid;
    }
    if (l != -1)
      sx.PB({s[a][i].X, s[b][l].Y});
  }
  if (SZ(sx) == 0)
    return 0;
  sort(sx.begin(), sx.end());
  s[x].PB(sx[0]);
  For(i, 1, SZ(sx)) {
    if (sx[i].Y < s[x].back().Y) {
      s[x].PB(sx[i]);
    }
  }
  return 1;
}

int main() {
  cin >> n;
  memset(lf, -1, sizeof lf);
  memset(rf, -1, sizeof rf);
  For(i, 1, n) {
    ll p;
    cin >> p >> parv[i];
    p--;
    if (lf[p] != -1)
      rf[p] = i;
    else
      lf[p] = i;
  }
  ll l = -1, r = 1e18;
  while (r - l > 1) {
    V = (l + r) >> 1;
    if (dfs(0))
      r = V;
    else
      l = V;
  }
  RET(r);
}


Fixed submission:
// Be Name Khoda
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma optimize O3
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

typedef long long int LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define RETD(x)                                                                \
  {                                                                            \
    cout << fixed << setprecision(15) << x;                                    \
    exit(0);                                                                   \
  }
const ll M = 2e5 + 5, LG = 32, SM = 600 + 5, inf = 1e18 + 5;
ll mod = 1e9 + 7;

ll n;
ll lf[M], rf[M];
ll parv[M];
vector<pll> s[M];
ll V = 0;

bool dfs(ll x) {
  s[x].clear();
  if (lf[x] == -1) {
    s[x].PB({0, 0});
    return 1;
  }
  ll a = lf[x], b = rf[x];
  if (!dfs(a) || !dfs(b))
    return 0;
  For(i, 0, SZ(s[a])) {
    s[a][i].X += parv[a];
    s[a][i].Y += parv[a];
  }
  For(i, 0, SZ(s[b])) {
    s[b][i].X += parv[b];
    s[b][i].Y += parv[b];
  }
  if (SZ(s[a]) > SZ(s[b]))
    swap(a, b);
  vector<pll> sx;
  For(i, 0, SZ(s[a])) {
    ll l = -1, r = SZ(s[b]);
    while (r - l > 1) {
      ll mid = (l + r) >> 1;
      if (s[a][i].X + s[b][mid].Y <= V)
        r = mid;
      else
        l = mid;
    }
    if (r != SZ(s[b]))
      sx.PB({s[b][r].X, s[a][i].Y});
    l = -1, r = SZ(s[b]);
    while (r - l > 1) {
      ll mid = (l + r) >> 1;
      if (s[a][i].Y + s[b][mid].X <= V)
        l = mid;
      else
        r = mid;
    }
    if (l != -1)
      sx.PB({s[a][i].X, s[b][l].Y});
  }
  if (SZ(sx) == 0)
    return 0;
  sort(sx.begin(), sx.end());
  s[x].PB(sx[0]);
  For(i, 1, SZ(sx)) {
    if (sx[i].Y < s[x].back().Y) {
      s[x].PB(sx[i]);
    }
  }
  return 1;
}

int main() {
  cin >> n;
  memset(lf, -1, sizeof lf);
  memset(rf, -1, sizeof rf);
  For(i, 1, n) {
    ll p;
    cin >> p >> parv[i];
    p--;
    if (lf[p] != -1)
      rf[p] = i;
    else
      lf[p] = i;
  }
  ll l = -1, r = 1e18;
  while (r - l > 1) {
    V = (l + r) >> 1;
    if (dfs(0))
      r = V;
    else
      l = V;
  }
  RET(r);
}