Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

ll dp[600][100100];
int k = 8;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  rep(i, 600) rep(j, 100100) dp[i][j] = 0;
  for (int cur = 1; cur <= min((1 << k), n); cur++) {
    int par = cur / 2;
    for (int i = 100000; i >= 0; i--) {
      chmax(dp[cur][i], dp[par][i]);
      if (i + w[cur] < 100100)
        chmax(dp[cur][i + w[cur]], dp[par][i] + v[cur]);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int node, l;
    cin >> node >> l;

    if (node < (1 << k)) {
      cout << dp[node][l] << endl;
      continue;
    }
    vector<int> vv, ww;
    for (; node >= (1 << k); node >>= 1)
      vv.push_back(v[node]), ww.push_back(w[node]);

    int t = (int)vv.size();
    ll ans = 0;
    for (int bit = 0; bit < (1 << t); bit++) {
      ll wsum = 0, vsum = 0;
      rep(i, t) if ((bit >> i) & 1) wsum += ww[i], vsum += vv[i];
      if (wsum <= l)
        chmax(ans, vsum + dp[node][l - wsum]);
    }
    cout << ans << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int dp[1025][100100];
int k = 10;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  rep(i, 600) rep(j, 100100) dp[i][j] = 0;
  for (int cur = 1; cur <= min((1 << k), n); cur++) {
    int par = cur / 2;
    for (int i = 100000; i >= 0; i--) {
      chmax(dp[cur][i], dp[par][i]);
      if (i + w[cur] < 100100)
        chmax(dp[cur][i + w[cur]], dp[par][i] + v[cur]);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int node, l;
    cin >> node >> l;

    if (node < (1 << k)) {
      cout << dp[node][l] << endl;
      continue;
    }
    vector<int> vv, ww;
    for (; node >= (1 << k); node >>= 1)
      vv.push_back(v[node]), ww.push_back(w[node]);

    int t = (int)vv.size();
    ll ans = 0;
    for (int bit = 0; bit < (1 << t); bit++) {
      ll wsum = 0, vsum = 0;
      rep(i, t) if ((bit >> i) & 1) wsum += ww[i], vsum += vv[i];
      if (wsum <= l)
        chmax(ans, vsum + dp[node][l - wsum]);
    }
    cout << ans << "\n";
  }
  return 0;
}