Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int V[1 << 18], W[1 << 18];
vector<P> vv[1 << 18];

int dep(int v) {
  int cnt = 0;
  while (v > 1) {
    cnt++;
    v >>= 1;
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) { scanf("%d%d", &V[i + 1], &W[i + 1]); }
  int t = 8;
  for (int i = 1; i <= n; i++) {
    if (dep(i) == t) {
      int v = i;
      vector<P> vs;
      while (v >= 1) {
        vs.push_back(P(V[v], W[v]));
        v >>= 1;
      }
      int N = vs.size();
      vector<P> a{P(0, 0)};
      rep(j, 1 << N) {
        int v = 0, w = 0;
        rep(k, N) {
          if (j >> k & 1)
            v += vs[k].first, w += vs[k].second;
        }
        a.push_back(P(w, v));
      }
      sort(a.begin(), a.end());
      vector<P> b;
      for (auto p : a) {
        if (b.empty() || b.back().second < p.second)
          b.push_back(p);
      }
      vv[i] = b;
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int v, L;
    scanf("%d%d", &v, &L);
    int id = -1;
    vector<P> vs;
    while (v >= 1) {
      if (dep(v) == t) {
        id = v;
        break;
      }
      vs.push_back(P(V[v], W[v]));
      v >>= 1;
    }
    if (id == -1) {
      int N = vs.size();
      int Max = 0;
      rep(j, 1 << N) {
        int v = 0, w = 0;
        rep(k, N) {
          if (j >> k & 1)
            v += vs[k].first, w += vs[k].second;
        }
        if (w <= L)
          Max = max(Max, v);
      }
      printf("%d\n", Max);
      continue;
    }
    int N = vs.size();
    int Max = 0;
    rep(j, 1 << N) {
      int v = 0, w = 0;
      rep(k, N) {
        if (j >> k & 1)
          v += vs[k].first, w += vs[k].second;
      }
      if (w > L)
        continue;
      auto it = lower_bound(vv[id].begin(), vv[id].end(), P(L - w + 1, 0));
      it--;
      Max = max(Max, v + it->second);
    }
    printf("%d\n", Max);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int V[1 << 18], W[1 << 18];
vector<P> vv[1 << 18];

int dep(int v) {
  int cnt = 0;
  while (v > 1) {
    cnt++;
    v >>= 1;
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) { scanf("%d%d", &V[i + 1], &W[i + 1]); }
  int t = 10;
  for (int i = 1; i <= n; i++) {
    if (dep(i) == t) {
      int v = i;
      vector<P> vs;
      while (v >= 1) {
        vs.push_back(P(V[v], W[v]));
        v >>= 1;
      }
      int N = vs.size();
      vector<P> a{P(0, 0)};
      rep(j, 1 << N) {
        int v = 0, w = 0;
        rep(k, N) {
          if (j >> k & 1)
            v += vs[k].first, w += vs[k].second;
        }
        a.push_back(P(w, v));
      }
      sort(a.begin(), a.end());
      vector<P> b;
      for (auto p : a) {
        if (b.empty() || b.back().second < p.second)
          b.push_back(p);
      }
      vv[i] = b;
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int v, L;
    scanf("%d%d", &v, &L);
    int id = -1;
    vector<P> vs;
    while (v >= 1) {
      if (dep(v) == t) {
        id = v;
        break;
      }
      vs.push_back(P(V[v], W[v]));
      v >>= 1;
    }
    if (id == -1) {
      int N = vs.size();
      int Max = 0;
      rep(j, 1 << N) {
        int v = 0, w = 0;
        rep(k, N) {
          if (j >> k & 1)
            v += vs[k].first, w += vs[k].second;
        }
        if (w <= L)
          Max = max(Max, v);
      }
      printf("%d\n", Max);
      continue;
    }
    int N = vs.size();
    int Max = 0;
    rep(j, 1 << N) {
      int v = 0, w = 0;
      rep(k, N) {
        if (j >> k & 1)
          v += vs[k].first, w += vs[k].second;
      }
      if (w > L)
        continue;
      auto it = lower_bound(vv[id].begin(), vv[id].end(), P(L - w + 1, 0));
      it--;
      Max = max(Max, v + it->second);
    }
    printf("%d\n", Max);
  }
}