Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

const int lim = 1 << 9;

map<int, vll> mp;
vector<P> dat;
int n;

void dfs(int v) {
  if (v > lim || v > n)
    return;
  vll tmp = mp[v / 2];
  for (int i = MAX; i >= dat[v - 1].second; i--) {
    chmax(tmp[i], tmp[i - dat[v - 1].second] + dat[v - 1].first);
  }
  mp[v] = tmp;
  dfs(v * 2);
  dfs(v * 2 + 1);
}

P get_top(int v) {
  int ct = 0;
  while (v > lim) {
    v >>= 1;
    ct++;
  }
  return P(ct, v);
}

int main() {
  cin >> n;
  dat.resize(n);
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    dat[i] = P(v, w);
  }
  mp[0] = vll(MAX + 1, 0);
  dfs(1);
  int q;
  cin >> q;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    if (v <= lim) {
      cout << mp[v][l] << endl;
    } else {
      ll ans = 0;
      P p = get_top(v);
      int top = p.second;
      int ct = p.first;
      rep(i, 1 << ct) {
        int tmp = v;
        int V = 0, W = 0;
        rep(j, ct) {
          if (i >> j & 1) {
            V += dat[tmp - 1].first;
            W += dat[tmp - 1].second;
          }
          tmp /= 2;
        }
        if (l - W >= 0)
          chmax(ans, mp[top][l - W] + V);
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

const int lim = 1 << 10;

map<int, vll> mp;
vector<P> dat;
int n;

void dfs(int v) {
  if (v > lim || v > n)
    return;
  vll tmp = mp[v / 2];
  for (int i = MAX; i >= dat[v - 1].second; i--) {
    chmax(tmp[i], tmp[i - dat[v - 1].second] + dat[v - 1].first);
  }
  mp[v] = tmp;
  dfs(v * 2);
  dfs(v * 2 + 1);
}

P get_top(int v) {
  int ct = 0;
  while (v > lim) {
    v >>= 1;
    ct++;
  }
  return P(ct, v);
}

int main() {
  cin >> n;
  dat.resize(n);
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    dat[i] = P(v, w);
  }
  mp[0] = vll(MAX + 1, 0);
  dfs(1);
  int q;
  cin >> q;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    if (v <= lim) {
      cout << mp[v][l] << endl;
    } else {
      ll ans = 0;
      P p = get_top(v);
      int top = p.second;
      int ct = p.first;
      rep(i, 1 << ct) {
        int tmp = v;
        int V = 0, W = 0;
        rep(j, ct) {
          if (i >> j & 1) {
            V += dat[tmp - 1].first;
            W += dat[tmp - 1].second;
          }
          tmp /= 2;
        }
        if (l - W >= 0)
          chmax(ans, mp[top][l - W] + V);
      }
      cout << ans << endl;
    }
  }
}