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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int get_random(int l, int r) {
  int k = r - l + 1;
  return l + rng() % k;
}

const int N = 365 + 7;
const int K = 26 + 7;
int n;
int c[K], s[N][K], t[N], last[K], sol[N], best = -(int)2e9;

int get() {
  for (int j = 1; j <= 26; j++) {
    last[j] = 0;
  }
  int sol = 0, grow = 0;
  for (int i = 1; i <= n; i++) {
    sol += s[i][t[i]];
    grow += c[t[i]] * (i - last[t[i]]);
    last[t[i]] = i;
    sol += grow;
  }
  return sol;
}

void upd() {
  int cur = get();
  if (cur > best) {
    best = cur;
    for (int i = 1; i <= n; i++) {
      sol[i] = t[i];
    }
  }
}

void make_better(double seconds) {
  time_t bg = clock();
  int c = 0;
  int cur = get();
  while (1) {
    c++;
    if (c % 10 == 0) {
      time_t nd = clock();
      if ((double)(nd - bg) / CLOCKS_PER_SEC > seconds) {
        break;
      }
    }
    int i = get_random(1, n), x = get_random(1, 26);
    while (x == t[i]) {
      x = get_random(1, 26);
    }
    int init = t[i];
    t[i] = x;
    int after = get();
    if (after > cur) {
      cur = after;
    } else {
      t[i] = init;
    }
  }
  upd();
}

void generate_max() {
  for (int i = 1; i <= n; i++) {
    t[i] = 1;
    for (int j = 1; j <= 26; j++) {
      if (s[i][j] > s[i][t[i]]) {
        t[i] = j;
      }
    }
  }
  int modif = get_random(1, 20);
  while (modif--) {
    int i = get_random(1, n), x = get_random(1, 26);
    while (t[i] == x) {
      x = get_random(1, 26);
    }
    t[i] = x;
  }
}

void generate_random() {
  generate_max();
  for (int i = 1; i <= n; i++) {
    t[i] = get_random(1, 26);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= 150; i++) {
    generate_random();
    make_better(0.01);
    upd();
  }
  generate_max();
  make_better(0.2);
  upd();
  for (int i = 1; i <= n; i++) {
    cout << sol[i] << "\\n";
  }
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int get_random(int l, int r) {
  int k = r - l + 1;
  return l + rng() % k;
}

const int N = 365 + 7;
const int K = 26 + 7;
int n;
int c[K], s[N][K], t[N], last[K], sol[N], best = -(int)2e9;

int get() {
  for (int j = 1; j <= 26; j++) {
    last[j] = 0;
  }
  int sol = 0, grow = 0;
  for (int i = 1; i <= n; i++) {
    sol += s[i][t[i]];
    grow += c[t[i]] * (i - last[t[i]]);
    last[t[i]] = i;
    sol += grow;
  }
  return sol;
}

void upd() {
  int cur = get();
  if (cur > best) {
    best = cur;
    for (int i = 1; i <= n; i++) {
      sol[i] = t[i];
    }
  }
}

void make_better(double seconds) {
  time_t bg = clock();
  int c = 0;
  int cur = get();
  while (1) {
    c++;
    if (c % 10 == 0) {
      time_t nd = clock();
      if ((double)(nd - bg) / CLOCKS_PER_SEC > seconds) {
        break;
      }
    }
    int i = get_random(1, n), x = get_random(1, 26);
    while (x == t[i]) {
      x = get_random(1, 26);
    }
    int init = t[i];
    t[i] = x;
    int after = get();
    if (after > cur) {
      cur = after;
    } else {
      t[i] = init;
    }
  }
  upd();
}

/**void generate_greedy() {

}**/

void generate_random() {
  for (int i = 1; i <= n; i++) {
    t[i] = get_random(1, 26);
  }
}

void generate_max() {
  for (int i = 1; i <= n; i++) {
    t[i] = 1;
    for (int j = 1; j <= 26; j++) {
      if (s[i][j] > s[i][t[i]]) {
        t[i] = j;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  freopen("input", "r", stdin);
  cin >> n;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= 150; i++) {
    generate_random();
    make_better(0.01);
    upd();
  }
  generate_max();
  make_better(0.2);
  upd();
  for (int i = 1; i <= n; i++) {
    cout << sol[i] << "\\n";
  }
  return 0;
}