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

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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\\n", ##__VA_ARGS__)

#define PARENT(x) (x / 2)

ll Query(vector<vector<ll>> &table, vector<pair<ll, ll>> &tree, ll n, ll L) {
  if (n < table.size()) {
    return table[n][L];
  }

  if (L - tree[n].second < 0) {
    return Query(table, tree, PARENT(n), L);
  }

  return max(Query(table, tree, PARENT(n), L - tree[n].second) + tree[n].first,
             Query(table, tree, PARENT(n), L));
}

int main() {
  std::cout << std::fixed << std::setprecision(10);

  ll N;
  cin >> N;
  vector<pair<ll, ll>> tree(N + 1);

  tree[0] = make_pair(-1, -1);

  REP(i, N) {
    ll V, W;
    cin >> V >> W;
    tree[i + 1].first = V;
    tree[i + 1].second = W;
    // DEBUG("tree[%lld]= (%lld, %lld)", i+1, V, W);
  }

  vector<vector<ll>> table(1024 + 1, vector<ll>(100001, -1));

  ll limit = min(N + 1, (ll)1024 + 1);
  for (ll n = 1; n < limit; n++) {
    for (ll w = 0; w <= 100000; w++) {
      if (w == 0) {
        table[n][w] = 0;
        continue;
      }

      if (n == 1) {
        if (w >= tree[n].second) {
          table[n][w] = tree[n].first;
        } else {
          table[n][w] = 0;
        }
      } else {
        if (w - tree[n].second < 0) {
          table[n][w] = table[PARENT(n)][w];
        } else {
          table[n][w] =
              max(table[PARENT(n)][w - tree[n].second] + tree[n].first,
                  table[PARENT(n)][w]);
        }
      }
    }
  }

  /*
  for(ll n = 1; n < limit; n++ ){
      printf("[%lld]",n);
      for( ll w = 0; w <= 10; w++ ){
          printf(" %03lld,", table[n][w] );
      }
      printf("\\n");
  }
  */

  ll Q;
  cin >> Q;
  REP(i, Q) {
    ll v, L;
    cin >> v >> L;
    cout << Query(table, tree, v, L) << endl;
  }
}


Error: 0

Fixed submission:

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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\\n", ##__VA_ARGS__)

#define PARENT(x) (x / 2)

ll Query(vector<vector<ll>> &table, vector<pair<ll, ll>> &tree, ll n, ll L) {
  if (table[n][L] != -1) {
    return table[n][L];
  }

  if (L - tree[n].second < 0) {
    table[n][L] = Query(table, tree, PARENT(n), L);
  }

  table[n][L] =
      max(Query(table, tree, PARENT(n), L - tree[n].second) + tree[n].first,
          Query(table, tree, PARENT(n), L));

  return table[n][L];
}

int main() {
  std::cout << std::fixed << std::setprecision(10);

  ll N;
  cin >> N;
  vector<pair<ll, ll>> tree(N + 1);

  tree[0] = make_pair(-1, -1);

  REP(i, N) {
    ll V, W;
    cin >> V >> W;
    tree[i + 1].first = V;
    tree[i + 1].second = W;
    // DEBUG("tree[%lld]= (%lld, %lld)", i+1, V, W);
  }

  vector<vector<ll>> table(N + 1, vector<ll>(100001, -1));

  ll limit = min(N + 1, (ll)1024);
  for (ll n = 1; n < limit; n++) {
    for (ll w = 0; w <= 100000; w++) {
      if (w == 0) {
        table[n][w] = 0;
        continue;
      }

      if (n == 1) {
        if (w >= tree[n].second) {
          table[n][w] = tree[n].first;
        } else {
          table[n][w] = 0;
        }
      } else {
        if (w - tree[n].second < 0) {
          table[n][w] = table[PARENT(n)][w];
        } else {
          table[n][w] =
              max(table[PARENT(n)][w - tree[n].second] + tree[n].first,
                  table[PARENT(n)][w]);
        }
      }
    }
  }

  /*
  for(ll n = 1; n < limit; n++ ){
      printf("[%lld]",n);
      for( ll w = 0; w <= 10; w++ ){
          printf(" %03lld,", table[n][w] );
      }
      printf("\\n");
  }
  */

  ll Q;
  cin >> Q;
  REP(i, Q) {
    ll v, L;
    cin >> v >> L;
    cout << Query(table, tree, v, L) << endl;
  }
}