Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

int main() {

  srand((unsigned)time(NULL));
  int d;
  int res = 0;
  cin >> d;
  vector<int> c(26);
  rep(i, 26) cin >> c[i];
  vector<vector<int>> v(d, vector<int>(26));
  vector<int> contest(d, 0);
  vector<int> last(26, 0);
  vector<int> satisfy(26, 0);
  rep(i, d) rep(j, 26) {
    int x;
    cin >> x;
    v[i][j] = x;
  }
  {
    int j = d;
    for (int i = 0; j; i++) {
      contest[d - j] = (i % 26) + 1;
      j--;
    }
  }
  for (int i = 1; i <= d; i++) {
    // rep(j,26)if(v[i-1][contest[i-1]] < v[i-1][j])contest[i-1] = j;
    last[contest[i - 1]] = i;
    res += v[i - 1][contest[i - 1]];
    rep(j, 26) res -= c[j] * (i - last[j]), satisfy[j] += c[j] * (i - last[j]);
  }
  while (1.0 * clock() / CLOCKS_PER_SEC <= 1.7) {
    // int now_time = clock();
    // if(1.0*now_time/CLOCKS_PER_SEC > 1.7)break;
    int day = rand() % d, pos = rand() % 26;
    vector<int> new_contest = contest;
    int res_ = res;
    int pre = new_contest[day];
    res_ += satisfy[pos];
    res_ += satisfy[pre];
    res_ -= v[day][pre];
    res_ += v[day][pos];
    int pos_satisfy = 0, pre_satisfy = 0;
    last[pos] = last[pre] = 0;
    new_contest[day] = pos;
    for (int i = 1; i <= d; i++)
      last[pos] = (new_contest[i - 1] == pos ? last[pos] = i : last[pos]),
      pos_satisfy += c[pos] * (i - last[pos]);
    for (int i = 1; i <= d; i++)
      last[pre] = (new_contest[i - 1] == pre ? last[pre] = i : last[pre]),
      pre_satisfy += c[pre] * (i - last[pre]);
    res_ -= (satisfy[pos] + satisfy[pre]);
    if (res < res_) {
      res = res_;
      contest = new_contest;
      satisfy[pre] = pre_satisfy;
      satisfy[pos] = pos_satisfy;
    }
  }
  rep(i, d) cout << contest[i] + 1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

int main() {

  srand((unsigned)time(NULL));
  int d;
  int res = 0;
  cin >> d;
  vector<int> c(26);
  rep(i, 26) cin >> c[i];
  vector<vector<int>> v(d, vector<int>(26));
  vector<int> contest(d, 0);
  vector<int> last(26, 0);
  vector<int> satisfy(26, 0);
  rep(i, d) rep(j, 26) {
    int x;
    cin >> x;
    v[i][j] = x;
  }
  {
    int j = d;
    for (int i = 0; j; i++) {
      contest[d - j] = (i % 26);
      j--;
    }
  }
  for (int i = 1; i <= d; i++) {
    // rep(j,26)if(v[i-1][contest[i-1]] < v[i-1][j])contest[i-1] = j;
    last[contest[i - 1]] = i;
    res += v[i - 1][contest[i - 1]];
    rep(j, 26) res -= c[j] * (i - last[j]), satisfy[j] += c[j] * (i - last[j]);
  }
  while (1.0 * clock() / CLOCKS_PER_SEC <= 1.7) {
    // int now_time = clock();
    // if(1.0*now_time/CLOCKS_PER_SEC > 1.7)break;
    int day = rand() % d, pos = rand() % 26;
    vector<int> new_contest = contest;
    int res_ = res;
    int pre = new_contest[day];
    res_ += satisfy[pos];
    res_ += satisfy[pre];
    res_ -= v[day][pre];
    res_ += v[day][pos];
    int pos_satisfy = 0, pre_satisfy = 0;
    last[pos] = last[pre] = 0;
    new_contest[day] = pos;
    for (int i = 1; i <= d; i++)
      last[pos] = (new_contest[i - 1] == pos ? last[pos] = i : last[pos]),
      pos_satisfy += c[pos] * (i - last[pos]);
    for (int i = 1; i <= d; i++)
      last[pre] = (new_contest[i - 1] == pre ? last[pre] = i : last[pre]),
      pre_satisfy += c[pre] * (i - last[pre]);
    res_ -= (satisfy[pos] + satisfy[pre]);
    if (res < res_) {
      res = res_;
      contest = new_contest;
      satisfy[pre] = pre_satisfy;
      satisfy[pos] = pos_satisfy;
    }
  }
  rep(i, d) cout << contest[i] + 1 << endl;

  return 0;
}