Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed 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);
  for (int i = 0; i < D; i++) {
    t[i] = rand() % 26 + 1;
  }
  int ans = 0;
  vector<int> Last(26);
  for (int i = 0; i < D; i++) {
    ans += s[i][t[i] - 1];
    Last[t[i] - 1] = i + 1;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * (i + 1 - Last[j]);
    }
  }
  int M = 300000;
  vector<P> horyuu;
  int cnt = 0;
  for (int i = 0; i < M; i++) {
    int d = rand() % D + 1;
    int q = rand() % 26 + 1;
    int ord = ans;
    int X = 0;
    int Y = 0;
    int A = 0;
    int B = 0;
    ans += s[d - 1][q - 1] - s[d - 1][t[d - 1] - 1];
    for (int j = 0; j < D; j++) {
      if (t[j] == t[d - 1]) {
        if (j == d - 1) {
          Y = j + 1;
        } else {
          X = j + 1;
        }
        A = j + 1;
      }
      if (t[j] == q) {
        Y = j + 1;
        B = j + 1;
      }
      ans -= c[t[d - 1] - 1] * (j + 1 - X) - c[t[d - 1] - 1] * (j + 1 - A);
      ans += c[q - 1] * (j + 1 - B) - c[q - 1] * (j + 1 - Y);
    }
    if (ans > ord) {
      ord = ans;
      cnt++;
      horyuu.push_back({d, q});
    } else {
      ans = ord;
    }
    if (cnt == 4) {
      cnt = 0;
      t[horyuu[3].first - 1] = horyuu[3].second;
      horyuu = {};
    }
  }
  for (int i = 0; i < D; i++) {
    cout << t[i] << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed 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);
  for (int i = 0; i < D; i++) {
    t[i] = rand() % 26 + 1;
  }
  int ans = 0;
  vector<int> Last(26);
  for (int i = 0; i < D; i++) {
    ans += s[i][t[i] - 1];
    Last[t[i] - 1] = i + 1;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * (i + 1 - Last[j]);
    }
  }
  int M = 30000000;
  for (int i = 0; i < M; i++) {
    int d = rand() % D + 1;
    int q = rand() % 26 + 1;
    int ord = ans;
    int X = 0;
    int Y = 0;
    int A = 0;
    int B = 0;
    ans += s[d - 1][q - 1] - s[d - 1][t[d - 1] - 1];
    for (int j = 0; j < D; j++) {
      if (t[j] == t[d - 1]) {
        if (j == d - 1) {
          Y = j + 1;
        } else {
          X = j + 1;
        }
        A = j + 1;
      }
      if (t[j] == q) {
        Y = j + 1;
        B = j + 1;
      }
      ans -= c[t[d - 1] - 1] * (j + 1 - X) - c[t[d - 1] - 1] * (j + 1 - A);
      ans += c[q - 1] * (j + 1 - B) - c[q - 1] * (j + 1 - Y);
    }
    if (ans > ord) {
      ord = ans;
      t[d - 1] = q;
    } else {
      ans = ord;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << t[i] << endl;
  }
}