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 mod = 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++) {
    int mx = 0;
    int as = 0;
    for (int j = 0; j < 26; j++) {
      if (mx < s[i][j]) {
        mx = s[i][j];
        as = j;
      }
    }
    t[i] = as + 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]);
    }
  }
  for (int z = 0; z < 666666; z++) {
    int M = 3;
    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;
      }
    }
    int Z = 1;
    for (int i = 0; i < Z; 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;
  }
}

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 mod = 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++) {
    int mx = 0;
    int as = 0;
    for (int j = 0; j < 26; j++) {
      if (mx < s[i][j]) {
        mx = s[i][j];
        as = j;
      }
    }
    t[i] = as + 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]);
    }
  }
  for (int z = 0; z < 700000; z++) {
    int Z = 2;
    for (int i = 0; i < Z; 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;
      }
    }
    int M = 2;
    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;
  }
}