Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <chrono>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  long d;
  cin >> d;
  long n = 26, csum = 0, ccost = 0;
  vector<long> c(n), last(n);
  rep(i, n) {
    cin >> c[i];
    csum += c[i];
    last[i] = 0;
  }
  vector<vector<long>> s(d, vector<long>(n));
  rep(i, d) rep(j, n) cin >> s[i][j];
  vector<vector<long>> cand;
  vector<long> score;
  random_device rd{};
  vector<long> yotei(d);
  vector<long> tmp(d);
  long final_ans = 0, final_index;

  rep(jj, 100) {

    long ans = 0;
    rep(i, n) last[i] = 0;
    rep(i, d) yotei[i] = rd() % n;
    rep(i, d) {
      ccost += csum;
      ccost -= c[yotei[i]] * (i + 1 - last[yotei[i]]);
      ans -= ccost;
      last[yotei[i]] = i + 1;
      ans += s[i][yotei[i]];
    }
    int ii = 0;
    while (ii < 200000) {
      long new_day = rd() % d, new_con;
      while (1) {
        new_con = rd() % n;
        if (yotei[new_day] != new_con)
          break;
      }
      long pre_con = yotei[new_day];
      yotei[new_day] = new_con;
      long tmpans = 0;
      ccost = 0;
      rep(i, n) last[i] = 0;
      rep(i, d) {
        tmp[i] = yotei[i];
        ccost += csum;
        ccost -= c[yotei[i]] * (i + 1 - last[yotei[i]]);
        tmpans -= ccost;
        last[yotei[i]] = i + 1;
        tmpans += s[i][yotei[i]];
      }
      if (tmpans < ans)
        yotei[new_day] = pre_con;
      else {
        ans = tmpans;
        cand.push_back(tmp);
      }
      ii++;
    }
    if (final_ans < ans) {
      final_ans = ans;
      final_index = int(cand.size() - 1);
    }
    end = chrono::system_clock::now();
    auto time = end - start;
    if (static_cast<double>(
            chrono::duration_cast<chrono::milliseconds>(time).count()) > 1950)
      break;
  }
  rep(i, d) cout << cand[final_index][i] + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  long d;
  cin >> d;
  long n = 26, csum = 0, ccost = 0;
  vector<long> c(n), last(n);
  rep(i, n) {
    cin >> c[i];
    csum += c[i];
    last[i] = 0;
  }
  vector<vector<long>> s(d, vector<long>(n));
  rep(i, d) rep(j, n) cin >> s[i][j];
  vector<vector<long>> cand;
  vector<long> score;
  random_device rd{};
  vector<long> yotei(d);
  vector<long> tmp(d);
  long final_ans = 0, final_index;

  rep(jj, 100) {

    long ans = 0;
    rep(i, n) last[i] = 0;
    rep(i, d) yotei[i] = rd() % n;
    rep(i, d) {
      ccost += csum;
      ccost -= c[yotei[i]] * (i + 1 - last[yotei[i]]);
      ans -= ccost;
      last[yotei[i]] = i + 1;
      ans += s[i][yotei[i]];
    }
    int ii = 0;
    while (ii < 200000) {
      long new_day = rd() % d, new_con;
      while (1) {
        new_con = rd() % n;
        if (yotei[new_day] != new_con)
          break;
      }
      long pre_con = yotei[new_day];
      yotei[new_day] = new_con;
      long tmpans = 0;
      ccost = 0;
      rep(i, n) last[i] = 0;
      rep(i, d) {
        tmp[i] = yotei[i];
        ccost += csum;
        ccost -= c[yotei[i]] * (i + 1 - last[yotei[i]]);
        tmpans -= ccost;
        last[yotei[i]] = i + 1;
        tmpans += s[i][yotei[i]];
      }
      if (tmpans < ans)
        yotei[new_day] = pre_con;
      else {
        ans = tmpans;
        cand.push_back(tmp);
      }
      ii++;
    }
    if (final_ans < ans) {
      final_ans = ans;
      final_index = int(cand.size() - 1);
    }
    end = chrono::system_clock::now();
    auto time = end - start;
    if (static_cast<double>(
            chrono::duration_cast<chrono::milliseconds>(time).count()) > 1880)
      break;
  }
  rep(i, d) cout << cand[final_index][i] + 1 << endl;
  return 0;
}