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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

class Schedular {
public:
  int D;
  vi c;
  vvi s;
  vvi hist;
  vi t;
  Schedular(int d) : D(d), c(26), s(D, vi(26)), t(D), hist(26) {
    cin >> c;
    REP(i, D)
    REP(j, 26) { cin >> s[i][j]; }
  }

  void out() {
    REP(i, D) { cout << t[i] + 1 << endl; }
  }

  void init() {
    vi last(26, -1);
    int score = 0;
    REP(d, D) {
      REP(j, 26) { score -= c[j] * (d - last[j]); }
      int mx = 0;
      int ind = 0;
      REP(j, 26) {
        int diff = s[d][j] + c[j] * (d - last[j]);
        if (diff > mx) {
          mx = diff;
          ind = j;
        }
      }
      score += s[d][ind] + c[ind] * (d - last[ind]);
      last[ind] = d;
      t[d] = ind;
    }
  }

  int calcScore(int dN) {
    vi last(26, -1);
    int score = 0;
    REP(d, dN) {
      score += s[d][t[d]];
      last[t[d]] = d;
      REP(j, 26) { score -= c[j] * (d - last[j]); }
    }
    return score;
  }

  int replace(int d, int q) {
    assert(0 <= d && d <= D);
    assert(0 <= q && q <= 26);
  }

  void build() {
    REP(i, 26)
    hist[i].push_back(-1);
  }
};

signed main() {
  cin >> D;
  Schedular s(D);
  s.init();
  s.out();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

class contest {
public:
  vector<int> c;
  vector<int> t;
  vector<vector<int>> s;
  vector<list<int>> memLast;
  int score = 0;
  contest() : c(26), s(D, vi(26)), t(D), memLast(26) {}

  void init(vector<int> &t_) {
    t = t_;
    REP(i, D) { memLast[t[i]].push_back(i + 1); }
  }

  int calcScore() {
    vector<int> last(26, 0);
    int res = 0;
    REP(i, D) {
      int score = s[i][t[i]];
      res += score;
      last[t[i]] = i + 1;
      REP(j, 26) { res -= c[j] * (i + 1 - last[j]); }
      // cout << res << endl;
    }
    score = res;
    return res;
  }

  void greedy() {
    REP(i, D) {
      int mx = 0;
      REP(j, 26) {
        int diff = update(i + 1, j);
        if (mx < diff) {
          mx = diff;
          t[i] = j;
        }
      }
      score += mx;
    }
  }

  int update(int d, int q) {
    int diff = 0;
    dump(memLast);
    int next_d = 0, prev_d = 0;
    if (q == t[d - 1])
      return diff;
    int ct = t[d - 1];
    diff -= s[d - 1][ct];
    auto tmp = find(ALL(memLast[ct]), d);
    dump(ct, t);
    list<int>::iterator itr = memLast[ct].erase(tmp);
    if (itr == memLast[ct].end()) {
      next_d = D + 1;
    } else {
      next_d = *itr;
    }
    if (itr == memLast[ct].begin()) {
      prev_d = 0;
    } else {
      prev_d = *(--itr);
    }
    diff -= (d - prev_d) * c[ct] * (next_d - d);
    dump(next_d, d, prev_d);

    t[d - 1] = q;
    diff += s[d - 1][q];
    itr = memLast[q].end();
    for (auto it = memLast[q].begin(); it != memLast[q].end(); it++) {
      if (*it > d) {
        itr = it;
        break;
      }
    }
    memLast[q].insert(itr, d);
    if (itr == memLast[q].end()) {
      next_d = D + 1;
    } else {
      next_d = *itr;
    }
    itr--;
    if (itr == memLast[q].begin()) {
      prev_d = 0;
    } else {
      prev_d = *(--itr);
    }
    diff += (d - prev_d) * c[q] * (next_d - d);
    dump(next_d, d, prev_d);
  }
};

signed main() {
  cin >> D;
  contest atc;
  cin >> atc.c;
  REP(i, D) { cin >> atc.s[i]; }
  vector<int> t(D, 0);
  atc.init(t);
  atc.greedy();
  REP(i, D) { cout << atc.t[i] << endl; }
}