Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
using namespace std;
typedef long long LL;
typedef pair<int, int> pa;
typedef vector<int> vec;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar())
    v = v * 10 + ch - '0';
  if (fu)
    v = -v;
}
int f[520][100010];
int n, N, M, tmp[55], q, ts, vv[500010], ww[500010], v[500010], w[500010],
    o[500010], V, L, ans;
int main() {
  cin >> n;
  FOR(i, 1, n) getint(v[i]), getint(w[i]);
  N = 1;
  while (1) {
    if (n > (1 << N) - 1)
      ++N;
    else
      break;
  }
  M = (1 << N) - 1;
  FOR(i, 1, M) {
    int fa = i >> 1;
    FOR(j, 0, 100000) f[i][j] = f[fa][j];
    FOR(j, w[i], 100000) f[i][j] = max(f[i][j], f[fa][j - w[i]] + v[i]);
  }
  FOR(i, 1, 500000) {
    FOR(j, 0, 22) if ((i >> j) & 1) {
      o[i] = j;
      break;
    }
  }
  cin >> q;
  while (q--) {
    scanf("%d%d", &V, &L);
    if (V <= M) {
      printf("%d\n", f[V][L]);
      continue;
    }
    ts = 0;
    while (1) {
      if (V <= M)
        break;
      tmp[ts++] = V;
      V >>= 1;
    }
    vv[0] = 0;
    ww[0] = 0;
    FOR(i, 1, (1 << ts) - 1) {
      vv[i] = vv[i - (1 << o[i])] + v[tmp[o[i]]];
      ww[i] = ww[i - (1 << o[i])] + w[tmp[o[i]]];
    }
    ans = 0;
    FOR(i, 0, (1 << ts) - 1)
    if (ww[i] <= L) ans = max(ans, vv[i] + f[V][L - ww[i]]);
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
using namespace std;
typedef long long LL;
typedef pair<int, int> pa;
typedef vector<int> vec;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar())
    v = v * 10 + ch - '0';
  if (fu)
    v = -v;
}
int f[520][100010];
int n, N, M, tmp[55], q, ts, vv[500010], ww[500010], v[500010], w[500010],
    o[500010], V, L, ans;
int main() {
  cin >> n;
  FOR(i, 1, n) getint(v[i]), getint(w[i]);
  N = 1;
  while (1) {
    if (n > (1 << N) - 1)
      ++N;
    else
      break;
  }
  N = (N + 1) / 2;
  M = (1 << N) - 1;
  FOR(i, 1, M) {
    int fa = i >> 1;
    FOR(j, 0, 100000) f[i][j] = f[fa][j];
    FOR(j, w[i], 100000) f[i][j] = max(f[i][j], f[fa][j - w[i]] + v[i]);
  }
  FOR(i, 1, 500000) {
    FOR(j, 0, 22) if ((i >> j) & 1) {
      o[i] = j;
      break;
    }
  }
  cin >> q;
  while (q--) {
    scanf("%d%d", &V, &L);
    if (V <= M) {
      printf("%d\n", f[V][L]);
      continue;
    }
    ts = 0;
    while (1) {
      if (V <= M)
        break;
      tmp[ts++] = V;
      V >>= 1;
    }
    vv[0] = 0;
    ww[0] = 0;
    FOR(i, 1, (1 << ts) - 1) {
      vv[i] = vv[i - (1 << o[i])] + v[tmp[o[i]]];
      ww[i] = ww[i - (1 << o[i])] + w[tmp[o[i]]];
    }
    ans = 0;
    FOR(i, 0, (1 << ts) - 1)
    if (ww[i] <= L) ans = max(ans, vv[i] + f[V][L - ww[i]]);
    printf("%d\n", ans);
  }
  return 0;
}