Problem p02648 (C++)

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

using namespace std;

#define sz(x) (int)((x).size())
#define forn(i, n) for (int i = 0; i < n; i++)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define pb push_back
#define fi first
#define se second
#define all(vec) vec.begin(), vec.end()
#define time() ((double)clock() / CLOCKS_PER_SEC)

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// Debug
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.fi) + ", " + to_string(p.se) + ")";
}
template <typename A> string to_string(A v) {
  if (v.begin() == v.end())
    return "";
  return accumulate(
             ++v.begin(), v.end(), "{" + to_string(*v.begin()),
             [](const string &a, auto b) { return a + ", " + to_string(b); }) +
         "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) false
#endif

double PI = 3.141592653589793;

const int mx = 1 << 18, mvn = 1e5;
map<int, int> proc[mx];
int va[mx], w[mx];

void solve() {
  int n, q;
  cin >> n;
  int bs = 9;
  forse(i, 1, n + 1) cin >> va[i] >> w[i];

  forse(i, 1, n + 1) {

    if (i >= (1 << bs))
      break;
    map<int, int> cur;

    if (i == 1) {
      cur[0] = 0;
      cur[w[i]] = va[i];
      proc[i] = cur;
      continue;
    }

    cur = proc[i / 2];
    for (auto v : proc[i / 2]) {
      int vn = v.fi + w[i], val = v.se + va[i];
      if (vn <= mvn)
        cur[vn] = max(cur[vn], val);
    }
    int bval = -1;
    for (auto v : cur) {
      if (v.se <= bval)
        continue;
      bval = v.se;
      proc[i][v.fi] = v.se;
    }
  }

  //	forse(i,1,n+1){
  //		debug(i,proc[i]);
  //	}

  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    int pr = v, out = 0;

    vector<pii> pro;
    while (pr >= (1 << bs)) {
      pro.pb({w[pr], va[pr]});
      pr /= 2;
    }

    forn(ind, (1 << sz(pro))) {
      int vsm = 0, wsm = 0;
      forn(j, sz(pro)) {
        if (ind & (1 << j)) {
          vsm += pro[j].se;
          wsm += pro[j].fi;
        }
      }
      if (l < wsm)
        continue;
      out = max(out, vsm + (--proc[pr].upper_bound(l - wsm))->se);
    }

    cout << out << '\n';
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  //	cin >> T;
  forse(c, 1, T + 1) {
    //	cout << "Case #" << cid << ": ";
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)((x).size())
#define forn(i, n) for (int i = 0; i < n; i++)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define pb push_back
#define fi first
#define se second
#define all(vec) vec.begin(), vec.end()
#define time() ((double)clock() / CLOCKS_PER_SEC)

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// Debug
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.fi) + ", " + to_string(p.se) + ")";
}
template <typename A> string to_string(A v) {
  if (v.begin() == v.end())
    return "";
  return accumulate(
             ++v.begin(), v.end(), "{" + to_string(*v.begin()),
             [](const string &a, auto b) { return a + ", " + to_string(b); }) +
         "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) false
#endif

double PI = 3.141592653589793;

const int mx = 1 << 18, mvn = 1e5;
map<int, int> proc[mx];
int va[mx], w[mx];

void solve() {
  int n, q;
  cin >> n;
  int bs = 10;
  forse(i, 1, n + 1) cin >> va[i] >> w[i];

  forse(i, 1, n + 1) {

    if (i >= (1 << bs))
      break;
    map<int, int> cur;

    if (i == 1) {
      cur[0] = 0;
      cur[w[i]] = va[i];
      proc[i] = cur;
      continue;
    }

    cur = proc[i / 2];
    for (auto v : proc[i / 2]) {
      int vn = v.fi + w[i], val = v.se + va[i];
      if (vn <= mvn)
        cur[vn] = max(cur[vn], val);
    }
    int bval = -1;
    for (auto v : cur) {
      if (v.se <= bval)
        continue;
      bval = v.se;
      proc[i][v.fi] = v.se;
    }
  }

  //	forse(i,1,n+1){
  //		debug(i,proc[i]);
  //	}

  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    int pr = v, out = 0;

    vector<pii> pro;
    while (pr >= (1 << bs)) {
      pro.pb({w[pr], va[pr]});
      pr /= 2;
    }

    forn(ind, (1 << sz(pro))) {
      int vsm = 0, wsm = 0;
      forn(j, sz(pro)) {
        if (ind & (1 << j)) {
          vsm += pro[j].se;
          wsm += pro[j].fi;
        }
      }
      if (l < wsm)
        continue;
      out = max(out, vsm + (--proc[pr].upper_bound(l - wsm))->se);
    }

    cout << out << '\n';
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  //	cin >> T;
  forse(c, 1, T + 1) {
    //	cout << "Case #" << cid << ": ";
    solve();
  }
}