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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli N;
vector<lli> V, W;
const lli border = 10;
lli dp[(1LL << border)][100100];
bool vis[(1LL << border)][100100];

void calc(lli now, lli from) {

  if (now >= (1LL << border) || now > N)
    return;

  REP(i, 0, 100100) {
    if (!vis[from][i])
      continue;
    lli nextW = i + W[now - 1];
    lli nextV = dp[from][i] + V[now - 1];
    chmax(dp[now][i], dp[from][i]);
    vis[now][i] = true;

    if (nextW > 100000)
      continue;
    chmax(dp[now][nextW], nextV);
    vis[now][nextW] = true;
  }

  calc(2 * now, now);
  calc(2 * now + 1, now);

  // create DP table for query
  REP(i, 0, 100099) chmax(dp[now][i + 1], dp[now][i]);
}

void func(long long Q, std::vector<long long> v, std::vector<long long> L) {

  vis[0][0] = true;
  dp[0][0] = 0;
  calc(1, 0);

  REP(i, 0, Q) {
    if (v[i] < (1LL << border)) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      lli ans = -INF;
      lli needBit = 0;
      lli tmp = v[i];
      while (tmp >= (1LL << border)) {
        needBit++;
        tmp /= 2;
      }
      REP(bit, 0, (1LL << needBit)) {
        lli nowB = 0;
        lli sumW = 0, sumV = 0;
        lli from = v[i];
        while (from >= (1LL << border)) {
          if (bit & (1LL << nowB)) {
            sumW += W[from - 1];
            sumV += V[from - 1];
          }
          from /= 2;
          nowB++;
        }
        if (sumW > L[i])
          continue;
        LOG4(v[i], bit, sumW, sumV);
        LOG3(from, ans, dp[from][L[i] - sumW] + sumV);
        chmax(ans, dp[from][L[i] - sumW] + sumV);
      }
      cout << ans << endl;
    }
  }
}

int main() {
  // cout << fixed << setprecision(5);

  scanf("%lld", &N);
  V.resize(N);
  W.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  func(Q, std::move(v), std::move(L));
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli N;
vector<lli> V, W;
lli dp[(1LL << 10)][100100];
bool vis[(1LL << 10)][100100];

void calc(lli now, lli from) {

  if (now >= (1LL << 10))
    return;

  REP(i, 0, 100100) {
    if (!vis[from][i])
      continue;
    lli nextW = i + W[now - 1];
    lli nextV = dp[from][i] + V[now - 1];
    if (nextW > 100000)
      continue;
    chmax(dp[now][i], dp[from][i]);
    chmax(dp[now][nextW], nextV);
    vis[now][i] = true;
    vis[now][nextW] = true;
  }

  calc(2 * now, now);
  calc(2 * now + 1, now);

  // create DP table for query
  REP(i, 0, 100099) chmax(dp[now][i + 1], dp[now][i]);
}

void func(long long Q, std::vector<long long> v, std::vector<long long> L) {

  vis[0][0] = true;
  dp[0][0] = 0;
  calc(1, 0);

  REP(i, 0, Q) {
    if (v[i] < (1LL << 10)) {
      cout << dp[v[i]][L[i]] << endl;
    } else {
      lli sumW = 0, sumV = 0;
      lli from = v[i];
      REP(bit, 0, (1LL << 9)) {
        lli nowB = 0;
        while (from > (1LL << 10)) {
          if (bit & (1LL << nowB)) {
            sumW += W[from];
            sumV += V[from];
          }
          from /= 2;
          nowB++;
        }
      }
      if (sumW > L[i]) {
        cout << 0 << endl;
      } else {
        cout << dp[from][L[i] - sumW] + sumV << endl;
      }
    }
  }
}

int main() {
  // cout << fixed << setprecision(5);

  scanf("%lld", &N);
  V.resize(N);
  W.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  func(Q, std::move(v), std::move(L));
  return 0;
}