Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int &i : c)
    cin >> i;
  vector<vector<int>> s(D, vector<int>(26));
  for (vector<int> &v : s)
    for (int &i : v)
      cin >> i;
  vector<int> t(D);
  vector<int> A(26, 0);
  int mzint = 0;
  for (int i = 0; i < D; i++) {
    int mzi = mzint, mzin = 0, k;
    for (int j = 0; j < 26; j++)
      mzi -= (i + 1 - A[j]) * c[j];
    for (int j = 0; j < 26; j++) {
      int l = mzi + s[i][j] - (i + 1 - A[j]) * c[j];
      if (l > mzin) {
        k = j;
        mzin = l;
      }
    }
    A[k] = i + 1;
    mzint = mzin;
    t[i] = k + 1;
  }
  int M = 8000000, ans = mzint;
  while (M--) {
    vector<pair<int, int>> AAA = {make_pair(rand() % D, rand() % 26),
                                  make_pair(rand() % D, rand() % 26)};
    vector<int> BBB = {t[AAA[0].first], t[AAA[1].first]};
    int ans2 = ans;
    for (int aaa = 0; aaa < 2; aaa++) {
      int d = AAA[aaa].first, q = AAA[aaa].second;
      ans += s[d][q] - s[d][t[d] - 1];
      int mae = -1, ato = D;
      for (int i = d - 1; i >= 0; i--) {
        if (t[i] == t[d]) {
          mae = i;
          break;
        }
      }
      for (int i = d + 1; i < D; i++) {
        if (t[i] == t[d]) {
          ato = i;
          break;
        }
      }
      ans -= (ato - d) * (d - mae) * c[t[d] - 1];
      mae = -1, ato = D;
      for (int i = d; i >= 0; i--) {
        if (t[i] == q + 1) {
          mae = i;
          break;
        }
      }
      for (int i = d; i < D; i++) {
        if (t[i] == q + 1) {
          ato = i;
          break;
        }
      }
      ans += (ato - d) * (d - mae) * c[q];
      t[d] = q + 1;
    }
    if (ans2 > ans) {
      ans = ans2;
      t[AAA[0].first] = BBB[0];
      t[AAA[1].first] = BBB[1];
    }
  }
  for (int i : t)
    cout << i << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int &i : c)
    cin >> i;
  vector<vector<int>> s(D, vector<int>(26));
  for (vector<int> &v : s)
    for (int &i : v)
      cin >> i;
  vector<int> t(D);
  vector<int> A(26, 0);
  int mzint = 0;
  for (int i = 0; i < D; i++) {
    int mzi = mzint, mzin = 0, k;
    for (int j = 0; j < 26; j++)
      mzi -= (i + 1 - A[j]) * c[j];
    for (int j = 0; j < 26; j++) {
      int l = mzi + s[i][j] - (i + 1 - A[j]) * c[j];
      if (l > mzin) {
        k = j;
        mzin = l;
      }
    }
    A[k] = i + 1;
    mzint = mzin;
    t[i] = k + 1;
  }
  int M = 7750000, ans = mzint;
  while (M--) {
    vector<pair<int, int>> AAA = {make_pair(rand() % D, rand() % 26),
                                  make_pair(rand() % D, rand() % 26)};
    vector<int> BBB = {t[AAA[0].first], t[AAA[1].first]};
    int ans2 = ans;
    for (int aaa = 0; aaa < 2; aaa++) {
      int d = AAA[aaa].first, q = AAA[aaa].second;
      ans += s[d][q] - s[d][t[d] - 1];
      int mae = -1, ato = D;
      for (int i = d - 1; i >= 0; i--) {
        if (t[i] == t[d]) {
          mae = i;
          break;
        }
      }
      for (int i = d + 1; i < D; i++) {
        if (t[i] == t[d]) {
          ato = i;
          break;
        }
      }
      ans -= (ato - d) * (d - mae) * c[t[d] - 1];
      mae = -1, ato = D;
      for (int i = d; i >= 0; i--) {
        if (t[i] == q + 1) {
          mae = i;
          break;
        }
      }
      for (int i = d; i < D; i++) {
        if (t[i] == q + 1) {
          ato = i;
          break;
        }
      }
      ans += (ato - d) * (d - mae) * c[q];
      t[d] = q + 1;
    }
    if (ans2 > ans) {
      ans = ans2;
      t[AAA[0].first] = BBB[0];
      t[AAA[1].first] = BBB[1];
    }
  }
  for (int i : t)
    cout << i << endl;
}