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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr long long INF = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

#include <random>

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int D;
  cin >> D;
  int c[26];
  int q[D];
  rep(i, 26) { cin >> c[i]; }
  int s[D][26];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  int ans = 0;
  int l[26]{};
  int y = 0;
  rep(i, D) {

    int an = -INF;
    int x = 0;
    rep(k, 26) {
      int a = ans;
      int la[26];
      rep(_, 26) { la[_] = l[_]; }
      a += s[i][k];
      rep(j, 26) {
        if (j == k) {
          la[k] = 0;
        } else {
          la[j]++;
          a -= la[j] * c[j];
        }
      }
      if (an < a) {
        x = k;
        an = a;
      }
    }
    y = x;
    ans += s[i][y];
    rep(j, 26) {
      if (j == y) {
        l[y] = 0;
      } else {
        l[j]++;
        ans -= l[j] * c[j];
      }
    }
    y++;
    q[i] = y;
  }

  int tr = 0;
  std::random_device rnd;
  while (tr <= 100000) {
    int g = rnd() % 26;
    int h = rnd() % 26;
    tr++;
    int ansm = 0;
    int r[D];
    rep(i, D) {
      if (i == g) {
        if (h != q[i]) {
          r[i] = h;
        } else {
          r[i] = rnd() % 26;
        }

      } else {
        r[i] = q[i];
      }
    }
    int lx[26]{};
    rep(i, D) {
      ansm += s[i][r[i]];
      rep(j, 26) {
        if (j == r[i]) {
          lx[r[i]] = 0;
        } else {
          lx[j]++;
          ansm -= lx[j] * c[j];
        }
      }
    }
    if (ansm > ans) {
      rep(i, D) { q[i] = r[i]; }
      ans = ansm;
    }
  }
  rep(i, D) { cout << q[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr long long INF = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

#include <random>

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int D;
  cin >> D;
  int c[26];
  int q[D];
  rep(i, 26) { cin >> c[i]; }
  int s[D][26];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  int ans = 0;
  int l[26]{};
  int y = 0;
  rep(i, D) {

    int an = -INF;
    int x = 0;
    rep(k, 26) {
      int a = ans;
      int la[26];
      rep(_, 26) { la[_] = l[_]; }
      a += s[i][k];
      rep(j, 26) {
        if (j == k) {
          la[k] = 0;
        } else {
          la[j]++;
          a -= la[j] * c[j];
        }
      }
      if (an < a) {
        x = k;
        an = a;
      }
    }
    y = x;
    ans += s[i][y];
    rep(j, 26) {
      if (j == y) {
        l[y] = 0;
      } else {
        l[j]++;
        ans -= l[j] * c[j];
      }
    }
    y++;
    q[i] = y;
  }

  int tr = 0;
  std::random_device rnd;
  while (tr <= 180000) {
    int g = rnd() % 26;
    int h = rnd() % 26;
    tr++;
    int ansm = 0;
    int r[D];
    rep(i, D) {
      if (i == g) {
        if (h != q[i]) {
          r[i] = h;
        } else {
          r[i] = rnd() % 26;
        }

      } else {
        r[i] = q[i];
      }
    }
    int lx[26]{};
    rep(i, D) {
      ansm += s[i][r[i]];
      rep(j, 26) {
        if (j == r[i]) {
          lx[r[i]] = 0;
        } else {
          lx[j]++;
          ansm -= lx[j] * c[j];
        }
      }
    }
    if (ansm > ans) {
      rep(i, D) { q[i] = r[i]; }
      ans = ansm;
    }
  }
  rep(i, D) { cout << q[i] << endl; }
}