Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

struct Timer {
  chrono::high_resolution_clock::time_point st;

  Timer() { reset(); }

  void reset() { st = chrono::high_resolution_clock::now(); }

  chrono::milliseconds::rep elapsed() {
    auto ed = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(ed - st).count();
  }
};

struct RNG {
  mt19937 mt;

  RNG() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}

  int operator()(int a, int b) {
    uniform_int_distribution<int> dist(a, b - 1);
    return dist(mt);
  }

  int operator()(int b) { return (*this)(0, b); }
};

constexpr int D = 365;
vector<int> c(D);
vector<vector<int>> s(D, vector<int>(26));
Timer TimeKeeper;
RNG rng;

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

  int kyomu;
  cin >> kyomu;
  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  vector<int> maxe(D, -1);
  vector<int> cur(D, 0);
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < 26; ++j) {
      cin >> s[i][j];
      if (chmax(maxe[i], s[i][j]))
        cur[i] = j;
    }
  }

  int BestScore = 0;
  auto calc_score = [&](vector<int> &v) -> int {
    if (v.size() != D)
      return -INF;
    int res = 0;
    vector<int> pre(26, -1);
    for (int i = 0; i < D; ++i) {
      res += s[i][v[i]];
      pre[v[i]] = i;
      for (int j = 0; j < 26; ++j) {
        if (pre[j] == -1)
          continue;
        res -= c[j] * (i - pre[j]);
      }
    }
    return res;
  };
  BestScore = calc_score(cur);

  int p = 0;
  while (TimeKeeper.elapsed() < 1995) {
    int NxtScore = -INF;
    int ProblemType = -1;
    vector<int> nxt = cur;
    for (int i = 0; i < 26; ++i) {
      if (i == cur[p])
        continue;
      nxt[p] = i;
      if (chmax(NxtScore, calc_score(nxt)))
        ProblemType = i;
    }
    if (NxtScore > BestScore) {
      BestScore = NxtScore;
      cur[p] = ProblemType;
    }
    if (++p == D)
      p = 0;
  }

  for (int i = 0; i < D; ++i)
    cout << cur[i] + 1 << '\n';
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

struct Timer {
  chrono::high_resolution_clock::time_point st;

  Timer() { reset(); }

  void reset() { st = chrono::high_resolution_clock::now(); }

  chrono::milliseconds::rep elapsed() {
    auto ed = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(ed - st).count();
  }
};

struct RNG {
  mt19937 mt;

  RNG() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}

  int operator()(int a, int b) {
    uniform_int_distribution<int> dist(a, b - 1);
    return dist(mt);
  }

  int operator()(int b) { return (*this)(0, b); }
};

constexpr int D = 365;
vector<int> c(D);
vector<vector<int>> s(D, vector<int>(26));
Timer TimeKeeper;
RNG rng;

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

  int kyomu;
  cin >> kyomu;
  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  vector<int> maxe(D, -1);
  vector<int> cur(D, 0);
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < 26; ++j) {
      cin >> s[i][j];
      if (chmax(maxe[i], s[i][j]))
        cur[i] = j;
    }
  }

  int BestScore = 0;
  auto calc_score = [&](vector<int> &v) -> int {
    if (v.size() != D)
      return -INF;
    int res = 0;
    vector<int> pre(26, -1);
    for (int i = 0; i < D; ++i) {
      res += s[i][v[i]];
      pre[v[i]] = i;
      for (int j = 0; j < 26; ++j) {
        if (pre[j] == -1)
          continue;
        res -= c[j] * (i - pre[j]);
      }
    }
    return res;
  };
  BestScore = calc_score(cur);

  int p = 0;
  while (TimeKeeper.elapsed() < 1950) {
    int NxtScore = -INF;
    int ProblemType = -1;
    vector<int> nxt = cur;
    for (int i = 0; i < 26; ++i) {
      if (i == cur[p])
        continue;
      nxt[p] = i;
      if (chmax(NxtScore, calc_score(nxt)))
        ProblemType = i;
    }
    if (NxtScore > BestScore) {
      BestScore = NxtScore;
      cur[p] = ProblemType;
    }
    if (++p == D)
      p = 0;
  }

  for (int i = 0; i < D; ++i)
    cout << cur[i] + 1 << '\n';
}