Problem p02648 (C++)

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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \
  {                                                                            \
    cerr << "[L" << __LINE__ << "] ";                                          \
    _debug(__VA_ARGS__);                                                       \
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &mp) {
  for (auto p : mp) {
    o << p.fi << ": " << p.se << ", ";
  }
  return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

pii t[2 << 19];
int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  int n;
  cin >> n;
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    t[i] = pii(v, w);
  }

  int L = 100010;
  int K = min(n + 1, 1 << 9);
  vvl dp(K + 1, vl(L));
  REP(i, 1, K) {
    int v = t[i - 1].fi, w = t[i - 1].se;
    int par = i >> 1;
    rep(j, L) {
      dp[i][j] = dp[par][j];
      if (j - w >= 0)
        dp[i][j] = max(dp[i][j], dp[par][j - w] + v);
    }
  }

  int q;
  cin >> q;
  rep(_, q) {
    ll u, l;
    cin >> u >> l;
    ll ans = 0;
    if (u < K) {
      ans = dp[u][l];
    } else {
      vi vs;
      while (u >= K) {
        vs.push_back(u);
        u >>= 1;
      }
      int d = vs.size();
      rep(i, 1 << d) {
        ll v = 0, w = 0;
        rep(j, d) {
          if (i >> j & 1) {
            v += t[vs[j] - 1].fi;
            w += t[vs[j] - 1].se;
          }
        }
        if (l - w >= 0)
          ans = max(ans, dp[u][l - w] + v);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \
  {                                                                            \
    cerr << "[L" << __LINE__ << "] ";                                          \
    _debug(__VA_ARGS__);                                                       \
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &mp) {
  for (auto p : mp) {
    o << p.fi << ": " << p.se << ", ";
  }
  return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

pii t[2 << 19];
int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  int n;
  cin >> n;
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    t[i] = pii(v, w);
  }

  int L = 100010;
  int K = min(n + 1, 1 << 10);
  vvl dp(K + 1, vl(L));
  REP(i, 1, K) {
    int v = t[i - 1].fi, w = t[i - 1].se;
    int par = i >> 1;
    rep(j, L) {
      dp[i][j] = dp[par][j];
      if (j - w >= 0)
        dp[i][j] = max(dp[i][j], dp[par][j - w] + v);
    }
  }

  int q;
  cin >> q;
  rep(_, q) {
    ll u, l;
    cin >> u >> l;
    ll ans = 0;
    if (u < K) {
      ans = dp[u][l];
    } else {
      vi vs;
      while (u >= K) {
        vs.push_back(u);
        u >>= 1;
      }
      int d = vs.size();
      rep(i, 1 << d) {
        ll v = 0, w = 0;
        rep(j, d) {
          if (i >> j & 1) {
            v += t[vs[j] - 1].fi;
            w += t[vs[j] - 1].se;
          }
        }
        if (l - w >= 0)
          ans = max(ans, dp[u][l - w] + v);
      }
    }
    cout << ans << endl;
  }
  return 0;
}