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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll c[30], s[370][30];
int a[370], last[30];

unsigned seed = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(seed);

ll f(int d) {
  ll x = 0;
  memset(last, -1, sizeof(last));
  for (int i = 0; i < d; i++) {
    x += s[i][a[i]];
    last[a[i]] = i;
    for (int j = 0; j < 26; j++) {
      x -= c[j] * (ll)(i - last[j]);
    }
  }
  return x;
}

int main() {
  auto start = chrono::high_resolution_clock::now();
  ll x = LLONG_MIN, sc = 0;
  int d, ans[370];
  scanf("%d", &d);
  for (int i = 0; i < 26; i++) {
    scanf("%lld", &c[i]);
    sc += c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%lld", &s[i][j]);
    }
  }
  if (sc == 0) {
    for (int i = 0; i < d; i++) {
      int mx = -1, j = -1;
      for (int k = 0; k < 26; k++) {
        if (s[i][k] > mx)
          mx = s[i][k], j = k;
      }
      printf("%d\\n", j + 1);
    }
    return 0;
  }
  while (true) {
    for (int i = 0; i < d; i++)
      a[i] = rng() % 26;
    ll y = f(d);
    if (y > x)
      copy(a, a + d, ans), x = y;
    auto stop = chrono::high_resolution_clock::now();
    int duration =
        chrono::duration_cast<chrono::microseconds>(stop - start).count();
    if (2000000 - duration < 100000)
      break;
  }
  for (int i = 0; i < d; i++) {
    assert(ans[i] >= 0 && ans[i] < 26);
    printf("%d\\n", ans[i] + 1);
  }
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll c[30], s[370][30];
int a[370], last[30];

unsigned seed = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(seed);

ll f(int d) {
  ll x = 0;
  memset(last, -1, sizeof(last));
  for (int i = 0; i < d; i++) {
    x += s[i][a[i]];
    last[a[i]] = i;
    for (int j = 0; j < 26; j++) {
      x -= c[j] * (ll)(i - last[j]);
    }
  }
  return x;
}

int main() {
  auto start = chrono::high_resolution_clock::now();
  ll x = -1, sc = 0;
  int d, ans[370];
  scanf("%d", &d);
  for (int i = 0; i < 26; i++) {
    scanf("%lld", &c[i]);
    sc += c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%lld", &s[i][j]);
    }
  }
  if (sc == 0) {
    for (int i = 0; i < d; i++) {
      int mx = -1, j = -1;
      for (int k = 0; k < 26; k++) {
        if (s[i][k] > mx)
          mx = s[i][k], j = k;
      }
      printf("%d\\n", j + 1);
    }
    return 0;
  }
  while (true) {
    for (int i = 0; i < d; i++)
      a[i] = rand() % 26;
    ll y = f(d);
    if (y > x)
      copy(a, a + d, ans), x = y;
    auto stop = chrono::high_resolution_clock::now();
    int duration =
        chrono::duration_cast<chrono::microseconds>(stop - start).count();
    if (2000000 - duration < 100000)
      break;
  }
  for (int i = 0; i < d; i++) {
    assert(ans[i] >= 0 && ans[i] < 26);
    printf("%d\\n", ans[i] + 1);
  }
  return 0;
}