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

#pragma GCC optimize("Ofast")
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// --------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int K = 9;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  vector<int> depth(N + 1, 0);
  for (int i = 2; i <= N; i++) {
    depth[i] = depth[i / 2] + 1;
  }
  vector<vector<int>> dp((1LL << K), vector<int>(100005, 0));
  for (int i = 1; i < min((1 << K), N + 1); i++) {
    for (int j = 0; j <= 100000; j++) {
      chmax(dp[i][j], dp[i / 2][j]);
      chmax(dp[i][j + 1], dp[i / 2][j]);
      if (j + W[i] <= 100000) {
        chmax(dp[i][j + W[i]], dp[i / 2][j] + V[i]);
      }
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    if (depth[v] < K) {
      cout << dp[v][L] << "\\n";
    } else {
      vector<int> now;
      while (depth[v] >= K) {
        now.emplace_back(v);
        v /= 2;
      }
      int ans = 0;
      for (int bit = 0; bit < (1LL << (now.size())); bit++) {
        int sum_V = 0, sum_W = 0;
        for (int k = 0; k < now.size(); k++) {
          if (bit & (1LL << k)) {
            sum_V += V[now[k]];
            sum_W += W[now[k]];
          }
        }
        if (sum_W <= L) {
          chmax(ans, sum_V + dp[v][L - sum_W]);
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}

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


Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// --------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int K = 9;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  vector<int> depth(N + 1, 0);
  for (int i = 2; i <= N; i++) {
    depth[i] = depth[i / 2] + 1;
  }
  vector<vector<int>> dp((1LL << K), vector<int>(100005, 0));
  for (int i = 1; i <= min((1 << K), N); i++) {
    for (int j = 0; j <= 100000; j++) {
      chmax(dp[i][j], dp[i / 2][j]);
      chmax(dp[i][j + 1], dp[i / 2][j]);
      if (j + W[i] <= 100000) {
        chmax(dp[i][j + W[i]], dp[i / 2][j] + V[i]);
      }
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    if (depth[v] < K) {
      cout << dp[v][L] << "\\n";
    } else {
      vector<int> now;
      while (depth[v] >= K) {
        now.emplace_back(v);
        v /= 2;
      }
      int ans = 0;
      for (int bit = 0; bit < (1LL << (now.size())); bit++) {
        int sum_V = 0, sum_W = 0;
        for (int k = 0; k < now.size(); k++) {
          if (bit & (1LL << k)) {
            sum_V += V[now[k]];
            sum_W += W[now[k]];
          }
        }
        if (sum_W <= L) {
          chmax(ans, sum_V + dp[v][L - sum_W]);
        }
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}