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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rng(time(NULL));

const int D = 365, N = 26;
int d;
ll c[N + 1];
ll s[D + 1][N + 1];
ll last[N];
int contest[D];

ll calc(int idx, int x) {
  ll ans = 0;
  ll l = idx - 1, r = idx + 1;
  ll len, len2;
  while (l > 0 && contest[l] != contest[idx])
    l--;
  len = idx - l;
  while (r <= d && contest[r] != contest[idx])
    r++;
  len2 = r - idx;
  ans -= s[idx][contest[idx]];
  ans -= c[contest[idx]] * len * len2;
  ll tmp = contest[idx];
  contest[idx] = x;

  l = idx - 1, r = idx + 1;
  while (l > 0 && contest[l] != contest[idx])
    l--;
  len = idx - l;
  while (r <= d && contest[r] != contest[idx])
    r++;
  len2 = r - idx;
  ans += s[idx][x];
  ans += c[x] * len * len2;
  contest[idx] = tmp;
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> d;
  for (int i = 1; i <= N; i++)
    cin >> c[i];
  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= N; j++)
      cin >> s[i][j];
  }
  memset(last, 0, sizeof last);
  ll ans = 0;
  for (int i = 1; i <= d; i++) {
    ll penalty = 0;
    for (int j = 1; j <= 26; j++)
      penalty += c[j] * (last[j] - i);
    ll best = -1, idx = -1;
    for (int j = 1; j <= 26; j++) {
      ll cur = s[i][j] - penalty + c[j] * (i - last[j]);
      if (best < cur)
        best = cur, idx = j;
    }
    last[idx] = i;
    ans += best;
    contest[i] = idx;
  }

  for (int i = 0; i < 120000; i++) {
    int idx = rng() % d + 1;
    for (int to = 1; to <= 26; to++)
      if (to != contest[idx]) {
        ll change = calc(idx, to);
        if (change > 0) {
          contest[idx] = to;
          ans += change;
        }
      }
  }

  for (int i = 1; i <= d; i++)
    cout << contest[i] << '\\n';

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rng(time(NULL));

const int D = 365, N = 26;
int d;
ll c[N + 1];
ll s[D + 1][N + 1];
ll last[N];
int contest[D];

ll calc(int idx, int x) {
  ll ans = 0;
  ll l = idx - 1, r = idx + 1;
  ll len, len2;
  while (l > 0 && contest[l] != contest[idx])
    l--;
  len = idx - l;
  while (r <= d && contest[r] != contest[idx])
    r++;
  len2 = r - idx;
  ans -= s[idx][contest[idx]];
  ans -= c[contest[idx]] * len * len2;
  ll tmp = contest[idx];
  contest[idx] = x;

  l = idx - 1, r = idx + 1;
  while (l > 0 && contest[l] != contest[idx])
    l--;
  len = idx - l;
  while (r <= d && contest[r] != contest[idx])
    r++;
  len2 = r - idx;
  ans += s[idx][x];
  ans += c[x] * len * len2;
  contest[idx] = tmp;
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> d;
  for (int i = 1; i <= N; i++)
    cin >> c[i];
  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= N; j++)
      cin >> s[i][j];
  }
  memset(last, 0, sizeof last);
  ll ans = 0;
  for (int i = 1; i <= d; i++) {
    ll penalty = 0;
    for (int j = 1; j <= 26; j++)
      penalty += c[j] * (last[j] - i);
    ll best = -1, idx = -1;
    for (int j = 1; j <= 26; j++) {
      ll cur = s[i][j] - penalty + c[j] * (i - last[j]);
      if (best < cur)
        best = cur, idx = j;
    }
    last[idx] = i;
    ans += best;
    contest[i] = idx;
  }

  for (int i = 0; i < 120000; i++) {
    int idx = rng() % d + 1;
    for (int to = 1; i <= N; to++)
      if (to != contest[idx]) {
        ll change = calc(idx, to);
        if (change > 0) {
          contest[idx] = to;
          ans += change;
        }
      }
  }

  for (int i = 1; i <= d; i++)
    cout << contest[i] << '\\n';

  return 0;
}