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

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
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;
}
const ll INF = 1e18;

ll n, q;
vector<ll> V, W, v, L;
ll dp[1100][100010];

int main() {
  cin >> n;
  V.resize(n);
  W.resize(n);
  rep(i, n) cin >> V[i] >> W[i];
  cin >> q;
  v.resize(q);
  L.resize(q);
  rep(i, q) {
    cin >> v[i] >> L[i];
    v[i]--;
  }
  rep(i, min(n, 1024LL)) {
    if (i == 0) {
      rep(j, 100010) {
        if (j - W[0] < 0)
          continue;
        dp[0][j] = V[0];
      }
    } else {
      ll p = (i - 1) / 2;
      rep(j, 100010) {
        dp[i][j] = dp[p][j];
        if (j - W[i] < 0)
          continue;
        chmax(dp[i][j], dp[p][j - W[i]] + V[i]);
      }
    }
  }
  rep(i, q) {
    if (v[i] < 1024) {
      ll ans = dp[v[i]][L[i]];
      cout << ans << endl;
    } else {
      ll now = v[i];
      ll ans = 0;
      int d = 0;
      ll tmp = now;
      while (tmp > 0) {
        tmp--;
        tmp /= 2;
        d++;
      }
      d -= 9;
      for (int bit = 0; bit < (1 << d); bit++) {
        ll weight = 0, value = 0;
        ll cd = now;
        rep(j, d) {
          if ((bit >> j) & 1) {
            weight += W[cd];
            value += V[cd];
            cd = (cd - 1) / 2;
          } else {
            cd = (cd - 1) / 2;
          }
        }
        if (L[i] - weight < 0)
          continue;
        chmax(ans, dp[cd][L[i] - weight] + value);
      }
      cout << ans << endl;
    }
  }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
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;
}
const ll INF = 1e18;

ll n, q;
vector<ll> V, W, v, L;
ll dp[1100][100010];

int main() {
  cin >> n;
  V.resize(n);
  W.resize(n);
  rep(i, n) cin >> V[i] >> W[i];
  cin >> q;
  v.resize(q);
  L.resize(q);
  rep(i, q) {
    cin >> v[i] >> L[i];
    v[i]--;
  }
  rep(i, min(n, 1024LL)) {
    if (i == 0) {
      rep(j, 100010) {
        if (j - W[0] < 0)
          continue;
        dp[0][j] = V[0];
      }
    } else {
      ll p = (i - 1) / 2;
      rep(j, 100010) {
        dp[i][j] = dp[p][j];
        if (j - W[i] < 0)
          continue;
        chmax(dp[i][j], dp[p][j - W[i]] + V[i]);
      }
    }
  }
  rep(i, q) {
    if (v[i] < 1024) {
      ll ans = dp[v[i]][L[i]];
      cout << ans << endl;
    } else {
      ll now = v[i];
      ll ans = 0;
      int d = 0;
      ll tmp = now;
      while (tmp > 0) {
        tmp--;
        tmp /= 2;
        d++;
      }
      d -= 9;
      for (int bit = 0; bit < (1 << d); bit++) {
        ll weight = 0, value = 0;
        ll cd = now;
        rep(j, d) {
          if ((bit >> j) & 1) {
            weight += W[cd];
            value += V[cd];
            cd = (cd - 1) / 2;
          } else {
            cd = (cd - 1) / 2;
          }
        }
        if (L[now] - weight < 0)
          continue;
        chmax(ans, dp[cd][L[i] - weight] + value);
      }
      cout << ans << endl;
    }
  }

  return 0;
}