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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
ll V[(1 << 18) + 10], W[(1 << 18) + 10];
ll Q;
ll dp[(1 << 9) + 10][100010];

void dfs1(ll v, ll p = 0, ll dep = 0) {
  if (dep >= (N + 1) / 2)
    return;

  rep(j, 100001) {
    chmax(dp[v][j], dp[p][j]);

    if (j - W[v] >= 0)
      chmax(dp[v][j], dp[p][j - W[v]] + V[v]);
  }

  for (auto u : {2 * v, 2 * v + 1}) {
    dfs1(u, v, dep + 1);
  }

  return;
}

ll depth[(1 << 18) + 1];
std::map<ll, std::vector<ll>> mp;
std::map<ll, ll> par;

int main() {
  std::cin >> M;

  ll two = 1, cnt = -1;

  repi(i, 1, M + 1) {
    if (i == two)
      two <<= 1, ++cnt;

    depth[i] = cnt;
  }

  N = depth[M];

  rep(i, M) std::cin >> V[i + 1] >> W[i + 1];

  dfs1(1);

  std::cin >> Q;

  rep(q, Q) {
    ll v, L;
    std::cin >> v >> L;

    std::vector<ll> vs;

    while (depth[v] >= (N + 1) / 2 && v > 0) {
      vs.emplace_back(v);
      v >>= 1;
    }

    ll ans = 0;

    rep(bit, 1 << (vs.size())) {
      ll wsum = 0, vsum = 0;

      rep(j, vs.size()) if (bit >> j & 1) {
        wsum += W[vs[j]];
        vsum += V[vs[j]];
      }

      if (wsum <= L) {
        chmax(ans, vsum + dp[v][L - wsum]);
      }
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
ll V[(1 << 18) + 10], W[(1 << 18) + 10];
ll Q;
ll dp[(1 << 9) + 10][100010];
ll ans[100010];
std::vector<P> qry[(1 << 18) + 10];
bool placed[(1 << 18) + 10];

void dfs1(ll v, ll p = 0, ll dep = 0) {
  if (dep >= N / 2)
    return;

  rep(j, 100001) {
    chmax(dp[v][j], dp[p][j]);

    if (j - W[v] >= 0)
      chmax(dp[v][j], dp[p][j - W[v]] + V[v]);
  }

  for (auto u : {2 * v, 2 * v + 1}) {
    dfs1(u, v, dep + 1);
  }

  return;
}

std::vector<ll> vs;

void dfs2(ll v, ll p = 0, ll dep = 0) {
  if (!placed[v])
    return;

  if (dep >= N / 2)
    vs.emplace_back(v);

  for (auto pr : qry[v]) {
    ll L = pr.first;
    ll q = pr.second;

    rep(bit, 1 << (vs.size())) {
      ll wsum = 0, vsum = 0;

      rep(j, vs.size()) if (bit >> j & 1) {
        wsum += W[vs[j]];
        vsum += V[vs[j]];

        if (wsum > L)
          break;
      }

      if (wsum > L)
        continue;

      chmax(ans[q], vsum + dp[p][L - wsum]);
    }
  }

  for (auto u : {2 * v, 2 * v + 1}) {
    dfs2(u, dep == N / 2 - 1 ? v : dep >= N / 2 ? p : -1, dep + 1);
  }

  if (dep >= N / 2)
    vs.pop_back();
}

int main() {
  std::cin >> M;

  ll two = 1;

  while (two <= M) {
    two <<= 1;
    ++N;
  }

  if (two != M)
    --N;

  rep(i, M) std::cin >> V[i + 1] >> W[i + 1];

  std::cin >> Q;

  rep(q, Q) {
    ll v, L;
    std::cin >> v >> L;

    qry[v].emplace_back(L, q);

    while (v > 0) {
      placed[v] = true;
      v >>= 1;
    }
  }

  dfs1(1);
  dfs2(1);

  rep(q, Q) std::cout << ans[q] << std::endl;

  return 0;
}