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

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

const int D = 365;
int C[26] = {};
int S[D][26] = {};
vector<int> L(26, 0);
vector<int> T;

ll ret[26] = {};

void input() {
  int dummy;
  cin >> dummy;
  REP(i, 0, 26) cin >> C[i];
  REP(i, 0, D) REP(j, 0, 26) cin >> S[i][j];
}

ll calc_init() {
  ll ret = 0;
  int d = T.size();
  L.assign(26, 0);
  REP(i, 0, d) {
    ret += S[i][T[i]];
    L[T[i]] = i + 1;
    REP(j, 0, 26) ret -= C[j] * (i + 1 - L[j]);
  }
  return ret;
}

void init() {
  REP(i, 0, D) {
    int max_score = 0, max_idx = 0;
    REP(j, 0, 26) {
      T.push_back(j);
      ll cur = calc_init();
      if (max_score < cur) {
        max_score = cur;
        max_idx = j;
      }
      T.pop_back();
    }
    T.push_back(max_idx);
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  auto start = std::chrono::system_clock::now();
  const ll limit = 1950 * 1000;

  input();
  init();

  REP(i, 0, D) {
    ret[T[i]] += S[i][T[i]];
    L[T[i]] = i + 1;
    REP(j, 0, 26) { ret[j] -= C[j] * (i + 1 - L[j]); }
  }

  ll best = 0;
  REP(i, 0, 26) best += ret[i];

  mt19937_64 mt(random_device{}());
  uniform_int_distribution<int> day(0, D - 1);
  uniform_int_distribution<int> con(0, 25);
  while (1) {
    std::chrono::system_clock::time_point now =
        std::chrono::system_clock::now();
    double elapsed =
        std::chrono::duration_cast<std::chrono::microseconds>(now - start)
            .count();
    if (elapsed > limit)
      break;

    int d = day(mt);
    int next = con(mt);
    int prev = T[d];
    T[d] = next;
    L[prev] = 0;
    L[next] = 0;
    ll oldp = ret[prev];
    ll oldn = ret[next];
    ll cur = best - oldp - oldn;
    ret[prev] = 0;
    ret[next] = 0;
    REP(i, 0, D) {
      if (T[i] == prev || T[i] == next) {
        ret[T[i]] += S[i][T[i]];
        L[T[i]] = i + 1;
      }
      ret[prev] -= C[prev] * (i + 1 - L[prev]);
      ret[next] -= C[next] * (i + 1 - L[next]);
    }
    if (best >= cur + ret[prev] + ret[next]) {
      T[d] = prev;
      ret[prev] = oldp;
      ret[next] = oldn;
    } else {
      best = cur + ret[prev] + ret[next];
    }
    // cout << best << endl;
  }

  for (auto t : T)
    cout << t + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

const int D = 365;
int C[26] = {};
int S[D][26] = {};
vector<int> L(26, 0);
vector<int> T;

ll ret[26] = {};

void input() {
  int dummy;
  cin >> dummy;
  REP(i, 0, 26) cin >> C[i];
  REP(i, 0, D) REP(j, 0, 26) cin >> S[i][j];
}

ll calc_init() {
  ll ret = 0;
  int d = T.size();
  L.assign(26, 0);
  REP(i, 0, d) {
    ret += S[i][T[i]];
    L[T[i]] = i + 1;
    REP(j, 0, 26) ret -= C[j] * (i + 1 - L[j]);
  }
  return ret;
}

void init() {
  REP(i, 0, D) {
    int max_score = 0, max_idx = 0;
    REP(j, 0, 26) {
      T.push_back(j);
      ll cur = calc_init();
      if (max_score < cur) {
        max_score = cur;
        max_idx = j;
      }
      T.pop_back();
    }
    T.push_back(max_idx);
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  input();
  init();

  REP(i, 0, D) {
    ret[T[i]] += S[i][T[i]];
    L[T[i]] = i + 1;
    REP(j, 0, 26) { ret[j] -= C[j] * (i + 1 - L[j]); }
  }

  auto start = std::chrono::system_clock::now();
  const ll limit = 1950 * 1000;

  ll best = 0;
  REP(i, 0, 26) best += ret[i];

  mt19937_64 mt(random_device{}());
  uniform_int_distribution<int> day(0, D - 1);
  uniform_int_distribution<int> con(0, 25);
  while (1) {
    std::chrono::system_clock::time_point now =
        std::chrono::system_clock::now();
    double elapsed =
        std::chrono::duration_cast<std::chrono::microseconds>(now - start)
            .count();
    if (elapsed > limit)
      break;

    int d = day(mt);
    int next = con(mt);
    int prev = T[d];
    T[d] = next;
    L[prev] = 0;
    L[next] = 0;
    ll oldp = ret[prev];
    ll oldn = ret[next];
    ll cur = best - oldp - oldn;
    ret[prev] = 0;
    ret[next] = 0;
    REP(i, 0, D) {
      if (T[i] == prev || T[i] == next) {
        ret[T[i]] += S[i][T[i]];
        L[T[i]] = i + 1;
      }
      ret[prev] -= C[prev] * (i + 1 - L[prev]);
      ret[next] -= C[next] * (i + 1 - L[next]);
    }
    if (best >= cur + ret[prev] + ret[next]) {
      T[d] = prev;
      ret[prev] = oldp;
      ret[next] = oldn;
    } else {
      best = cur + ret[prev] + ret[next];
    }
    // cout << best << endl;
  }

  for (auto t : T)
    cout << t + 1 << endl;
  return 0;
}