Problem p03941 (C++)

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

#define MAXN 150005

typedef long long lint;

using namespace std;

int n;
int ch[MAXN][2], val[MAXN][2], size[MAXN];

struct node {
  lint p, q;
  node(lint p = 0, lint q = 0) : p(p), q(q) {}

  bool operator<(const node b) const {
    return p < b.p || (p == b.p && q < b.q);
  }
};

vector<node> a[MAXN];

int read() {
  char c = getchar();
  int x = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x;
}

void merge(lint limit, vector<node> &u, vector<node> &v, vector<node> &w) {
  if (!v.size() || !u.size())
    return;
  for (int i = 0, j = 0; i < (int)u.size(); ++i) {
    while (j + 1 < (int)v.size() && u[i].q + v[j + 1].p <= limit)
      ++j;
    if (u[i].q + v[j].p <= limit) {
      node temp(u[i].p, v[j].q);
      if (w.size() && temp.p >= w[w.size() - 1].p &&
          temp.q >= w[w.size() - 1].q)
        continue;
      w.push_back(temp);
    }
  }
}

void DFS(int u, lint limit) {
  a[u].clear();
  if (!size[u]) {
    a[u].push_back(node());
    return;
  }
  for (int i = 0; i < 2; ++i) {
    int v = ch[u][i], w = val[u][i];
    DFS(v, limit);
    for (int j = 0; j < (int)a[v].size(); ++j) {
      a[v][j].p += w;
      a[v][j].q += w;
    }
  }
  int ls = ch[u][0], rs = ch[u][1];
  sort(a[ls].begin(), a[ls].end());
  sort(a[rs].begin(), a[rs].end());
  merge(limit, a[ls], a[rs], a[u]);
  merge(limit, a[rs], a[ls], a[u]);
}

bool check(lint limit) {
  DFS(1, limit);
  return a[1].size();
}

int solve() {
  lint l = 0, r = 1ll << 34, res = -1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return res;
}

int main() {
  n = read();
  for (int i = 2; i <= n; ++i) {
    int f = read(), v = read();
    ch[f][size[f]] = i;
    val[f][size[f]] = v;
    ++size[f];
  }
  lint ans = solve();
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>

#define MAXN 150005

typedef long long lint;

using namespace std;

int n;
int ch[MAXN][2], val[MAXN][2], size[MAXN];

struct node {
  lint p, q;
  node(lint p = 0, lint q = 0) : p(p), q(q) {}

  bool operator<(const node b) const {
    return p < b.p || (p == b.p && q < b.q);
  }
};

vector<node> a[MAXN];

int read() {
  char c = getchar();
  int x = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x;
}

void merge(lint limit, vector<node> &u, vector<node> &v, vector<node> &w) {
  if (!v.size() || !u.size())
    return;
  for (int i = 0, j = 0; i < (int)u.size(); ++i) {
    while (j + 1 < (int)v.size() && u[i].q + v[j + 1].p <= limit)
      ++j;
    if (u[i].q + v[j].p <= limit) {
      node temp(u[i].p, v[j].q);
      if (w.size() && temp.p >= w[w.size() - 1].p &&
          temp.q >= w[w.size() - 1].q)
        continue;
      w.push_back(temp);
    }
  }
}

void DFS(int u, lint limit) {
  a[u].clear();
  if (!size[u]) {
    a[u].push_back(node());
    return;
  }
  for (int i = 0; i < 2; ++i) {
    int v = ch[u][i], w = val[u][i];
    DFS(v, limit);
    for (int j = 0; j < (int)a[v].size(); ++j) {
      a[v][j].p += w;
      a[v][j].q += w;
    }
  }
  int ls = ch[u][0], rs = ch[u][1];
  sort(a[ls].begin(), a[ls].end());
  sort(a[rs].begin(), a[rs].end());
  merge(limit, a[ls], a[rs], a[u]);
  merge(limit, a[rs], a[ls], a[u]);
}

bool check(lint limit) {
  DFS(1, limit);
  return a[1].size();
}

int solve() {
  lint l = 0, r = 1ll << 34, res = -1;
  while (l <= r) {
    lint mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return res;
}

int main() {
  n = read();
  for (int i = 2; i <= n; ++i) {
    int f = read(), v = read();
    ch[f][size[f]] = i;
    val[f][size[f]] = v;
    ++size[f];
  }
  lint ans = solve();
  printf("%lld\n", ans);
}