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

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

typedef long long LL;

const int N = 18, M = 262144;

int n, cq, a[M + 9], b[M + 9];
vector<int> qm[M + 9], qid[M + 9];

void into() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i], &b[i]);
  scanf("%d", &cq);
  for (int i = 1; i <= cq; ++i) {
    int x, m;
    scanf("%d%d", &x, &m);
    qm[x].push_back(m);
    qid[x].push_back(i);
  }
}

LL dp[N + 9][M + 9];

void Get_dp(int d, int k) {
  for (int i = 0; i <= M; ++i) {
    dp[d][i] = dp[d - 1][i];
    if (i >= b[k])
      dp[d][i] = max(dp[d][i], dp[d - 1][i - b[k]] + a[k]);
  }
}

vector<int> x, nw;
vector<LL> nv;

void Dfs_now(int k, LL v, int w) {
  if (w > M)
    return;
  if (k >= x.size()) {
    nv.push_back(v);
    nw.push_back(w);
    return;
  }
  Dfs_now(k + 1, v, w);
  Dfs_now(k + 1, v + a[x[k]], w + b[x[k]]);
}

LL ans[M + 9];

void Dfs_ans(int k) {
  if (k > n)
    return;
  x.push_back(k);
  nv.clear();
  nw.clear();
  Dfs_now(0, 0, 0);
  for (int vs = qm[k].size(), i = 0; i < vs; ++i) {
    int m = qm[k][i], id = qid[k][i];
    for (int vs = nv.size(), j = 0; j < vs; ++j)
      if (m >= nw[j])
        ans[id] = max(ans[id], nv[j] + dp[9][m - nw[j]]);
  }
  Dfs_ans(k << 1);
  Dfs_ans(k << 1 | 1);
  x.pop_back();
}

void Dfs_dp(int k, int d) {
  if (k > n)
    return;
  if (d > 9) {
    Dfs_ans(k);
    return;
  }
  Get_dp(d, k);
  for (int vs = qm[k].size(), i = 0; i < vs; ++i)
    ans[qid[k][i]] = dp[d][qm[k][i]];
  Dfs_dp(k << 1, d + 1);
  Dfs_dp(k << 1 | 1, d + 1);
}

void work() { Dfs_dp(1, 1); }

void outo() {
  for (int i = 1; i <= cq; ++i)
    printf("%lld\\n", ans[i]);
}

int main() {
  into();
  work();
  outo();
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long LL;

const int N = 18, M = 262144, K = 100000;

int n, cq, a[M + 9], b[M + 9];
vector<int> qm[M + 9], qid[M + 9];

void into() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i], &b[i]);
  scanf("%d", &cq);
  for (int i = 1; i <= cq; ++i) {
    int x, m;
    scanf("%d%d", &x, &m);
    qm[x].push_back(m);
    qid[x].push_back(i);
  }
}

LL dp[N + 9][K + 9];

void Get_dp(int d, int k) {
  for (int i = 0; i <= K; ++i) {
    dp[d][i] = dp[d - 1][i];
    if (i >= b[k])
      dp[d][i] = max(dp[d][i], dp[d - 1][i - b[k]] + a[k]);
  }
}

vector<int> x, nw;
vector<LL> nv;

void Dfs_now(int k, LL v, int w) {
  if (w > K)
    return;
  if (k >= x.size()) {
    nv.push_back(v);
    nw.push_back(w);
    return;
  }
  Dfs_now(k + 1, v, w);
  Dfs_now(k + 1, v + a[x[k]], w + b[x[k]]);
}

LL ans[N + 9];

void Dfs_ans(int k, int d) {
  if (k > n)
    return;
  x.push_back(k);
  nv.clear();
  nw.clear();
  Dfs_now(0, 0, 0);
  for (int vs = qm[k].size(), i = 0; i < vs; ++i) {
    int m = qm[k][i], id = qid[k][i];
    for (int vs = nv.size(), j = 0; j < vs; ++j)
      ans[id] = max(ans[id], nv[j] + dp[9][m - nw[j]]);
  }
  x.pop_back();
}

void Dfs_dp(int k, int d) {
  if (k > n)
    return;
  if (d > 9) {
    Dfs_ans(k, d);
    return;
  }
  Get_dp(d, k);
  for (int vs = qm[k].size(), i = 0; i < vs; ++i)
    ans[qid[k][i]] = dp[d][qm[k][i]];
  Dfs_dp(k << 1, d + 1);
  Dfs_dp(k << 1 | 1, d + 1);
}

void work() { Dfs_dp(1, 1); }

void outo() {
  for (int i = 1; i <= cq; ++i)
    printf("%lld\\n", ans[i]);
}

int main() {
  into();
  work();
  outo();
  return 0;
}