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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

int main() {
  int d;
  cin >> d;
  vector<int> c(26);
  vector<P> C;
  rep(i, 26) {
    cin >> c[i];
    C.emplace_back(c[i], i);
  }
  sort(all(C));
  reverse(all(C));
  vector<vector<int>> s(d);
  rep(i, d) rep(j, 26) {
    int a;
    cin >> a;
    s[i].push_back(a);
  }

  // vector<int> ld(26,0);
  vector<vector<int>> ds(26);
  vector<int> days(d, 25);
  // rep(i,d) days[i]=i%26;

  // ll ans=0;
  rep(i, d) {
    int t = days[i];
    ds[t].push_back(i);
    /*
    ld[t]=i+1;
    ans+=s[i][t];
    rep(j,26){
        ans-=c[j]*(i+1-ld[j]);
    }
    */
  }
  rep(i, 3000) {
    rep(ss, 26) rep(D, d) {
      int Q = C[ss].second;
      int pd = days[D];
      if (pd == Q)
        continue;
      int be, af, be2, af2;
      int r, l;
      l = -1;
      r = ds[pd].size();
      while (l + 1 < r) {
        int c = (l + r) / 2;
        if (ds[pd][c] <= D)
          l = c;
        else
          r = c;
      }
      if (r == ds[pd].size())
        af = d;
      else
        af = ds[pd][r];
      l = -1;
      r = ds[pd].size();
      while (l + 1 < r) {
        int c = (l + r) / 2;
        if (ds[pd][c] >= D)
          r = c;
        else
          l = c;
      }
      if (l == -1)
        be = -1;
      else
        be = ds[pd][l];

      int l2 = -1;
      r = ds[Q].size();
      while (l2 + 1 < r) {
        int c = (l2 + r) / 2;
        if (ds[Q][c] <= D)
          l2 = c;
        else
          r = c;
      }
      if (r == ds[Q].size())
        af2 = d;
      else
        af2 = ds[Q][r];
      l2 = -1;
      r = ds[Q].size();
      while (l2 + 1 < r) {
        int c = (l2 + r) / 2;
        if (ds[Q][c] >= D)
          r = c;
        else
          l2 = c;
      }
      if (l2 == -1)
        be2 = -1;
      else
        be2 = ds[Q][l2];

      if (c[Q] * (D - be2) * (af2 - D) + s[D][Q] - c[pd] * (D - be) * (af - D) -
              s[D][pd] >
          0) {
        days[D] = Q;
        ds[pd].erase(ds[pd].begin() + l + 1);
        ds[Q].insert(ds[Q].begin() + l2 + 1, D);
      }
    }
  }
  rep(i, d) cout << days[i] + 1 << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

int main() {
  int d;
  cin >> d;
  vector<int> c(26);
  vector<P> C;
  rep(i, 26) {
    cin >> c[i];
    C.emplace_back(c[i], i);
  }
  sort(all(C));
  reverse(all(C));
  vector<vector<int>> s(d);
  rep(i, d) rep(j, 26) {
    int a;
    cin >> a;
    s[i].push_back(a);
  }

  // vector<int> ld(26,0);
  vector<vector<int>> ds(26);
  vector<int> days(d, d - 1);
  // rep(i,d) days[i]=i%26;

  // ll ans=0;
  rep(i, d) {
    int t = days[i];
    ds[t].push_back(i);
    /*
    ld[t]=i+1;
    ans+=s[i][t];
    rep(j,26){
        ans-=c[j]*(i+1-ld[j]);
    }
    */
  }
  rep(i, 1) {
    rep(ss, 26) rep(D, d) {
      int Q = C[ss].second;
      int pd = days[D];
      if (pd == Q)
        continue;
      int be, af, be2, af2;
      int r, l;
      l = -1;
      r = ds[pd].size();
      while (l + 1 < r) {
        int c = (l + r) / 2;
        if (ds[pd][c] <= D)
          l = c;
        else
          r = c;
      }
      if (r == ds[pd].size())
        af = d;
      else
        af = ds[pd][r];
      l = -1;
      r = ds[pd].size();
      while (l + 1 < r) {
        int c = (l + r) / 2;
        if (ds[pd][c] >= D)
          r = c;
        else
          l = c;
      }
      if (l == -1)
        be = -1;
      else
        be = ds[pd][l];

      int l2 = -1;
      r = ds[Q].size();
      while (l2 + 1 < r) {
        int c = (l2 + r) / 2;
        if (ds[Q][c] <= D)
          l2 = c;
        else
          r = c;
      }
      if (r == ds[Q].size())
        af2 = d;
      else
        af2 = ds[Q][r];
      l2 = -1;
      r = ds[Q].size();
      while (l2 + 1 < r) {
        int c = (l2 + r) / 2;
        if (ds[Q][c] >= D)
          r = c;
        else
          l2 = c;
      }
      if (l2 == -1)
        be2 = -1;
      else
        be2 = ds[Q][l2];

      if (c[Q] * (D - be2) * (af2 - D) + s[D][Q] - c[pd] * (D - be) * (af - D) -
              s[D][pd] >
          0) {
        days[D] = Q;
        ds[pd].erase(ds[pd].begin() + l + 1);
        ds[Q].insert(ds[Q].begin() + l2 + 1, D);
      }
    }
  }
  rep(i, d) cout << days[i] + 1 << endl;
  return 0;
}