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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 1 << 18, MID = 1 << 10, MAX = 1e5;
int dp[MID + 1][MAX + 1];
int val[N], w[N], h[N];
int main() {
  run();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
    h[i] = h[i >> 1] + 1;
  }
  for (int i = 1; i <= n && i <= MID; i++)
    for (int j = 0; j <= MAX; j++) {
      dp[i][j] = dp[i >> 1][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i >> 1][j - w[i]] + val[i]);
    }
  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    vector<pair<int, int>> tmp;
    while (v > MID) {
      tmp.push_back({val[v], w[v]});
      v >>= 1;
    }
    int rt = dp[v][l];
    for (int i = 1; i < (1 << sz(tmp)); i++) {
      int sumVal = 0, sumWig = 0;
      for (int j = 0; j < sz(tmp); j++) {
        if (i & (1 << j))
          sumWig += tmp[j].second, sumVal += tmp[j].first;
      }
      if (sumWig <= l)
        rt = max(rt, dp[v][l - sumWig] + sumVal);
    }
    cout << rt << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

const int N = 1 << 18, MID = 1 << 10, MAX = 1e5;
int dp[MID + 1][MAX + 1];
int val[N], w[N], h[N];
int main() {
  run();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
    h[i] = h[i >> 1] + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = w[i]; j <= MAX; j++)
      dp[i][j] = max(dp[i >> 1][j], dp[i >> 1][j - w[i]] + val[i]);
  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    vector<pair<int, int>> tmp;
    while (v > MID) {
      tmp.push_back({val[v], w[v]});
      v >>= 1;
    }
    int rt = dp[v][l];
    for (int i = 0; i < (1 << sz(tmp)); i++) {
      int sumVal = 0, sumWig = 0;
      for (int j = 0; j < sz(tmp); j++) {
        if (i & (1 << j))
          sumWig += tmp[i].second, sumVal += tmp[j].first;
      }
      if (sumWig <= l)
        rt = max(rt, dp[v][l - sumWig] + sumVal);
    }
    cout << rt << endl;
  }
}