Problem p03941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mo 998244353
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
long long n, i, j, x, y, l, r, mid, min1, flag, top2, b[300005], c[300005];
vector<pair<long long, long long>> a[300005];
long long top, qu[300005], nex[300005], to[300005], fir[300005];
void lj(int u, int v, long long w) {
  top++;
  nex[top] = fir[u];
  fir[u] = top;
  to[top] = v;
  qu[top] = w;
}
void ss(int v, long long w) {
  if (flag == 1)
    return;
  if (fir[v] == 0) {
    a[v].insert(a[v].end(), make_pair(w, w));
    return;
  }
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    ss(to[top1], qu[top1]);
  if (flag == 1)
    return;
  int l = to[fir[v]], r = to[nex[fir[v]]];
  if (a[l].size() > a[r].size())
    swap(a[l], a[r]);
  long long m1 = a[l].size(), m2 = a[r].size();
  if (v == 1) {
    if (a[l][0].first + a[r][0].first <= mid ||
        a[l][0].first + a[r][m2 - 1].second <= mid ||
        a[l][m1 - 1].second + a[r][0].first <= mid ||
        a[l][m1 - 1].second + a[r][m2 - 1].second <= mid)
      return;
    else {
      flag = 1;
      return;
    }
  }
  j = m2 - 1;
  top2 = 0;
  for (i = 0; i < m1; i++) {
    while (a[l][i].first + a[r][j].first > mid) {
      j--;
      if (j == -1)
        break;
    }
    if (j == -1)
      break;
    top2++;
    b[top2] = a[r][j].second;
    c[top2] = a[l][i].second;
  }
  for (i = 1; i <= top2; i++) {
    b[top2 + i] = c[top2 - i + 1];
    c[top2 + i] = b[top2 - i + 1];
  }
  i = 1;
  j = top2 + 1;
  long long min2 = 1000000000000LL;
  while (i <= top2 && j <= (top2 << 1)) {
    if (b[i] < b[j]) {
      if (c[i] + w < min2) {
        a[v].insert(a[v].end(), make_pair(b[i] + w, c[i] + w));
        min2 = c[i] + w;
      }
      i++;
    } else {
      if (c[j] + w < min2) {
        a[v].insert(a[v].end(), make_pair(b[j] + w, c[j] + w));
        min2 = c[j] + w;
      }
      j++;
    }
  }
  while (i <= top2) {
    if (c[i] + w < min2) {
      a[v].insert(a[v].end(), make_pair(b[i] + w, c[i] + w));
      min2 = c[i] + w;
    }
    i++;
  }
  while (j <= (top2 << 1)) {
    if (c[j] + w < min2) {
      a[v].insert(a[v].end(), make_pair(b[j] + w, c[j] + w));
      min2 = c[j] + w;
    }
    j++;
  }
  if (a[v].size() == 0)
    flag = 1;
}
int main() {
  freopen("a.in", "r", stdin);
  n = read();
  for (i = 2; i <= n; i++) {
    x = read();
    y = read();
    lj(x, i, y);
  }
  l = 0;
  r = 1000000000000LL;
  mid = (l + r >> 1);
  min1 = r;
  while (l <= r) {
    flag = 0;
    ss(1, 0);
    if (flag == 1)
      l = mid + 1;
    else {
      r = mid - 1;
      min1 = min(min1, mid);
    }
    mid = (l + r >> 1);
    for (i = 1; i <= n; i++)
      a[i].clear();
  }
  pus(min1, 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mo 998244353
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
long long n, i, j, x, y, l, r, mid, min1, flag, top2, b[300005], c[300005];
vector<pair<long long, long long>> a[300005];
long long top, qu[300005], nex[300005], to[300005], fir[300005];
void lj(int u, int v, long long w) {
  top++;
  nex[top] = fir[u];
  fir[u] = top;
  to[top] = v;
  qu[top] = w;
}
void ss(int v, long long w) {
  if (flag == 1)
    return;
  if (fir[v] == 0) {
    a[v].insert(a[v].end(), make_pair(w, w));
    return;
  }
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    ss(to[top1], qu[top1]);
  if (flag == 1)
    return;
  int l = to[fir[v]], r = to[nex[fir[v]]];
  if (a[l].size() > a[r].size())
    swap(a[l], a[r]);
  long long m1 = a[l].size(), m2 = a[r].size();
  if (v == 1) {
    if (a[l][0].first + a[r][0].first <= mid ||
        a[l][0].first + a[r][m2 - 1].second <= mid ||
        a[l][m1 - 1].second + a[r][0].first <= mid ||
        a[l][m1 - 1].second + a[r][m2 - 1].second <= mid)
      return;
    else {
      flag = 1;
      return;
    }
  }
  j = m2 - 1;
  top2 = 0;
  for (i = 0; i < m1; i++) {
    while (a[l][i].first + a[r][j].first > mid) {
      j--;
      if (j == -1)
        break;
    }
    if (j == -1)
      break;
    top2++;
    b[top2] = a[r][j].second;
    c[top2] = a[l][i].second;
  }
  for (i = 1; i <= top2; i++) {
    b[top2 + i] = c[top2 - i + 1];
    c[top2 + i] = b[top2 - i + 1];
  }
  i = 1;
  j = top2 + 1;
  long long min2 = 1000000000000LL;
  while (i <= top2 && j <= (top2 << 1)) {
    if (b[i] < b[j]) {
      if (c[i] + w < min2) {
        a[v].insert(a[v].end(), make_pair(b[i] + w, c[i] + w));
        min2 = c[i] + w;
      }
      i++;
    } else {
      if (c[j] + w < min2) {
        a[v].insert(a[v].end(), make_pair(b[j] + w, c[j] + w));
        min2 = c[j] + w;
      }
      j++;
    }
  }
  while (i <= top2) {
    if (c[i] + w < min2) {
      a[v].insert(a[v].end(), make_pair(b[i] + w, c[i] + w));
      min2 = c[i] + w;
    }
    i++;
  }
  while (j <= (top2 << 1)) {
    if (c[j] + w < min2) {
      a[v].insert(a[v].end(), make_pair(b[j] + w, c[j] + w));
      min2 = c[j] + w;
    }
    j++;
  }
  if (a[v].size() == 0)
    flag = 1;
}
int main() {
  n = read();
  for (i = 2; i <= n; i++) {
    x = read();
    y = read();
    lj(x, i, y);
  }
  l = 0;
  r = 1000000000000LL;
  mid = (l + r >> 1);
  min1 = r;
  while (l <= r) {
    flag = 0;
    ss(1, 0);
    if (flag == 1)
      l = mid + 1;
    else {
      r = mid - 1;
      min1 = min(min1, mid);
    }
    mid = (l + r >> 1);
    for (i = 1; i <= n; i++)
      a[i].clear();
  }
  pus(min1, 2);
  return 0;
}