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

/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
// #define endl '\\n'

int n;
int aa[300000];
int bb[300000];
int q;
int no, w;
vector<int> cc;
int dp[512][100001];
int kk;
int ans = 0;
void brute(int ind, int su = 0, int val = 0) {
  if (ind == cc.size()) {
    if (su <= w) {
      ans = max(ans, dp[kk][w - su] + val);
      // cout<<dp[kk][w-su]+val<<endl;
    }
  } else {
    brute(ind + 1, su + bb[cc[ind] - 1], val + aa[cc[ind] - 1]);
    brute(ind + 1, su, val);
  }
}
void calc(int no) {
  if (no > 511 or no > n) {
    return;
  }
  if (no > 1) {
    for (int i = 0; i <= 100000; i++) {
      dp[no][i] = dp[no / 2][i];
    }
  }
  for (int i = 100000; i >= bb[no - 1]; i--) {
    dp[no][i] = max(dp[no][i], dp[no][i - bb[no - 1]] + aa[no - 1]);
  }
  calc(no * 2 + 1);
  calc(no * 2);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> aa[i] >> bb[i];
  }
  cin >> q;
  calc(1);
  while (q--) {
    cin >> no >> w;

    if (no <= 511) {
      cout << dp[no][w] << endl;
      continue;
    }

    while (no > 511) {
      cc.pb(no);
      no /= 2;
    }
    kk = no;
    ans = 0;
    brute(0);
    cc.clear();
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\\n'

int n;
int aa[300000];
int bb[300000];
int q;
int no, w;
vector<int> cc;
vector<int> dd;
pair<int, int> kk[2000];
pair<int, int> mm[2000];

/*
vector<pair<int,int>> kk;
vector<pair<int,int>> mm;*/
int ss = 0;
int tt = 0;

void brute(int ind, int su = 0, int val = 0) {
  if (ind == cc.size()) {
    if (su <= w) {
      kk[ss] = {su, val};
      ss += 1;
    }

  } else {
    brute(ind + 1, su + bb[cc[ind] - 1], val + aa[cc[ind] - 1]);
    brute(ind + 1, su, val);
  }
}
void brute2(int ind, int su = 0, int val = 0) {
  if (ind == dd.size()) {
    if (su <= w) {
      mm[tt] = {su, val};
      tt += 1;
    }
  } else {
    brute2(ind + 1, su + bb[dd[ind] - 1], val + aa[dd[ind] - 1]);
    brute2(ind + 1, su, val);
  }
}
bool cmp(int ac, int bc) { return bb[ac] < bb[bc]; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> aa[i] >> bb[i];
  }
  cin >> q;
  while (q--) {

    cin >> no >> w;
    if (no == 1) {
      if (bb[0] <= w) {
        cout << aa[0] << endl;
      } else {
        cout << 0 << endl;
      }
      continue;
    }
    ss = 0;
    tt = 0;
    while (no > 0) {
      cc.pb(no);
      no /= 2;
    }
    int x = cc.size() / 2;
    if (cc.size() > 15) {
      x = 10;
    }
    while (cc.size() > x) {
      dd.pb(cc.back());
      cc.pop_back();
    }
    /*	sort(cc.begin(),cc.end(),cmp);
            sort(dd.begin(),dd.end(),cmp);*/
    //	reverse(cc.begin(),cc.end());
    reverse(dd.begin(), dd.end());
    brute(0);
    brute2(0);
    //	sort(kk.begin(),kk.end());
    //	sort(mm.begin(),mm.end());
    sort(kk, kk + ss);
    sort(mm, mm + tt);
    int ma = 0;
    /*		for(int i=0;i<ss;i++){
                            cout<<kk[i].a<<","<<kk[i].b<<endl;
                    }
                    for(int i=0;i<tt;i++){
                            cout<<mm[i].a<<","<<mm[i].b<<endl;
                    }*/
    for (int i = 0; i < ss; i++) {
      kk[i].b = max(kk[i].b, ma);
      ma = max(ma, kk[i].b);
    }
    ma = 0;
    for (int i = 0; i < tt; i++) {
      mm[i].b = max(mm[i].b, ma);
      ma = max(ma, mm[i].b);
    }
    /*	for(auto j:kk){
                    cout<<j.a<<","<<j.b<<endl;
            }
            for(auto j:mm){
                    cout<<j.a<<":"<<j.b<<endl;
            }*/
    int ans = 0;
    int ind = tt - 1;
    for (int i = 0; i < ss; i++) {
      /*			while(true){
                                      if(mm[ind].a+kk[i].a>w){
                                              ind--;
                                      }
                                      else{
                                              break;
                                      }
                              }*/
      int low = 0;
      int high = tt - 1;
      int indd;
      while (low < high) {
        if (abs(low - high) <= 3) {
          for (int jj = high; jj >= low; jj--) {
            if (mm[jj].a + kk[i].a <= w) {
              indd = jj;
              break;
            }
          }

          break;
          //	cout<<11<<endl;
        } else {
          int mid = (low + high) / 2;
          if (mm[mid].a + kk[i].a <= w) {
            low = mid;
          } else {
            high = mid;
          }
        }
      }
      // int x=lower_bound(mm,mm+tt,{l-kk[i].b+1,0})-mm-1;

      ans = max(ans, kk[i].b + mm[indd].b);
    }
    cout << ans << endl;
    cc.clear();
    dd.clear();
    /*	kk.clear();
            mm.clear();*/
  }

  return 0;
}