Problem p02648 (C++)

Buggy submission (status: Runtime Error):
// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

const int U = 1 << 10 - 1;

using pi = pair<int, int>;

int main() {
  int n;
  scanf(" %d", &n);

  vector<int> v(n + 1), w(n + 1);
  rep(i, n) scanf(" %d %d", &v[i + 1], &w[i + 1]);

  vector<int> p(n + 1);
  p[1] = -1;
  for (int i = 2; i <= n; ++i)
    p[i] = i / 2;

  vector<int> dep(n + 1);
  dep[1] = 0;
  for (int i = 2; i <= n; ++i)
    dep[i] = dep[p[i]] + 1;

  vector<vector<pi>> up(U + 1);
  for (int i = 1; i <= min(U, n); ++i) {
    int x = i;
    vector<int> a;
    while (x > 0) {
      a.pb(x);
      x = p[x];
    }
    const int A = a.size();

    vector<pi> ss;
    rep(mask, 1 << A) {
      int tw = 0, tv = 0;
      rep(j, A) if (mask >> j & 1) {
        tw += w[a[j]];
        tv += v[a[j]];
      }
      ss.pb({tw, -tv});
    }
    sort(all(ss));

    up[i].pb({0, 0});
    for (const auto &pp : ss) {
      int vv = -pp.se;
      if (up[i].back().se < vv)
        up[i].pb({pp.fi, vv});
    }
  }

  int Q;
  scanf(" %d", &Q);
  rep(qqq, Q) {
    int x, L;
    scanf(" %d %d", &x, &L);

    int ans = 0;
    if (dep[x] <= 9) {
      auto itr = lower_bound(all(up[x]), pi(L + 1, 0));
      --itr;
      ans = itr->se;
    } else {
      vector<int> a;
      while (dep[x] > 9) {
        a.pb(x);
        x = p[x];
      }
      int A = a.size();

      rep(mask, 1 << A) {
        int tw = 0, tv = 0;
        rep(i, A) {
          if (mask >> i & 1) {
            tw += w[a[i]];
            tv += v[a[i]];
          }
        }
        if (tw > L)
          continue;

        auto itr = lower_bound(all(up[x]), pi(L - tw + 1, 0));
        --itr;
        ans = max(ans, tv + itr->se);
      }
    }

    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

const int U = (1 << 10) - 1;

using pi = pair<int, int>;

int main() {
  int n;
  scanf(" %d", &n);

  vector<int> v(n + 1), w(n + 1);
  rep(i, n) scanf(" %d %d", &v[i + 1], &w[i + 1]);

  vector<int> p(n + 1);
  p[1] = -1;
  for (int i = 2; i <= n; ++i)
    p[i] = i / 2;

  vector<int> dep(n + 1);
  dep[1] = 0;
  for (int i = 2; i <= n; ++i)
    dep[i] = dep[p[i]] + 1;

  vector<vector<pi>> up(U + 1);
  for (int i = 1; i <= min(U, n); ++i) {
    int x = i;
    vector<int> a;
    while (x > 0) {
      a.pb(x);
      x = p[x];
    }
    const int A = a.size();

    vector<pi> ss;
    rep(mask, 1 << A) {
      int tw = 0, tv = 0;
      rep(j, A) if (mask >> j & 1) {
        tw += w[a[j]];
        tv += v[a[j]];
      }
      ss.pb({tw, -tv});
    }
    sort(all(ss));

    up[i].pb({0, 0});
    for (const auto &pp : ss) {
      int vv = -pp.se;
      if (up[i].back().se < vv)
        up[i].pb({pp.fi, vv});
    }
  }

  int Q;
  scanf(" %d", &Q);
  rep(qqq, Q) {
    int x, L;
    scanf(" %d %d", &x, &L);

    int ans = 0;
    if (dep[x] <= 9) {
      auto itr = lower_bound(all(up[x]), pi(L + 1, 0));
      --itr;
      ans = itr->se;
    } else {
      vector<int> a;
      while (dep[x] > 9) {
        a.pb(x);
        x = p[x];
      }
      int A = a.size();

      rep(mask, 1 << A) {
        int tw = 0, tv = 0;
        rep(i, A) {
          if (mask >> i & 1) {
            tw += w[a[i]];
            tv += v[a[i]];
          }
        }
        if (tw > L)
          continue;

        auto itr = lower_bound(all(up[x]), pi(L - tw + 1, 0));
        --itr;
        ans = max(ans, tv + itr->se);
      }
    }

    printf("%d\n", ans);
  }

  return 0;
}