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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_map>

using namespace std;

typedef long long ll;

const int sz = (1 << 18) + 1;
const int sz2 = (1 << 9) + 1;

int n, q;
int dp[sz2][sz];
int vw[sz][2];

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vw[i][1] >> vw[i][0];
  }

  for (int i = 1; i < sz2; i++)
    for (int j = 0; j < sz; j++)
      dp[i][j] = -1;

  dp[1][0] = 0;
  dp[1][vw[1][0]] = vw[1][1];

  for (int i = 2; i < sz2; i++) {
    for (int j = sz - 1; j >= 0; j--) {
      if (dp[i / 2][j] == -1)
        continue;
      dp[i][j] = max(dp[i][j], dp[i / 2][j]);
      if (j + vw[i][0] < sz)
        dp[i][j + vw[i][0]] = max(dp[i][j + vw[i][0]], dp[i / 2][j] + vw[i][1]);
    }
  }

  for (int i = 1; i < sz2; i++) {
    for (int j = 1; j < sz; j++)
      dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    if (v < sz2) {
      cout << dp[v][l];
    } else {
      vector<int> anc;
      int u = v;
      while (u != 0) {
        anc.push_back(u);
        u >>= 1;
      }
      int ind = anc.size();
      for (int j = 0; j < (int)anc.size(); j++)
        if (anc[j] < sz2) {
          ind = j;
          break;
        }
      int otv = 0;
      for (int j = 0; j < (1 << ind); j++) {
        int w = 0, val = 0;
        for (int k = 0; k < ind; k++)
          if (((j >> k) & 1) == 1) {
            w += vw[anc[k]][0];
            val += vw[anc[k]][1];
          }
        if (w > l)
          continue;
        otv = max(otv, val + dp[anc[ind]][l - w]);
      }

      cout << otv;
    }
    cout << '\\n';
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q = 1;
  // cin >> q;
  for (; q > 0; q--) {
    solve();
    cout << '\\n';
  }
}

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_map>

using namespace std;

typedef long long ll;

const int sz = (1 << 18) + 1;
const int sz2 = (1 << 9) + 1;

int n, q;
int dp[sz2][sz];
int vw[sz][2];

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vw[i][1] >> vw[i][0];
  }

  for (int i = 1; i < sz2; i++)
    for (int j = 0; j < sz; j++)
      dp[i][j] = -1;

  dp[1][0] = 0;
  dp[1][vw[1][0]] = vw[1][1];

  for (int i = 2; i < sz2; i++) {
    for (int j = sz - 1; j >= 0; j--) {
      if (dp[i / 2][j] == -1)
        continue;
      dp[i][j] = max(dp[i][j], dp[i / 2][j]);
      if (j + vw[i][0] < sz)
        dp[i][j + vw[i][0]] = max(dp[i][j + vw[i][0]], dp[i / 2][j] + vw[i][1]);
    }
  }

  for (int i = 1; i < sz2; i++) {
    for (int j = 1; j <= sz - 1; j++)
      dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    if (v <= sz2) {
      cout << dp[v][l];
    } else {
      vector<int> anc;
      int u = v;
      while (u != 0) {
        anc.push_back(u);
        u >>= 1;
      }
      int ind = anc.size();
      for (int j = 0; j < (int)anc.size(); j++)
        if (anc[j] <= sz2) {
          ind = j;
          break;
        }
      int otv = 0;
      for (int j = 0; j < (1 << ind); j++) {
        int w = 0, val = 0;
        for (int k = 0; k < ind; k++)
          if (((j >> k) & 1) == 1) {
            w += vw[anc[k]][0];
            val += vw[anc[k]][1];
          }
        if (w > l)
          continue;
        otv = max(otv, val + dp[anc[ind]][l - w]);
      }

      cout << otv;
    }
    cout << '\\n';
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q = 1;
  // cin >> q;
  for (; q > 0; q--) {
    solve();
    cout << '\\n';
  }
}