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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 5e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll V[1 << 18], W[1 << 18];
pair<ll, ll> DP[2050][2100];

int main() {
  init();
  ll N;
  cin >> N;
  rep(i, N) cin >> V[i] >> W[i];

  ll Q;
  cin >> Q;
  pair<ll, ll> m1[1 << 11], m2[1 << 11];
  ll vv[20], ww[20];

  if (N >= 1 << 11) {
    repe(di, 1024, min(N + 1, 2048ll)) {
      ll v = di, vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
      }
      rep(i, 1 << 11) {
        ll ws = 0, vs = 0;
        rep(j, 11) {
          if (1 << j & i) {
            ws += ww[j], vs += vv[j];
          }
        }
        DP[di][i] = {ws, vs};
      }
      DP[di][1 << 11] = {INF, INF};
      sort(DP[di], DP[di] + (1 << 11));

      ll t = 0;
      rep(i, 1 << 11) {
        if (!chmax(t, DP[di][i].second))
          DP[di][i].second = t;
      }
    }
  }

  rep(_, Q) {
    ll v, pv, L;
    cin >> v >> L;
    pv = v;
    if (v < 2048) {
      ll vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
      }

      ll s1 = vi / 2, s2 = (vi + 1) / 2;
      rep(i, 1 << s1) {
        ll vs = 0, ws = 0;
        rep(j, s1) {
          if (1 << j & i)
            vs += vv[j], ws += ww[j];
        }
        m1[i] = {ws, vs};
      }
      rep(i, 1 << s2) {
        ll vs = 0, ws = 0;
        rep(j, s2) {
          if (1 << j & i)
            vs += vv[s1 + j], ws += ww[s1 + j];
        }
        m2[i] = {ws, vs};
      }
      m2[1 << s2] = {INF, INF};

      sort(m1, m1 + (1 << s1)), sort(m2, m2 + (1 << s2) + 1);

      ll t = 0;
      rep(i, 1 << s1) {
        if (!chmax(t, m1[i].second))
          m1[i].second = t;
      }
      t = 0;
      rep(i, 1 << s2) {
        if (!chmax(t, m2[i].second))
          m2[i].second = t;
      }

      ll ans = 0;
      ll i2 = 0;
      while (m1[0].first + m2[i2 + 1].first <= L)
        i2++;

      rep(i1, 1 << s1) {
        if (m1[i1].first > L)
          break;
        while (m1[i1].first + m2[i2].first > L)
          i2--;
        chmax(ans, m1[i1].second + m2[i2].second);
      }
      cout << ans << '\\n';
    } else {
      ll vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
        if (v < 2048)
          break;
      }

      ll s = vi;
      rep(i, 1 << s) {
        ll ws = 0, vs = 0;
        rep(j, s) {
          if (1 << j & i) {
            ws += ww[j], vs += vv[j];
          }
        }
        m1[i] = {ws, vs};
      }

      sort(m1, m1 + (1 << s));
      ll t = 0;
      rep(i, 1 << s) {
        if (!chmax(t, m1[i].second))
          m1[i].second = t;
      }

      ll ans = 0;
      ll i2 = 0;
      while (m1[0].first + DP[v][i2 + 1].first <= L)
        i2++;

      rep(i1, 1 << s) {
        if (m1[i1].first > L)
          break;
        while (m1[i1].first + DP[v][i2].first > L)
          i2--;
        chmax(ans, m1[i1].second + DP[v][i2].second);
      }
      cout << ans << '\\n';
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 5e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll V[1 << 18], W[1 << 18];
pair<ll, ll> DP[2050][2100];

int main() {
  init();
  ll N;
  cin >> N;
  rep(i, N) cin >> V[i] >> W[i];

  ll Q;
  cin >> Q;
  pair<ll, ll> m1[1 << 11], m2[1 << 11];
  ll vv[20], ww[20];

  if (N >= 1 << 11) {
    repe(di, 1024, min(N, 2047ll)) {
      ll v = di, vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
      }
      rep(i, 1 << 11) {
        ll ws = 0, vs = 0;
        rep(j, 11) {
          if (1 << j & i) {
            ws += ww[j], vs += vv[j];
          }
        }
        DP[di][i] = {ws, vs};
      }
      DP[di][1 << 11] = {INF, INF};
      sort(DP[di], DP[di] + (1 << 11));

      ll t = 0;
      rep(i, 1 << 11) {
        if (!chmax(t, DP[di][i].second))
          DP[di][i].second = t;
      }
    }
  }

  rep(_, Q) {
    ll v, pv, L;
    cin >> v >> L;
    pv = v;
    if (v < 2048) {
      ll vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
      }

      ll s1 = vi / 2, s2 = (vi + 1) / 2;
      rep(i, 1 << s1) {
        ll vs = 0, ws = 0;
        rep(j, s1) {
          if (1 << j & i)
            vs += vv[j], ws += ww[j];
        }
        m1[i] = {ws, vs};
      }
      rep(i, 1 << s2) {
        ll vs = 0, ws = 0;
        rep(j, s2) {
          if (1 << j & i)
            vs += vv[s1 + j], ws += ww[s1 + j];
        }
        m2[i] = {ws, vs};
      }
      m2[1 << s2] = {INF, INF};

      sort(m1, m1 + (1 << s1)), sort(m2, m2 + (1 << s2) + 1);

      ll t = 0;
      rep(i, 1 << s1) {
        if (!chmax(t, m1[i].second))
          m1[i].second = t;
      }
      t = 0;
      rep(i, 1 << s2) {
        if (!chmax(t, m2[i].second))
          m2[i].second = t;
      }

      ll ans = 0;
      ll i2 = 0;
      while (m1[0].first + m2[i2 + 1].first <= L)
        i2++;

      rep(i1, 1 << s1) {
        if (m1[i1].first > L)
          break;
        while (m1[i1].first + m2[i2].first > L)
          i2--;
        chmax(ans, m1[i1].second + m2[i2].second);
      }
      cout << ans << '\\n';
    } else {
      ll vi = 0;
      while (v) {
        vv[vi] = V[v - 1], ww[vi] = W[v - 1];
        v /= 2, vi++;
        if (v < 2048)
          break;
      }

      ll s = vi;
      rep(i, 1 << s) {
        ll ws = 0, vs = 0;
        rep(j, s) {
          if (1 << j & i) {
            ws += ww[j], vs += vv[j];
          }
        }
        m1[i] = {ws, vs};
      }

      sort(m1, m1 + (1 << s));
      ll t = 0;
      rep(i, 1 << s) {
        if (!chmax(t, m1[i].second))
          m1[i].second = t;
      }

      ll ans = 0;
      ll i2 = 0;
      while (m1[0].first + DP[v][i2 + 1].first <= L)
        i2++;

      rep(i1, 1 << s) {
        if (m1[i1].first > L)
          break;
        while (m1[i1].first + DP[v][i2].first > L)
          i2--;
        chmax(ans, m1[i1].second + DP[v][i2].second);
      }
      cout << ans << '\\n';
    }
  }
}