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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, q, v[300000], w[300000], dp[2100][100020], tot = 0, depth, ww, vv;
vector<pii> dd;

void dfs(LL pos, LL d) {
  if (d > depth / 2 || pos >= n)
    return;
  tot++;
  if (pos == 0)
    dp[pos][w[pos]] = v[pos];
  else {
    LL lst = (pos - 1) / 2;
    rep(i, 100005) {
      if (dp[lst][i] < 0)
        continue;
      dp[pos][i] = max(dp[pos][i], dp[lst][i]);
      if (i + w[pos] <= 100005)
        dp[pos][i + w[pos]] = max(dp[pos][i + w[pos]], dp[lst][i] + v[pos]);
    }
  }
  dfs(pos * 2 + 1, d + 1);
  dfs(pos * 2 + 2, d + 1);
}

LL getDepth(LL xx) {
  LL ret = 1;
  while (xx > 0) {
    ret++;
    xx = (xx - 1) / 2;
  }
  return ret;
}

int main() {
  cin >> n;
  depth = getDepth(n - 1);
  rep(i, n) scanf("%lld%lld", &v[i], &w[i]);
  rep(i, 595) rep(j, 100010) dp[i][j] = -1;
  dp[0][0] = 0;
  dfs(0, 1);
  rep(i, max(tot, 1LL)) repn(j, 100003) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
  cin >> q;
  LL x, y;
  rep(qn, q) {
    scanf("%lld%lld", &x, &y);
    x--;
    LL d = getDepth(x);
    if (d <= depth / 2)
      printf("%lld\\n", dp[x][y]);
    else {
      dd.clear();
      rep(i, d - depth / 2) {
        dd.pb(mpr(w[x], v[x]));
        x = (x - 1) / 2;
      }
      LL ans = 0;
      rep(i, 1 << dd.size()) {
        ww = 0;
        vv = 0;
        rep(j, dd.size()) {
          if ((i & (1 << j)) == 0)
            continue;
          ww += dd[j].fi;
          vv += dd[j].se;
          if (ww > y)
            break;
        }
        if (ww > y)
          continue;
        ans = max(ans, vv + dp[x][y - ww]);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, q, v[300000], w[300000], dp[600][100020], tot = 0, depth, ww, vv;
vector<pii> dd;

void dfs(LL pos, LL d) {
  if (d > depth / 2)
    return;
  tot++;
  if (pos == 0) {
    dp[pos][0] = 0;
    dp[pos][w[pos]] = v[pos];
  } else {
    LL lst = (pos - 1) / 2;
    rep(i, 100005) {
      if (dp[lst][i] < 0)
        continue;
      dp[pos][i] = max(dp[pos][i], dp[lst][i]);
      if (i + w[pos] <= 100005)
        dp[pos][i + w[pos]] = max(dp[pos][i + w[pos]], dp[lst][i] + v[pos]);
    }
  }
  dfs(pos * 2 + 1, d + 1);
  dfs(pos * 2 + 2, d + 1);
}

LL getDepth(LL xx) {
  LL ret = 1;
  while (xx > 0) {
    ret++;
    xx = (xx - 1) / 2;
  }
  return ret;
}

int main() {
  cin >> n;
  depth = getDepth(n - 1);
  rep(i, n) scanf("%lld%lld", &v[i], &w[i]);
  rep(i, 595) rep(j, 100010) dp[i][j] = -1;
  dfs(0, 1);
  rep(i, tot) repn(j, 100003) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
  cin >> q;
  LL x, y;
  rep(qn, q) {
    scanf("%lld%lld", &x, &y);
    x--;
    LL d = getDepth(x);
    if (d <= depth / 2 + 2)
      printf("%lld\\n", dp[x][y]);
    else {
      dd.clear();
      rep(i, d - depth / 2) {
        dd.pb(mpr(w[x], v[x]));
        x = (x - 1) / 2;
      }
      LL ans = 0;
      rep(i, 1 << dd.size()) {
        ww = 0;
        vv = 0;
        rep(j, dd.size()) {
          if ((i & (1 << j)) == 0)
            continue;
          ww += dd[j].fi;
          vv += dd[j].se;
          if (ww > y)
            break;
        }
        if (ww > y)
          continue;
        ans = max(ans, vv + dp[x][y - ww]);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}