Problem p03941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1LL << 61;
using ll = long long;

typedef pair<ll, ll> ii;

vector<ii> AdjList[140005];

long long W;

vector<ii> S[140005][2];
vector<ll> prefixMin[140005][2];

void dfs(int u) {
  if (AdjList[u].empty()) {
    S[u][0].emplace_back(0, 0);
    S[u][1].emplace_back(0, 0);
    prefixMin[u][0].push_back(0);
    prefixMin[u][1].push_back(0);
    return;
  }

  int l = AdjList[u][0].first;
  long long w_l = AdjList[u][0].second;
  int r = AdjList[u][1].first;
  long long w_r = AdjList[u][1].second;
  dfs(l);
  dfs(r);

  vector<int> set1;

  int indxC = (int)S[r][0].size() - 1;
  int indxD = (int)S[r][1].size() - 1;
  for (ii temp1 : S[l][0]) {
    long long a, b;
    tie(a, b) = temp1;
    long long tempC = INF;
    long long tempD = INF;

    while (indxD >= 0) {
      ii temp2 = S[r][1][indxD];
      long long c, d;
      tie(d, c) = temp2;
      if (d <= W - a - w_l - w_r) {
        tempC = prefixMin[r][1][indxD] + w_r; // to change
        break;
      } else {
        indxD--;
      }
    }
    if (indxD == -1) {
      break;
    }

    if (tempC < INF) {
      S[u][0].emplace_back(tempC, b + w_l);
      S[u][1].emplace_back(b + w_l, tempC);
      if (u == 1) {
        break;
      }
    }
  }

  for (ii temp1 : S[l][1]) {
    long long a, b;
    tie(b, a) = temp1;
    long long tempD = INF;

    while (indxC >= 0) {
      ii temp2 = S[r][0][indxC];
      long long c, d;
      tie(c, d) = temp2;
      if (c <= W - b - w_l - w_r) {
        tempD = prefixMin[r][0][indxC] + w_r; // to change
        break;
      } else {
        indxC--;
      }
    }
    if (indxC == -1) {
      break;
    }

    if (tempD < INF) {
      S[u][0].emplace_back(a + w_l, tempD);
      S[u][1].emplace_back(tempD, a + w_l);
      if (u == 1) {
        break;
      }
    }
  }

  if (u == 1) {
    return;
  }

  sort(S[u][0].begin(), S[u][0].end());
  sort(S[u][1].begin(), S[u][1].end());

  int tempSize = S[u][0].size();
  prefixMin[u][0].assign(tempSize, 0);
  prefixMin[u][1].assign(tempSize, 0);

  for (int x = 0; x < 2; x++) {
    long long tempMin = INF;
    long long tempMax = 0;
    int cnt = 0;
    for (ii temp : S[u][x]) {
      tempMin = min(temp.second, tempMin);
      prefixMin[u][x][cnt++] = tempMin;
    }
  }
}

int N;

bool boleh(long long W2) {
  W = W2;
  for (int i = 1; i <= N; i++) {
    S[i][0].clear();
    S[i][1].clear();
  }
  dfs(1);
  return !S[1][0].empty();
}

int main() {
  scanf("%d", &N);

  for (int i = 2; i <= N; i++) {
    int a, v;
    scanf("%d%d", &a, &v);
    AdjList[a].push_back(ii(i, v));
  }

  long long lo = 0;
  long long hi = 1LL << 35;
  while (lo < hi) {
    long long mid = (lo + hi) >> 1;
    if (boleh(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }

  printf("%lld", lo);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1LL << 61;
using ll = long long;

typedef pair<ll, ll> ii;

vector<ii> AdjList[140005];

long long W;

vector<ii> S[140005][2];
vector<ll> prefixMin[140005][2];

void dfs(int u) {
  if (AdjList[u].empty()) {
    S[u][0].emplace_back(0, 0);
    S[u][1].emplace_back(0, 0);
    prefixMin[u][0].push_back(0);
    prefixMin[u][1].push_back(0);
    return;
  }

  int l = AdjList[u][0].first;
  long long w_l = AdjList[u][0].second;
  int r = AdjList[u][1].first;
  long long w_r = AdjList[u][1].second;
  dfs(l);
  dfs(r);

  vector<int> set1;

  int indxC = (int)S[r][0].size() - 1;
  int indxD = (int)S[r][1].size() - 1;
  for (ii temp1 : S[l][0]) {
    long long a, b;
    tie(a, b) = temp1;
    long long tempC = INF;
    long long tempD = INF;

    while (indxD >= 0) {
      ii temp2 = S[r][1][indxD];
      long long c, d;
      tie(d, c) = temp2;
      if (d <= W - a - w_l - w_r) {
        tempC = prefixMin[r][1][indxD] + w_r; // to change
        break;
      } else {
        indxD--;
      }
    }
    if (indxD == -1) {
      break;
    }

    if (tempC < INF) {
      S[u][0].emplace_back(tempC, b + w_l);
      S[u][1].emplace_back(b + w_l, tempC);
      if (u == 1) {
        break;
      }
    }
  }

  for (ii temp1 : S[l][1]) {
    long long a, b;
    tie(b, a) = temp1;
    long long tempD = INF;

    while (indxC >= 0) {
      ii temp2 = S[r][0][indxC];
      long long c, d;
      tie(c, d) = temp2;
      if (c <= W - b - w_l - w_r) {
        tempD = prefixMin[r][0][indxC] + w_r; // to change
        break;
      } else {
        indxC--;
      }
    }
    if (indxC == -1) {
      break;
    }

    if (tempD < INF) {
      S[u][0].emplace_back(a + w_l, tempD);
      S[u][1].emplace_back(tempD, a + w_l);
      if (u == 1) {
        break;
      }
    }
  }

  if (u == 1) {
    return;
  }

  sort(S[u][0].begin(), S[u][0].end());
  sort(S[u][1].begin(), S[u][1].end());

  int tempSize = S[u][0].size();
  prefixMin[u][0].assign(tempSize, 0);
  prefixMin[u][1].assign(tempSize, 0);

  for (int x = 0; x < 2; x++) {
    long long tempMin = INF;
    long long tempMax = 0;
    int cnt = 0;
    for (ii temp : S[u][x]) {
      tempMin = min(temp.second, tempMin);
      prefixMin[u][x][cnt++] = tempMin;
    }
  }
}

int N;

bool boleh(long long W2) {
  W = W2;
  for (int i = 1; i <= N; i++) {
    S[i][0].clear();
    S[i][1].clear();
  }
  dfs(1);
  return !S[1][0].empty();
}

int main() {
  scanf("%d", &N);

  for (int i = 2; i <= N; i++) {
    int a, v;
    scanf("%d%d", &a, &v);
    AdjList[a].push_back(ii(i, v));
  }

  long long lo = 0;
  long long hi = 1LL << 23;
  while (lo < hi) {
    long long mid = (lo + hi) >> 1;
    if (boleh(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }

  printf("%lld", lo);
  return 0;
}