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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, c[30], s[400][30];
int d[30];
int ans[400];

int score() {
  int res = 0;
  int dd[30];
  for (int i = 1; i <= n; i++) {
    res += s[i][ans[i]];
    d[ans[i]] = i;
    for (int j = 1; j <= 26; j++)
      res -= c[j] * (i - d[j]);
  }
  return res;
}

signed main() {
  std::random_device rnd;
  cin >> n;
  for (int i = 1; i <= 26; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 26; j++)
      cin >> s[i][j];
  for (int i = 1; i <= n; i++) {
    int ma = -1000000000, p = 0;
    for (int j = 1; j <= 26; j++) {
      int cnt = 0;
      for (int k = 1; k <= 26; k++)
        if (j != k)
          cnt -= c[k] * (i - d[k]);
      cnt += s[i][j];
      if (ma < cnt) {
        ma = cnt;
        p = j;
      }
    }
    ans[i] = p;
    d[p] = i;
  }
  int q = 140000;
  while (q) {
    int x = rnd() % 365 + 1, y = rnd() % 26 + 1;
    int g = ans[x];
    int f = score();
    ans[x] = y;
    if (score() < f)
      ans[x] = g;
    q--;
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  // cout << score() << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, c[30], s[400][30];
int d[30];
int ans[400];

int score() {
  int res = 0;
  int dd[30];
  for (int i = 1; i <= n; i++) {
    res += s[i][ans[i]];
    d[ans[i]] = i;
    for (int j = 1; j <= 26; j++)
      res -= c[j] * (i - d[j]);
  }
  return res;
}

signed main() {
  std::random_device rnd;
  cin >> n;
  for (int i = 1; i <= 26; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 26; j++)
      cin >> s[i][j];
  for (int i = 1; i <= n; i++) {
    int ma = -1000000000, p = 0;
    for (int j = 1; j <= 26; j++) {
      int cnt = 0;
      for (int k = 1; k <= 26; k++)
        if (j != k)
          cnt -= c[k] * (i - d[k]);
      cnt += s[i][j];
      if (ma < cnt) {
        ma = cnt;
        p = j;
      }
    }
    ans[i] = p;
    d[p] = i;
  }
  int q = 10000;
  while (q) {
    int x = rnd() % 365 + 1, y = rnd() % 26 + 1;
    int g = ans[x];
    int f = score();
    ans[x] = y;
    if (score() < f)
      ans[x] = g;
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}