Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(const Args &...args) const {
      print(args...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(const T &head, const Tail &...tail) const {
      std::cout << head, print_rest(tail...);
    }
    template <class T, class... Tail>
    void print_rest(const T &head, const Tail &...tail) const {
      std::cout << sep << head, print_rest(tail...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\n") const {
    return __Prints(sep, term);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

constexpr ll MX_W = 100000;
ll dp1[600][100010];

void solve() {
  ll N;
  cin >> N;
  Vl V(N + 1), W(N + 1);
  for (ll i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  ll th = N / 2;

  for (ll i = 1; i <= th; i++) {
    for (ll j = 0; j <= MX_W; j++) {
      chmax(dp1[i][j], dp1[i >> 1][j]);
      if (j - W[i] >= 0)
        chmax(dp1[i][j], dp1[i >> 1][j - W[i]] + V[i]);
    }
  }

  ll Q;
  cin >> Q;
  vector<Pll> d;
  while (Q--) {
    ll v, L;
    cin >> v >> L;
    d.clear();
    int idx = v;
    while (idx > th) {
      d.emplace_back(V[idx], W[idx]);
      idx >>= 1;
    }
    ll res = 0;
    ll sz = (ll)d.size();
    for (ll mask = 0; mask < (1ll << sz); mask++) {
      ll tv = 0, tw = 0;
      for (ll i = 0; i < sz; i++) {
        if (mask >> i & 1) {
          tv += d[i].first;
          tw += d[i].second;
        }
      }
      if (tw > L)
        continue;
      chmax(res, tv + dp1[idx][L - tw]);
    }
    prints()(res);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(nullptr), cout.tie(nullptr);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(const Args &...args) const {
      print(args...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(const T &head, const Tail &...tail) const {
      std::cout << head, print_rest(tail...);
    }
    template <class T, class... Tail>
    void print_rest(const T &head, const Tail &...tail) const {
      std::cout << sep << head, print_rest(tail...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\n") const {
    return __Prints(sep, term);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

constexpr ll MX_W = 100000;
ll dp1[600][100010];

void solve() {
  ll N;
  cin >> N;
  Vl V(N + 1), W(N + 1);
  for (ll i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }

  ll th;
  {
    ll left = -1, right = N;
    while (right - left > 1) {
      ll mid = (right + left) >> 1;
      if (mid * mid <= N) {
        left = mid;
      } else {
        right = mid;
      }
    }
    th = left;
  }

  for (ll i = 1; i <= th; i++) {
    for (ll j = 0; j <= MX_W; j++) {
      chmax(dp1[i][j], dp1[i >> 1][j]);
      if (j - W[i] >= 0)
        chmax(dp1[i][j], dp1[i >> 1][j - W[i]] + V[i]);
    }
  }

  ll Q;
  cin >> Q;
  vector<Pll> d;
  while (Q--) {
    ll v, L;
    cin >> v >> L;
    d.clear();
    int idx = v;
    while (idx > th) {
      d.emplace_back(V[idx], W[idx]);
      idx >>= 1;
    }
    ll res = 0;
    ll sz = (ll)d.size();
    for (ll mask = 0; mask < (1ll << sz); mask++) {
      ll tv = 0, tw = 0;
      for (ll i = 0; i < sz; i++) {
        if (mask >> i & 1) {
          tv += d[i].first;
          tw += d[i].second;
        }
      }
      if (tw > L)
        continue;
      chmax(res, tv + dp1[idx][L - tw]);
    }
    prints()(res);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(nullptr), cout.tie(nullptr);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}