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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int d;
int c[26], s[365][26], last[26], t[365];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    int score;
    for (int j = 0; j < 26; j++) {
      int tscore = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (k == j)
          continue;
        tscore -= c[k] * (i + 1 - last[k]);
      }
      if (j == 0)
        score = tscore;
      else if (tscore > score) {
        score = tscore;
        t[i] = j;
      }
    }
    last[t[i]] = i + 1;
  }
  int cnt = 100000;
  while (cnt--) {
    int day = rand() % d;
    int v = rand() % 26;
    if (t[day] == v)
      continue;
    int dscore = 0;
    int check[2] = {v, t[day]};
    int pscore = 0;
    int cscore = 0;
    for (int i = 0; i < 2; i++)
      last[check[i]] = 0;
    for (int i = 0; i < d; i++) {
      pscore += s[i][t[i]];
      last[t[i]] = i + 1;
      for (int j = 0; j < 2; j++) {
        int id = check[j];
        pscore -= c[id] * (i + 1 - last[id]);
      }
    }
    for (int i = 0; i < 2; i++)
      last[check[i]] = 0;
    for (int i = 0; i < d; i++) {
      if (day == i) {
        cscore += s[i][v];
        last[v] = i + 1;
      } else {
        cscore += s[i][t[i]];
        last[t[i]] = i + 1;
      }
      for (int j = 0; j < 2; j++) {
        int id = check[j];
        cscore -= c[id] * (i + 1 - last[id]);
      }
    }
    if (pscore < cscore) {
      t[day] = v;
    }
  }
  for (int i = 0; i < d; i++) {
    printf("%d\\n", t[i] + 1);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int d;
int c[26], s[365][26], last[26], t[365];

int main() {
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    int score;
    for (int j = 0; j < 26; j++) {
      int tscore = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (k == j)
          continue;
        tscore -= c[k] * (i + 1 - last[k]);
      }
      if (j == 0)
        score = tscore;
      else if (tscore > score) {
        score = tscore;
        t[i] = j;
      }
    }
    last[t[i]] = i + 1;
  }
  int cnt = 1000000;
  while (cnt--) {
    int day = rand() % d;
    int v = rand() % 26;
    if (t[day] == v)
      continue;
    int dscore = 0;
    int check[2] = {v, t[day]};
    int pscore = 0;
    int cscore = 0;
    for (int i = 0; i < 2; i++)
      last[check[i]] = 0;
    for (int i = 0; i < d; i++) {
      pscore += s[i][t[i]];
      last[t[i]] = i + 1;
      for (int j = 0; j < 2; j++) {
        int id = check[j];
        pscore -= c[id] * (i + 1 - last[id]);
      }
    }
    for (int i = 0; i < 2; i++)
      last[check[i]] = 0;
    for (int i = 0; i < d; i++) {
      if (day == i) {
        cscore += s[i][v];
        last[v] = i + 1;
      } else {
        cscore += s[i][t[i]];
        last[t[i]] = i + 1;
      }
      for (int j = 0; j < 2; j++) {
        int id = check[j];
        cscore -= c[id] * (i + 1 - last[id]);
      }
    }
    if (pscore < cscore) {
      t[day] = v;
    }
  }
  for (int i = 0; i < d; i++) {
    cout << t[i] + 1 << endl;
  }
}