Problem p02618 (C++)

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

int d, score, i, j, k, pscore, cscore, id;
int c[26], s[365][26], last[26], t[365];
int day1, day2, v1, v2, csize, f, check[4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> d;
  for (i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (i = 0; i < d; i++) {
    for (j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (i = 0; i < d; i++) {
    for (j = 0; j < 26; j++) {
      int tscore = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (k == j)
          continue;
        tscore -= c[k] * (i + 1 - last[k]);
      }
      if (j == 0)
        score = tscore;
      else if (tscore > score) {
        score = tscore;
        t[i] = j;
      }
    }
    last[t[i]] = i + 1;
  }
  int cnt = 400000;
  while (cnt--) {
    day1 = rand() % d;
    v1 = rand() % 26;
    while (t[day1] == v1) {
      v1 = rand() % 26;
    }
    check[0] = t[day1];
    check[1] = v1;
    csize = 2;
    f = 0;
    if (cnt < 100000) {
      f = 1;
      day2 = rand() % d;
      while (day2 == day1) {
        day2 = rand() % d;
      }
      v2 = rand() % 26;
      while (v2 == t[day2]) {
        v2 = rand() % d;
      }
      if (t[day1] != t[day2] && v1 != t[day2]) {
        check[csize++] = t[day2];
      }
      if (t[day1] != v2 && v1 != v2) {
        check[csize++] = v2;
      }
    }
    pscore = 0;
    cscore = 0;
    for (i = 0; i < csize; i++)
      last[check[i]] = 0;
    for (i = 0; i < d; i++) {
      pscore += s[i][t[i]];
      last[t[i]] = i + 1;
      for (j = 0; j < csize; j++) {
        int id = check[j];
        pscore -= c[id] * (i + 1 - last[id]);
      }
    }
    for (i = 0; i < csize; i++)
      last[check[i]] = 0;
    for (i = 0; i < d; i++) {
      if (day1 == i) {
        cscore += s[i][v1];
        last[v1] = i + 1;
      } else if (f && day2 == i) {
        cscore += s[i][v2];
        last[v2] = i + 1;
      } else {
        cscore += s[i][t[i]];
        last[t[i]] = i + 1;
      }
      for (j = 0; j < csize; j++) {
        id = check[j];
        cscore -= c[id] * (i + 1 - last[id]);
      }
    }
    if (pscore < cscore) {
      t[day1] = v1;
      if (f)
        t[day2] = v2;
    }
  }
  for (i = 0; i < d; i++) {
    printf("%d\n", t[i] + 1);
  }
}

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

int d, score, i, j, k, pscore, cscore, id;
int c[26], s[365][26], last[26], t[365];
int day1, day2, v1, v2, csize, f, check[4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> d;
  for (i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (i = 0; i < d; i++) {
    for (j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (i = 0; i < d; i++) {
    for (j = 0; j < 26; j++) {
      int tscore = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (k == j)
          continue;
        tscore -= c[k] * (i + 1 - last[k]);
      }
      if (j == 0)
        score = tscore;
      else if (tscore > score) {
        score = tscore;
        t[i] = j;
      }
    }
    last[t[i]] = i + 1;
  }
  int cnt = 400000;
  while (cnt--) {
    day1 = rand() % d;
    v1 = rand() % 26;
    while (t[day1] == v1) {
      v1 = rand() % 26;
    }
    check[0] = t[day1];
    check[1] = v1;
    csize = 2;
    f = 0;
    if (cnt < 100000) {
      f = 1;
      day2 = rand() % d;
      while (day2 == day1) {
        day2 = rand() % d;
      }
      v2 = rand() % 26;
      while (v2 == t[day2]) {
        v2 = rand() % 26;
      }
      if (t[day1] != t[day2] && v1 != t[day2]) {
        check[csize++] = t[day2];
      }
      if (t[day1] != v2 && v1 != v2) {
        check[csize++] = v2;
      }
    }
    pscore = 0;
    cscore = 0;
    for (i = 0; i < csize; i++)
      last[check[i]] = 0;
    for (i = 0; i < d; i++) {
      pscore += s[i][t[i]];
      last[t[i]] = i + 1;
      for (j = 0; j < csize; j++) {
        int id = check[j];
        pscore -= c[id] * (i + 1 - last[id]);
      }
    }
    for (i = 0; i < csize; i++)
      last[check[i]] = 0;
    for (i = 0; i < d; i++) {
      if (day1 == i) {
        cscore += s[i][v1];
        last[v1] = i + 1;
      } else if (f && day2 == i) {
        cscore += s[i][v2];
        last[v2] = i + 1;
      } else {
        cscore += s[i][t[i]];
        last[t[i]] = i + 1;
      }
      for (j = 0; j < csize; j++) {
        id = check[j];
        cscore -= c[id] * (i + 1 - last[id]);
      }
    }
    if (pscore < cscore) {
      t[day1] = v1;
      if (f)
        t[day2] = v2;
    }
  }
  for (i = 0; i < d; i++) {
    printf("%d\n", t[i] + 1);
  }
}