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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int dp[1 << 9][100001];
int v[1 << 18], w[1 << 18];
int solve(int V, int l) {
  if (V < 1 << 9) {
    return dp[V][l];
  }
  return max(solve((V - 1) / 2, l),
             l - w[V] >= 0 ? solve((V - 1) / 2, l - w[V]) + v[V] : 0);
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n) cin >> v[i] >> w[i];
  rep(j, 100001) {
    if (j >= w[0])
      dp[0][j] = v[0];
  }
  rep(i, min(n, 1 << 9) - 1) {
    rep(j, 100001) {
      dp[i + 1][j] =
          max(dp[i / 2][j],
              j - w[i + 1] >= 0 ? dp[i / 2][j - w[i + 1]] + v[i + 1] : 0);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int V, L;
    cin >> V >> L;
    cout << solve(V - 1, L) << "\\n";
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int dp[1 << 9][10001];
int v[1 << 18], w[1 << 18];
int solve(int V, int l) {
  if (V < 1 << 9) {
    return dp[V][l];
  }
  return max(solve(V / 2, l), solve(V / 2, l - w[V]) + v[V]);
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n) cin >> v[i] >> w[i];
  rep(j, 1000001) {
    if (j >= w[0])
      dp[0][j] = v[0];
  }
  rep(i, min(n, 1 << 9) - 1) {
    rep(j, 100001) {
      dp[i + 1][j] =
          max(dp[i / 2][j],
              j - w[i + 1] >= 0 ? dp[i / 2][j - w[i + 1]] + v[i + 1] : 0);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int V, L;
    cin >> V >> L;
    cout << solve(V - 1, L) << "\\n";
  }
}