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

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

long long D;
long long c[26];
long long s[366][26];
long long t[366];
long long score() {
  long long last[D + 1][26];
  fill(last[0], last[0] + 26, 0);
  long long ret = 0;
  for (int d = 0; d < D; d++) {
    ret += s[d][t[d]];
    for (int i = 0; i < 26; i++) {
      last[d + 1][i] = last[d][i];
    }
    last[d + 1][t[d]] = d + 1;
    for (int i = 0; i < 26; i++) {
      ret -= c[i] * (d + 1 - last[d + 1][i]);
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> D;
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  for (int d = 0; d < D; d++) {
    for (int i = 0; i < 26; i++) {
      cin >> s[d][i];
    }
  }
  for (int d = 0; d < D; d++) {
    long long last[26];
    fill(last, last + 26, 0);
    for (int d = 0; d < D; d++) {
      int max_i = 0;
      long long max_v = 0;
      for (int i = 0; i < 26; i++) {
        long long tmp = c[i] * (d + 1 - last[i]) + s[d][i];
        if (max_v < tmp) {
          max_v = tmp;
          max_i = i;
        }
        last[max_i] = d + 1;
        t[d] = max_i;
      }
    }
  }
  long long cur_score = score();
  int M = 100000;
  random_device seed_gen;
  mt19937 engine(seed_gen());
  for (int i = 0; i < M; i++) {
    int d, q;
    d = engine() % D;
    q = engine() % 26;
    int old = t[d];
    t[d] = q;
    long long new_score = score();
    if (cur_score > new_score) {
      t[d] = old;
    } else {
      cur_score = new_score;
    }
  }
  for (int d = 0; d < D; d++)
    cout << t[d] + 1 << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

long long D;
long long c[26];
long long s[357][26];
long long t[357];
long long score() {
  long long last[D + 1][26];
  fill(last[0], last[0] + 26, 0);
  long long ret = 0;
  for (int d = 0; d < D; d++) {
    ret += s[d][t[d]];
    for (int i = 0; i < 26; i++) {
      last[d + 1][i] = last[d][i];
    }
    last[d + 1][t[d]] = d + 1;
    for (int i = 0; i < 26; i++) {
      ret -= c[i] * (d + 1 - last[d + 1][i]);
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> D;
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  for (int d = 0; d < D; d++) {
    for (int i = 0; i < 26; i++) {
      cin >> s[d][i];
    }
  }
  for (int d = 0; d < D; d++) {
    long long last[26];
    fill(last, last + 26, 0);
    for (int d = 0; d < D; d++) {
      int max_i = 0;
      long long max_v = 0;
      for (int i = 0; i < 26; i++) {
        long long tmp = c[i] * (d + 1 - last[i]) + s[d][i];
        if (max_v < tmp) {
          max_v = tmp;
          max_i = i;
        }
        last[max_i] = d + 1;
        t[d] = max_i + 1;
      }
    }
  }
  long long cur_score = score();
  int M = 100000;
  random_device seed_gen;
  mt19937 engine(seed_gen());
  for (int i = 0; i < M; i++) {
    int d, q;
    d = engine() % D;
    q = engine() % 26;
    int old = t[d];
    t[d] = q;
    long long new_score = score();
    if (cur_score > new_score) {
      t[d] = old;
    } else {
      cur_score = new_score;
    }
  }
  for (int d = 0; d < D; d++)
    cout << t[d] << endl;
  return 0;
}