Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

std::random_device seed_gen;
std::mt19937 engine(seed_gen());
constexpr int prob = 26;
const double TL = 1.970;
// const double TL = 0.100;

vector<ll> c, l;
vector<vector<int>> s;
int d;

ll score(vector<int> &t) {
  ll sat = 0;
  rep(i, prob) l[i] = -1;
  rep(dy, d) {
    int k = t[dy];
    sat += s[dy][k];
    l[k] = dy + 1;
    rep(i, 26) sat -= c[i] * (dy - l[i]);
  }
  return sat;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  clock_t start = clock(), end;
  cin >> d;
  c.resize(prob);
  l.resize(prob);
  s.resize(d);
  rep(i, s.size()) s[i].resize(prob);
  rep(i, c.size()) cin >> c[i];
  rep(i, s.size()) {
    rep(j, s[0].size()) { cin >> s[i][j]; }
  }

  vector<int> t(d), tmp;
  rep(i, d) t[i] = engine() % prob;
  tmp = t;
  ll max_score = -(ll)1e18;
  int count = 1, k;
  while (true) {
    // write here

    do {
      k = engine() % d;
      tmp[k] = engine() % prob;
    } while (tmp[k] == t[k]);
    if (chmax(max_score, score(tmp)))
      t = tmp;

    if (count % 100 == 0) {
      end = clock();
      if (static_cast<double>(end - start) / CLOCKS_PER_SEC > TL)
        break;
    }
    count++;
  }

  rep(i, t.size()) cout << t[i] + 1 << "\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

std::random_device seed_gen;
std::mt19937 engine(seed_gen());

vector<ll> c(26), t(365), l(26);
vector<vector<int>> s(365, vector<int>(26));
const double TL = 1.970;

ll score(vector<int> t) {
  ll sat = 0;
  rep(dy, 365) {
    int k = t[dy] - 1;
    sat += s[dy][k];
    l[k] = dy + 1;
    eprintf("%lld ", sat);
    rep(i, 26) sat -= c[i] * (dy + 1 - l[i]);
  }
  return sat;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  clock_t start = clock(), end;
  int d;
  cin >> d;
  rep(i, c.size()) cin >> c[i];
  rep(i, s.size()) {
    rep(j, s[0].size()) { cin >> s[i][j]; }
  }
  rep(i, t.size()) cin >> t[i];

  vector<int> t(d), tmp(d);
  rep(i, d) t[i] = engine() % 26;
  tmp = t;
  ll max_score = -(ll)1e18;
  int count = 1, k;
  while (true) {
    // write here

    do {
      k = engine() % d;
      tmp[k] = engine() % 26;
    } while (tmp[k] == l[k]);
    if (chmax(max_score, score(tmp)))
      t = tmp;

    if (count % 100 == 0) {
      end = clock();
      if (static_cast<double>(end - start) / CLOCKS_PER_SEC > TL)
        break;
    }
    count++;
  }

  rep(i, 365) cout << t[i] + 1 << "\\n";

  return 0;
}