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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int D;
vector<int> down(26);
vector<vector<int>> up;

int main() {
  // std::chrono::system_clock::time_point  start, end; // 型は auto で可
  // start = std::chrono::system_clock::now();

  cin >> D;
  rep(i, 26) cin >> down.at(i);
  up.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) cin >> up.at(i).at(j);

  ll ans = 0;
  vector<ll> human(26, 0);
  rep(day, D) {
    rep(i, 26) {
      human.at(i) += down.at(i);
      ans -= human.at(i);
    }
    int choice = 0;
    rep(i, 26) {
      if (human.at(i) + up.at(day).at(i) >
          human.at(choice) + up.at(day).at(choice)) {
        choice = i;
      }
    }
    ans += human.at(choice) + up.at(day).at(choice);
    human.at(choice) = 0;
    cout << choice + 1 << endl;
  }

  // double elapsed;

  /*while(elapsed<1950){//時間が許す限り

      end = std::chrono::system_clock::now();  // 計測終了時間
      elapsed =
  std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
  }*/
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int D;
vector<int> c(26);
vector<vector<int>> s;
vector<int> spa(26, 0); // 経過日数
vector<int> t;
vector<set<int>> schedule(26); // 各コンテストが何日目に開催されるか
ll contest(int day,
           int x) { // day日にコンテストxを開催した時の満足度の増加量 1-indexed
  ll now = s.at(day - 1).at(x - 1);
  rep(i, 26) {
    if (i == x - 1) {
      spa.at(i) = 0;
      continue;
    }
    spa.at(i)++;
    now -= spa.at(i) * c.at(i);
  }
  return now;
}

int main() {
  std::chrono::system_clock::time_point start, end; // 型は auto で可
  start = std::chrono::system_clock::now();

  cin >> D;
  rep(i, 26) cin >> c.at(i);
  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) { cin >> s.at(i).at(j); }
  t.resize(D);

  // rep(i,D)cin>>t.at(i);
  /*rep(i,D){
      t.at(i)=rand()%26+1;
  }*/
  /*rep(i,D){
      rep(j,26){
          if(s.at(i).at(j)>s.at(i).at(t.at(i))){
              t.at(i)=j;
          }
      }
      t.at(i)++;
  }*/

  ll ans = 0;
  rep(i, D) {
    int h = 0;
    rep(j, 26) {
      if (s.at(i).at(j) + c.at(j) * spa.at(j) >
          s.at(i).at(h) + c.at(h) * spa.at(h)) {
        h = j;
      }
    }
    t.at(i) = h + 1;
    ans += contest(i + 1, t.at(i));
    // cout<<ans<<endl;問題A用
  }
  // 改善前のansを求める

  rep(i, 26) { schedule.at(i).insert(0); }
  rep(i, D) { schedule.at(t.at(i) - 1).insert(i + 1); }
  rep(i, 26) { schedule.at(i).insert(D + 1); }

  double elapsed;

  while (elapsed < 1950) { // 時間が許す限り
    int d, q;
    // cin>>d>>q;
    d = rand() % D + 1;
    q = t.at(d - 1);
    rep(i, 26) {
      if (schedule.at(i).size() < schedule.at(q).size()) {
        q = i;
      }
    }

    ll oldans = ans;

    int x = t.at(d - 1);
    t.at(d - 1) = q;
    int bx, nx;
    auto p =
        lower_bound(schedule.at(x - 1).begin(), schedule.at(x - 1).end(), d);
    p--;
    bx = *p;
    p++;
    p++;
    nx = *p;
    schedule.at(x - 1).erase(d);
    int bq, nq;
    auto itr =
        lower_bound(schedule.at(q - 1).begin(), schedule.at(q - 1).end(), d);
    nq = *itr;
    itr--;
    bq = *itr;
    schedule.at(q - 1).insert(d);
    ans += s.at(d - 1).at(q - 1) - s.at(d - 1).at(x - 1);
    ans += (d - bq) * (nq - d) * c.at(q - 1);
    ans -= (d - bx) * (nx - d) * c.at(x - 1);
    // cout<<ans<<endl;
    if (ans < oldans) {
      schedule.at(x - 1).insert(d);
      schedule.at(q - 1).erase(d);
      ans = oldans;
    }
    end = std::chrono::system_clock::now(); // 計測終了時間
    elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
                  .count();
  }
  rep(i, D) { cout << t.at(i) << endl; }
  // cout<<ans<<endl;
}