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

#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second
#define endl "\\n"

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  if (n == 1) {
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int x, L;
      cin >> x >> L;
      if (w[x] <= L)
        cout << v[x] << endl;
      else
        cout << 0 << endl;
    }
    return 0;
  }

  int depth = 1;
  for (int i = 1; i < 30; i++) {
    if ((1 << i) - 1 >= n) {
      depth = i;
      break;
    }
  }

  int d2 = depth / 2;
  int num1 = (1 << d2) - 1;
  int wMax = 100000;
  vector<vector<int>> dp(num1 + 1, vector<int>(wMax + 1, 0));

  vector<bool> state(n, true);
  for (int i = 1; i <= num1; i++) {
    state[i] = false;
  }

  dp[1][w[1]] = v[1];
  for (int i = 2; i <= num1; i++) {
    int bef = i / 2;
    for (int j = 0; j <= wMax; j++) {
      dp[i][j] = max(dp[i][j], dp[bef][j]);
      if (j + w[i] <= wMax) {
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[bef][j] + v[i]);
      }
    }
  }

  // i番目までの頂点で、重さj以下のうち最大の価値がdp[i][j]
  for (int i = 1; i <= num1; i++) {
    for (int j = 0; j < wMax; j++) {
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
    }
  }

  int q;
  cin >> q;
  for (int l = 0; l < q; l++) {
    int x, L;
    cin >> x >> L;
    vector<P> wvList;
    wvList.emplace_back(P(0, 0));
    while (state[x]) {
      vector<P> wvList_tmp;
      for (auto wv : wvList) {
        if (wv.fs + w[x] <= L) {
          wvList_tmp.emplace_back(P(wv.fs + w[x], wv.sc + v[x]));
        }
      }
      x /= 2;
      for (auto wv : wvList_tmp) {
        wvList.emplace_back(wv);
      }
    }

    int res = 0;
    for (auto wv : wvList) {
      int remain = L - wv.fs;
      res = max(res, wv.sc + dp[x][remain]);
    }
    cout << res << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second
#define endl "\\n"

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  int depth = 1;
  for (int i = 1; i < 30; i++) {
    if ((1 << i) - 1 >= n) {
      depth = i;
      break;
    }
  }

  int d2 = depth / 2;
  int num1 = (1 << d2) - 1;
  int wMax = 100000;
  vector<vector<int>> dp(num1 + 1, vector<int>(wMax + 1, 0));

  vector<bool> state(n, true);
  for (int i = 1; i <= num1; i++) {
    state[i] = false;
  }

  dp[1][w[1]] = v[1];
  for (int i = 2; i <= num1; i++) {
    int bef = i / 2;
    for (int j = 0; j <= wMax; j++) {
      dp[i][j] = max(dp[i][j], dp[bef][j]);
      if (j + w[i] <= wMax) {
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[bef][j] + v[i]);
      }
    }
  }

  // i番目までの頂点で、重さj以下のうち最大の価値がdp[i][j]
  for (int i = 1; i <= num1; i++) {
    for (int j = 0; j < wMax; j++) {
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
    }
  }

  int q;
  cin >> q;
  for (int l = 0; l < q; l++) {
    int x, L;
    cin >> x >> L;
    vector<P> wvList;
    wvList.emplace_back(P(0, 0));
    while (state[x]) {
      vector<P> wvList_tmp;
      for (auto wv : wvList) {
        if (wv.fs + w[x] <= L) {
          wvList_tmp.emplace_back(P(wv.fs + w[x], wv.sc + v[x]));
        }
      }
      x /= 2;
      for (auto wv : wvList_tmp) {
        wvList.emplace_back(wv);
      }
    }

    int res = 0;
    for (auto wv : wvList) {
      int remain = L - wv.fs;
      res = max(res, wv.sc + dp[x][remain]);
    }
    cout << res << endl;
  }

  return 0;
}