Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> weight(n), value(n);
  for (int i = 0; i < n; i++) {
    std::cin >> value[i] >> weight[i];
  }
  const int D = 10;
  const int B = 100000;
  auto chmax = [](int &a, int b) { return a = std::max(a, b); };
  std::vector<std::vector<int>> dp(1 << D, std::vector<int>(B + 2, -1e9));
  dp[0][0] = 0, dp[0][weight[0]] = value[0];
  for (int i = 1; i < std::min(1 << D, n); i++) {
    int par = (i - 1) / 2;
    for (int j = 0; j <= B; j++) {
      chmax(dp[i][j], dp[par][j]);
      if (j + weight[i] <= B)
        chmax(dp[i][j + weight[i]], dp[par][j] + value[i]);
    }
  }
  for (int i = 0; i < std::min(1 << D, n); i++) {
    for (int j = 0; j < B; j++) {
      chmax(dp[i][j + 1], dp[i][j]);
    }
  }
  int q;
  std::cin >> q;
  std::vector<std::pair<int, int>> buf;
  while (q--) {
    int v, lim;
    std::cin >> v >> lim;
    v--;
    buf.clear();
    while (v >= 1 << D) {
      buf.emplace_back(weight[v], value[v]);
      v = (v - 1) / 2;
    }
    int m = buf.size();
    int ans = 0;
    for (int i = 0; i < (1 << m); i++) {
      int wg = 0, vl = 0;
      for (int j = 0; j < m; j++) {
        if (~(i >> j) & 1)
          continue;
        wg += buf[j].first;
        vl += buf[j].second;
      }
      int targ = lim - wg;
      if (targ < 0)
        continue;
      chmax(ans, vl + dp[v][targ]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  assert(n > 1);
  std::vector<int> weight(n), value(n);
  for (int i = 0; i < n; i++) {
    std::cin >> value[i] >> weight[i];
  }
  const int D = 10;
  const int B = 100000;
  auto chmax = [](int &a, int b) { return a = std::max(a, b); };
  std::vector<std::vector<int>> dp(1 << D, std::vector<int>(B + 2, -1e9));
  dp[0][0] = 0, dp[0][weight[0]] = value[0];
  for (int i = 1; i < std::min(1 << D, n); i++) {
    int par = (i - 1) / 2;
    for (int j = 0; j <= B; j++) {
      chmax(dp[i][j], dp[par][j]);
      if (j + weight[i] <= B)
        chmax(dp[i][j + weight[i]], dp[par][j] + value[i]);
    }
  }
  for (int i = 1; i < std::min(1 << D, n); i++) {
    for (int j = 0; j < B; j++) {
      chmax(dp[i][j + 1], dp[i][j]);
    }
  }
  int q;
  std::cin >> q;
  std::vector<std::pair<int, int>> buf;
  while (q--) {
    int v, lim;
    std::cin >> v >> lim;
    v--;
    buf.clear();
    while (v >= 1 << D) {
      buf.emplace_back(weight[v], value[v]);
      v = (v - 1) / 2;
    }
    int m = buf.size();
    int ans = 0;
    for (int i = 0; i < (1 << m); i++) {
      int wg = 0, vl = 0;
      for (int j = 0; j < m; j++) {
        if (~(i >> j) & 1)
          continue;
        wg += buf[j].first;
        vl += buf[j].second;
      }
      int targ = lim - wg;
      if (targ < 0)
        continue;
      chmax(ans, vl + dp[v][targ]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}