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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int MAX = 1000000010;
const ll MOD = 1000000007;

int main() {
  int D;
  cin >> D;
  vector<int> C(26 + 1);
  for (int i = 1; i <= 26; i++)
    cin >> C[i];
  vector<vector<int>> S(D + 1, vector<int>(26 + 1));
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++)
      cin >> S[i][j];
  }

  vector<int> T(365 + 1);
  vector<vector<int>> last(D + 1, vector<int>(26 + 1));
  ll sum = 0;
  for (int d = 1; d <= D; d++) {
    int num = 0;
    ll grd = (-1) * INF;
    int daybef = 0;
    for (int t = 1; t <= 26; t++) {
      ll tmp = 0;
      tmp += (ll)S[d][t];
      int bef = last[d][t];
      last[d][t] = d;
      for (int i = 1; i <= 26; i++) {
        tmp -= (ll)C[i] * (d - last[d][i]);
      }
      if (chmax(grd, tmp)) {
        num = t;
        daybef = bef;
      }
      last[d][t] = bef;
    }
    last[d][num] = d;
    for (int i = daybef; i < d; i++) {
      last[i][num] = daybef;
    }
    T[d] = num;
    sum += (ll)S[d][num];
    for (int i = 1; i <= 26; i++) {
      sum -= (ll)C[i] * (d - last[d][i]);
    }
  }

  for (int q = 0; q <= 100000; q++) {
    int d = rand() % D + 1;
    int u = rand() % 26 + 1;
    ll oldSum = sum;
    sum -= S[d][T[d]];
    for (int i = 1; i <= 26; i++) {
      sum += (ll)C[i] * (d - last[d][i]);
    }
    int oldT = T[d];
    T[d] = u;
    sum += S[d][u];
    last[d][oldT] = last[d - 1][oldT];
    int bef = last[d][u];
    last[d][u] = d;
    for (int i = 1; i <= 26; i++) {
      sum -= (ll)C[i] * (d - last[d][i]);
    }
    if (oldSum >= sum) {
      T[d] = oldT;
      last[d][T[d]] = d;
      last[d][u] = bef;
      sum = oldSum;
    }
  }

  for (int i = 1; i <= D; i++) {
    printf("%d\\n", T[i]);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int MAX = 1000000010;
const ll MOD = 1000000007;

int main() {
  int D;
  cin >> D;
  vector<int> C(26 + 1);
  for (int i = 1; i <= 26; i++)
    cin >> C[i];
  vector<vector<int>> S(D + 1, vector<int>(26 + 1));
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++)
      cin >> S[i][j];
  }

  vector<int> T(26 + 1);
  vector<vector<int>> last(D + 1, vector<int>(26 + 1));
  ll sum = 0;
  for (int d = 1; d <= D; d++) {
    int num = 0;
    ll grd = (-1) * INF;
    int daybef = 0;
    for (int t = 1; t <= 26; t++) {
      ll tmp = 0;
      tmp += (ll)S[d][t];
      int bef = last[d][t];
      last[d][t] = d;
      for (int i = 1; i <= 26; i++) {
        tmp -= (ll)C[i] * (d - last[d][i]);
      }
      if (chmax(grd, tmp)) {
        num = t;
        daybef = bef;
      }
      last[d][t] = bef;
    }
    last[d][num] = d;
    for (int i = daybef; i < d; i++) {
      last[i][num] = daybef;
    }
    T[d] = num;
    sum += (ll)S[d][num];
    for (int i = 1; i <= 26; i++) {
      sum -= (ll)C[i] * (d - last[d][i]);
    }
  }

  for (int q = 0; q <= 100000; q++) {
    int d = 11 * q % D + 1;
    int u = 11 * q % 26 + 1;
    ll oldSum = sum;
    sum -= S[d][T[d]];
    for (int i = 1; i <= 26; i++) {
      sum += (ll)C[i] * (d - last[d][i]);
    }
    int oldT = T[d];
    T[d] = u;
    sum += S[d][u];
    last[d][oldT] = last[d - 1][oldT];
    int bef = last[d][u];
    last[d][u] = d;
    for (int i = 1; i <= 26; i++) {
      sum -= (ll)C[i] * (d - last[d][i]);
    }
    if (oldSum >= sum) {
      T[d] = oldT;
      last[d][T[d]] = d;
      last[d][u] = bef;
      sum = oldSum;
    }
  }

  for (int i = 1; i <= D; i++) {
    printf("%d\\n", T[i]);
  }

  return 0;
}