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

/* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define setval(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x, a) for (auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll;
#define int ll
using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int SZ = 101;
const long double eps = 1e-9;

using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
                         tree_order_statistics_node_update>;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef unsigned long long ull;
typedef long double ld;

ll po(ll x, ll y, ll p = mod) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const int M = (1 << 9) + 1;
const int L = 1e5 + 1;

int dp[M][L];

void solve() {

  int n;
  cin >> n;
  int V[n + 1], W[n + 1];
  rep(i, n) cin >> V[i + 1] >> W[i + 1];

  for (int i = 1; i <= min(M - 2, n); ++i) {
    for (int l = 0; l < L; ++l) {
      dp[i][l] = max(dp[i][l], dp[i / 2][l]);
      if (l >= W[i])
        dp[i][l] = max(dp[i][l], dp[i / 2][l - W[i]] + V[i]);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (v <= (M - 2)) {
      cout << dp[v][l] << '\\n';
    } else {
      int u = v, ans, k;
      vector<pii> s;
      while (u > (M - 2))
        s.pb({V[u], W[u]}), u /= 2;
      k = sz(s);
      ans = dp[u][l];
      rep(msk, 1 << k) {
        int tw = 0, tv = 0;
        rep(i, k) {
          if (msk >> i & 1) {
            tw += s[i].ss;
            tv += s[i].ff;
          }
        }
        if (tw <= l) {
          ans = max(ans, dp[u][l - tw] + tv);
        }
      }
      cout << ans << '\\n';
    }
  }
}

int32_t main(int32_t argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case #" << t << ": " ;
    solve();
    cout << "\\n";
  }
  return 0;
}


Error: -11

Fixed submission:

/* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define setval(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x, a) for (auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll;
#define int ll
using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int SZ = 101;
const long double eps = 1e-9;

using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
                         tree_order_statistics_node_update>;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef unsigned long long ull;
typedef long double ld;

ll po(ll x, ll y, ll p = mod) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const int M = (1 << 9) + 1;
const int L = 1e5 + 1;

int dp[M][L];

void solve() {

  int n;
  cin >> n;
  int V[n + 1], W[n + 1];
  rep(i, n) cin >> V[i + 1] >> W[i + 1];

  for (int i = 1; i <= min(M - 2, n); ++i) {
    for (int l = 0; l < L; ++l) {
      dp[i][l] = max(dp[i][l], dp[i / 2][l]);
      if (l >= W[i])
        dp[i][l] = max(dp[i][l], dp[i / 2][l - W[i]] + V[i]);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (v <= (M - 2)) {
      cout << dp[v][l] << '\\n';
    } else {
      int u = v, ans, k;
      vector<pii> s;
      while (u > (M - 2))
        s.pb({V[u], W[u]}), k /= 2;
      k = sz(s);
      rep(msk, 1 << k) {
        int tw = 0, tv = 0;
        rep(i, k) {
          if (msk >> i & 1) {
            tw += s[i].ss;
            tv += s[i].ff;
          }
        }
        if (tw <= l) {
          ans = max(ans, dp[u][l - tw] + tv);
        }
      }
      cout << ans << '\\n';
    }
  }
}

int32_t main(int32_t argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case #" << t << ": " ;
    solve();
    cout << "\\n";
  }
  return 0;
}