Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define SUM(x) ((x) * (x + 1) / 2)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(D, vector<int>(26));
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];

  vector<int> t(D);
  {
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      int mx = -1000000000, ti = 0;
      for (int k = 0; k < 26; k++) {
        int sd = s[i - 1][k];
        for (int l = 0; l < 26; l++) {
          if (l == k)
            continue;
          sd -= c[l] * (i - last[l]);
        }
        if (sd > mx) {
          mx = sd;
          ti = k;
        }
      }
      last[ti] = i;
      t[i - 1] = ti + 1;
    }
  }

  int score = 0;
  {
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      score += s[i - 1][t[i - 1] - 1];
      last[t[i - 1] - 1] = i;
      for (int j = 0; j < 26; j++)
        score -= c[j] * (i - last[j]);
    }
  }
  int m = 1000000;
  mt19937 mt((int)time(0));
  for (int i = 0; i < m; i++) {
    int d = mt() % D + 1, q = mt() % 26 + 1;
    int p = t[d - 1];
    t[d - 1] = q;

    int score2 = 0;
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      score2 += s[i - 1][t[i - 1] - 1];
      last[t[i - 1] - 1] = i;
      for (int j = 0; j < 26; j++)
        score2 -= c[j] * (i - last[j]);
    }
    if (score < score2)
      score = score2;
    else
      t[d - 1] = p;
  }

  for (int i = 0; i < D; i++)
    cout << t[i] << endl;

  // cout << "score:" << score << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define SUM(x) ((x) * (x + 1) / 2)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(D, vector<int>(26));
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];

  vector<int> t(D);
  {
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      int mx = -1000000000, ti = 0;
      for (int k = 0; k < 26; k++) {
        int sd = s[i - 1][k];
        for (int l = 0; l < 26; l++) {
          if (l == k)
            continue;
          sd -= c[l] * (i - last[l]);
        }
        if (sd > mx) {
          mx = sd;
          ti = k;
        }
      }
      last[ti] = i;
      t[i - 1] = ti + 1;
    }
  }

  int score = 0;
  {
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      score += s[i - 1][t[i - 1] - 1];
      last[t[i - 1] - 1] = i;
      for (int j = 0; j < 26; j++)
        score -= c[j] * (i - last[j]);
    }
  }
  int m = 100000;
  mt19937 mt((int)time(0));
  for (int i = 0; i < m; i++) {
    int d = mt() % D + 1, q = mt() % 26 + 1;
    int p = t[d - 1];
    t[d - 1] = q;

    int score2 = 0;
    vector<int> last(26, 0);
    for (int i = 1; i <= D; i++) {
      score2 += s[i - 1][t[i - 1] - 1];
      last[t[i - 1] - 1] = i;
      for (int j = 0; j < 26; j++)
        score2 -= c[j] * (i - last[j]);
    }
    if (score < score2)
      score = score2;
    else
      t[d - 1] = p;
  }

  for (int i = 0; i < D; i++)
    cout << t[i] << endl;

  // cout << "score:" << score << endl;

  return 0;
}