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

//
// Created by yamunaku on 2020/06/13.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main() {
  // CFS;
  int n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  int ppp = (1 << 9) - 2;
  vector<vector<pair<int, int>>> dp1(ppp + 1);
  dp1[0].push_back({0, 0});
  dp1[0].push_back({w[0], v[0]});
  repl(i, 1, ppp + 1) {
    int p = (i - 1) / 2;
    for (auto &c : dp1[p]) {
      dp1[i].push_back({c.first, c.second});
      dp1[i].push_back({c.first + w[i], c.second + v[i]});
    }
    sort(all(dp1[i]));
  }
  int q;
  cin >> q;
  rep(t, q) {
    int s, l;
    cin >> s >> l;
    s--;
    if (s <= ppp) {
      vector<pair<int, int>> dp2;
      dp2.push_back({0, 0});
      while (1) {
        per(k, dp2.size()) {
          if (dp2[k].first + w[s] <= l)
            dp2.push_back({dp2[k].first + w[s], dp2[k].second + v[s]});
        }
        if (s == 0)
          break;
        s = (s - 1) / 2;
      }
      int ans = 0;
      for (auto &c : dp2) {
        if (c.second > ans)
          ans = c.second;
      }
      cout << ans << endl;
      continue;
    }
    vector<pair<int, int>> dp2;
    dp2.push_back({0, 0});
    while (s > ppp) {
      per(k, dp2.size()) {
        if (dp2[k].first + w[s] <= l)
          dp2.push_back({dp2[k].first + w[s], dp2[k].second + v[s]});
      }
      s = (s - 1) / 2;
    }
    int ans = 0;
    sort(all(dp2));
    int idx = 0;
    int ma = 0;
    int sz = dp1[s].size();
    per(i, dp2.size()) {
      while (idx < sz - 1) {
        if (dp1[s][idx + 1].first + dp2[i].first > l)
          break;
        ma = max(ma, dp1[s][idx + 1].second);
        idx++;
      }
      //            cout << s SP i SP ma SP l - dp2[i].first SP dp2[i].first SP
      //            dp2[i].second << endl;
      ans = max(ans, ma + dp2[i].second);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

//
// Created by yamunaku on 2020/06/13.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main() {
  // CFS;
  int n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  vector<vector<pair<int, int>>> dp1((1 << 9) - 2);
  dp1[0].push_back({0, 0});
  dp1[0].push_back({w[0], v[0]});
  repl(i, 1, n) {
    int p = (i - 1) / 2;
    for (auto &c : dp1[p]) {
      dp1[i].push_back({c.first, c.second});
      dp1[i].push_back({c.first + w[i], c.second + v[i]});
    }
    sort(all(dp1[i]));
  }
  int q;
  cin >> q;
  rep(t, q) {
    int s, l;
    cin >> s >> l;
    s--;
    if (s <= (1 << 9) - 2) {
      vector<pair<int, int>> dp2;
      dp2.push_back({0, 0});
      while (1) {
        per(k, dp2.size()) {
          if (dp2[k].first + w[s] <= l)
            dp2.push_back({dp2[k].first + w[s], dp2[k].second + v[s]});
        }
        if (s == 0)
          break;
        s = (s - 1) / 2;
      }
      int ans = 0;
      for (auto &c : dp2) {
        if (c.second > ans)
          ans = c.second;
      }
      cout << ans << endl;
      continue;
    }
    vector<pair<int, int>> dp2;
    dp2.push_back({0, 0});
    while (s > (1 << 9) - 2) {
      per(k, dp2.size()) {
        if (dp2[k].first + w[s] <= l)
          dp2.push_back({dp2[k].first + w[s], dp2[k].second + v[s]});
      }
      s = (s - 1) / 2;
    }
    int ans = 0;
    sort(all(dp2));
    int idx = 0;
    int ma = 0;
    per(i, dp2.size()) {
      while (idx < n - 1) {
        if (dp1[s][idx + 1].first + dp2[i].first > l)
          break;
        ma = max(ma, dp1[s][idx].second);
        idx++;
      }
      ans = max(ans, dp2[i].second);
    }
    cout << ans << endl;
  }
  return 0;
}