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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define INF (int)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int n, q, a, b, depth[300005];
ll dp[550][100005], v[300005], w[300005];
vpi query[300005];

void dfs(int x, int dep) {
  int p = x / 2;
  FOR(i, 0, 100000) dp[x][i] = dp[p][i];
  DEC(i, 100000, w[x])
  dp[x][i] = max({dp[p][i - 1], dp[p][i], dp[p][i - w[x]] + v[x]});
  if (dep < 9) {
    if (2 * x <= n)
      dfs(2 * x, dep + 1);
    if (2 * x < n)
      dfs(2 * x + 1, dep + 1);
  }
}

int main() {
  fastio;
  cin >> n;
  depth[1] = 1;
  FOR(i, 2, n) depth[i] = depth[i / 2] + 1;
  FOR(i, 1, n) cin >> v[i] >> w[i];
  dfs(1, 1);
  cin >> q;
  while (q--) {
    cin >> a >> b;
    if (depth[a] <= 9)
      cout << dp[a][b] << "\\n";
    else {
      vi vect;
      while (depth[a] > 9) {
        vect.pb(a);
        a /= 2;
      }
      ll res = 0, sz = vect.size();
      FOR(j, 0, (1 << sz) - 1) {
        ll val = 0, wei = 0;
        FOR(k, 0, sz - 1) if (j & (1 << k)) {
          val += v[vect[k]], wei += w[vect[k]];
        }
        if (wei <= b)
          res = max(res, val + dp[a][b - wei]);
      }
      cout << res << "\\n";
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define INF (int)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int n, q, a, b, depth[300005];
ll dp[550][100005], v[300005], w[300005];

void dfs(int x, int dep) {
  int p = x / 2;
  FOR(i, 0, 100000) dp[x][i] = dp[p][i];
  DEC(i, 100000, w[x])
  dp[x][i] = max({dp[p][i - 1], dp[p][i], dp[p][i - w[x]] + v[x]});
  if (dep < 10) {
    if (2 * x <= n)
      dfs(2 * x, dep + 1);
    if (2 * x < n)
      dfs(2 * x + 1, dep + 1);
  }
}

int main() {
  fastio;
  cin >> n;
  depth[1] = 1;
  FOR(i, 2, n) depth[i] = depth[i / 2] + 1;
  FOR(i, 1, n) cin >> v[i] >> w[i];
  dfs(1, 1);
  cin >> q;
  while (q--) {
    cin >> a >> b;
    if (depth[a] <= 9)
      cout << dp[a][b] << "\\n";
    else {
      vi vect;
      ll x = a;
      while (depth[x] > 9) {
        vect.pb(x);
        x /= 2;
      }
      ll res = 0, sz = vect.size();
      FOR(j, 0, (1 << sz) - 1) {
        ll val = 0, wei = 0;
        FOR(k, 0, sz - 1) if (j & (1 << k)) {
          val += v[vect[k]], wei += w[vect[k]];
        }
        if (wei <= b)
          res = max(res, val + dp[x][b - wei]);
      }
      cout << res << "\\n";
    }
  }
}