Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

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

  int m = 1;
  while (m < n / 2) {
    m *= 2;
  }
  assert(m < (1 << 9));
  assert(m < n);
  vector<vector<int>> dp(m, vector<int>(100001));
  for (int i = 1; i < m; ++i) {
    int par = i / 2;
    for (int j = 0; j < 100001; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= w[i])
        chmax(dp[i][j], dp[par][j - w[i]] + v[i]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int V, l;
    cin >> V >> l;
    vector<int> vec;
    while (V >= m) {
      vec.push_back(V);
      V /= 2;
    }
    int nn = (int)vec.size();
    int res = 0;
    for (int bits = 0; bits < (1 << nn); ++bits) {
      int cv = 0, cw = 0;
      for (int j = 0; j < nn; ++j) {
        if (bits & (1 << j)) {
          cv += v[vec[j]];
          cw += w[vec[j]];
        }
      }
      if (l - cw >= 0)
        chmax(res, cv + dp[V][l - cw]);
    }
    cout << res << '\n';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

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

  int m = min((1 << 9), n + 1);
  vector<vector<int>> dp(m, vector<int>(100001));
  for (int i = 1; i < m; ++i) {
    int par = i / 2;
    for (int j = 0; j < 100001; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= w[i])
        chmax(dp[i][j], dp[par][j - w[i]] + v[i]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int V, l;
    cin >> V >> l;
    vector<int> vec;
    while (V >= m) {
      vec.push_back(V);
      V /= 2;
    }
    int nn = (int)vec.size();
    int res = 0;
    for (int bits = 0; bits < (1 << nn); ++bits) {
      int cv = 0, cw = 0;
      for (int j = 0; j < nn; ++j) {
        if (bits & (1 << j)) {
          cv += v[vec[j]];
          cw += w[vec[j]];
        }
      }
      if (l - cw >= 0)
        chmax(res, cv + dp[V][l - cw]);
    }
    cout << res << '\n';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}