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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define io_init                                                                \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cout << setprecision(10)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int calcScore(vector<int> &plan, vector<int> &c, vector<vector<int>> &s) {
  int D = 365;
  int score = 0;
  vector<int> last(26, -1);
  for (int i = 0; i < D; i++) {
    score += s[i][plan[i]];
    last[plan[i]] = i;
    for (int j = 0; j < 26; j++) {
      score -= c[j] * (i - last[j]);
    }
  }
  return score;
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(365, vector<int>(26, 0));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];
  }

  vector<int> plan(365, 0);
  for (int i = 0; i < 365; i++) {
    plan[i] = i % 26;
  }
  int sc = calcScore(plan, c, s);

  for (int n = 0; n < 300000; n++) {
    if (rand() % 10 < 3) {
      int i = rand() % 365, j = rand() % 26, old = plan[i];
      plan[i] = j;
      int nsc = calcScore(plan, c, s);
      if (nsc > sc) {
        sc = nsc;
      } else {
        plan[i] = old;
      }
    } else {
      int k = rand() % 5;
      int i = rand() % (365 - k), j = i + k;
      swap(plan[i], plan[j]);
      int nsc = calcScore(plan, c, s);
      if (nsc > sc) {
        sc = nsc;
      } else {
        swap(plan[i], plan[j]);
      }
    }
  }

  for (int i = 0; i < 365; i++) {
    cout << plan[i] + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define io_init                                                                \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cout << setprecision(10)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int calcScore(vector<int> &plan, vector<int> &c, vector<vector<int>> &s) {
  int D = 365;
  int score = 0;
  vector<int> last(26, -1);
  for (int i = 0; i < D; i++) {
    score += s[i][plan[i]];
    last[plan[i]] = i;
    for (int j = 0; j < 26; j++) {
      score -= c[j] * (i - last[j]);
    }
  }
  return score;
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(365, vector<int>(26, 0));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];
  }

  vector<int> plan(365, 0);
  for (int i = 0; i < 365; i++) {
    plan[i] = i % 26 + 1;
  }
  int sc = calcScore(plan, c, s);

  for (int n = 0; n < 300000; n++) {
    if (rand() % 10 < 3) {
      int i = rand() % 365, j = rand() % 26, old = plan[i];
      plan[i] = j;
      int nsc = calcScore(plan, c, s);
      if (nsc > sc) {
        sc = nsc;
      } else {
        plan[i] = old;
      }
    } else {
      int k = rand() % 5;
      int i = rand() % (365 - k), j = i + k;
      swap(plan[i], plan[j]);
      int nsc = calcScore(plan, c, s);
      if (nsc > sc) {
        sc = nsc;
      } else {
        swap(plan[i], plan[j]);
      }
    }
  }

  for (int i = 0; i < 365; i++) {
    cout << plan[i] + 1 << endl;
  }
}