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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS const int A = 26;
  const int M1 = 2e1;
  const int M2 = 1e4;
  int D;
  cin >> D;
  vector<int> c(A);
  REP(i, A) cin >> c[i];
  vector<vector<int>> s(D, vector<int>(A));
  REP(i, D) REP(j, A) cin >> s[i][j];

  vector<int> last(A, -1);
  vector<int> ans(D);
  REP(i, D) {
    int mx = 0, a = -1;
    REP(j, A) {
      int tmp = (i - last[j]) * c[j] + s[i][j];
      if (mx < tmp) {
        mx = tmp;
        a = j;
      }
    }
    ans[i] = a;
    last[a] = i;
  }
  auto calc = [&](const vector<int> &a) {
    vector<int> l(A, -1);
    int score = 0;
    REP(i, D) {
      score += s[i][a[i]];
      l[a[i]] = i;
      REP(j, A) { score -= c[j] * (i - l[j]); }
    }
    return score;
  };

  int maxscore = calc(ans);

  random_device seed_gen;
  mt19937 engine(seed_gen());
  REP(_, M1) {
    int mx = maxscore;
    vector<int> a1(ans);
    REP(__, M2) {
      vector<int> a2(a1);
      a2[engine() % D] = engine() % A;
      int tmp = calc(a2);
      if (mx < tmp) {
        swap(a1, a2);
        mx = tmp;
      }
    }
    if (maxscore < mx) {
      swap(ans, a1);
      maxscore = mx;
    }
  }

  REP(i, D)
  OUT(ans[i] + 1)

      return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS const int A = 26;
  const int M1 = 2e2;
  const int M2 = 2e3;
  int D;
  cin >> D;
  vector<int> c(A);
  REP(i, A) cin >> c[i];
  vector<vector<int>> s(D, vector<int>(A));
  REP(i, D) REP(j, A) cin >> s[i][j];

  vector<int> last(A, -1);
  vector<int> ans(D);
  REP(i, D) {
    int mx = 0, a = -1;
    REP(j, A) {
      int tmp = (i - last[j]) * c[j] + s[i][j];
      if (mx < tmp) {
        mx = tmp;
        a = j;
      }
    }
    ans[i] = a;
    last[a] = i;
  }
  auto calc = [&](const vector<int> &a) {
    vector<int> l(A, -1);
    int score = 0;
    REP(i, D) {
      score += s[i][a[i]];
      l[a[i]] = i;
      REP(j, A) { score -= c[j] * (i - l[j]); }
    }
    return score;
  };

  int maxscore = calc(ans);

  random_device seed_gen;
  mt19937 engine(seed_gen());
  REP(_, M1) {
    int mx = maxscore;
    vector<int> a1(ans);
    REP(__, M2) {
      vector<int> a2(a1);
      a2[engine() % D] = engine() % A;
      int tmp = calc(a2);
      if (mx < tmp) {
        swap(a1, a2);
        mx = tmp;
      }
    }
    if (maxscore < mx) {
      swap(ans, a1);
      maxscore = mx;
    }
  }

  REP(i, D)
  OUT(ans[i] + 1)

      return 0;
}