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

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

long long s[400][30];
const int Shurui = 26;           // 今回は26種類
int Date;                        // 今回は1年間なので365日
long long Seigenjikan = 1800000; // 実行時間

int main() {
  clock_t start = clock();
  cin >> Date;
  vector<long long> c(Shurui);
  vector<int> last(Shurui, 0); // 前回の日にち
  vector<int> t(Date, 0);      // 開催する順番
  for (int i = 0; i < Shurui; i++)
    t[i] = i % Shurui; // とりあえずの値
  long long wa = 0;
  for (int i = 0; i < Shurui; i++) {
    cin >> c[i];
    wa += c[i];
  }
  for (int i = 0; i < Date; i++) {
    for (int j = 0; j < Shurui; j++) {
      cin >> s[i][j];
    }
  }
  long long kotae = 0;
  long long hikukazu = 0;
  long long atai = 0;
  for (int i = 0; i < Date; i++) {
    atai = 0;
    for (int j = 0; j < Shurui; j++) {
      if (s[i][j] > atai) {
        t[i] = j;
        atai = s[i][j];
      }
    }
  }
  for (int i = 0; i < Date; i++) {
    atai = 0;
    atai = s[i][t[i]];
    hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
    last[t[i]] = i;
    kotae += atai - hikukazu;
  }
  clock_t end = clock();
  long long time = end - start;
  // cout<<start<<" "<<end<<" "<<time<<endl;
  while (time < Seigenjikan) {
    long long d = rand() % Date;
    long long q = rand() % Shurui;
    long long old = t[d];
    t[d] = q;
    long long new_kotae = 0;
    hikukazu = 0;
    for (int i = 0; i < Date; i++) {
      atai = 0;
      atai = s[i][t[i]];
      hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
      last[t[i]] = i + 1;
      new_kotae += atai - hikukazu;
    }
    if (new_kotae < kotae) {
      t[d] = old;
    } else {
      kotae = new_kotae;
      for (long long i = 0; i < Date; i = i + 13) {
        if (i >= Date)
          break;
        t[d + i] = q;
      }
    }
    end = clock();
    time = end - start;
    // if(time>1500000)cout<<time<<endl;
  }
  for (int i = 0; i < Date; i++) {
    cout << t[i] + 1 << endl;
  }
  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

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

long long s[400][30];
const int Shurui = 26;           // 今回は26種類
int Date;                        // 今回は1年間なので365日
long long Seigenjikan = 1800000; // 実行時間

int main() {
  clock_t start = clock();
  cin >> Date;
  vector<int> c(Shurui);
  vector<int> last(Date, 0); // 前回の日にち
  vector<int> t(Date, 0);    // 開催する順番
  for (int i = 0; i < Date; i++)
    t[i] = rand() % Shurui; // とりあえずの値
  long long wa = 0;
  for (int i = 0; i < Shurui; i++) {
    cin >> c[i];
    wa += c[i];
  }
  for (int i = 0; i < Date; i++) {
    for (int j = 0; j < Shurui; j++) {
      cin >> s[i][j];
    }
  }
  long long kotae = 0;
  long long hikukazu = 0;
  long long atai = 0;
  for (int i = 0; i < Date; i++) {
    atai = 0;
    for (int j = 0; j < Shurui; j++) {
      if (s[i][t[i]] - c[j] * (i + 1 - last[t[i]]) > atai) {
        t[i] = j;
        atai = s[i][t[i]] - c[j] * (i + 1 - last[t[i]]);
      }
    }
  }
  for (int i = 0; i < Date; i++) {
    atai = 0;
    atai = s[i][t[i]];
    hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
    last[t[i]] = i + 1;
    kotae += atai - hikukazu;
    // cout<<kotae<<endl;
  }
  clock_t end = clock();
  long long time = end - start;
  // cout<<start<<" "<<end<<" "<<time<<endl;
  while (time < Seigenjikan) {
    int d = rand() % Date;
    int q = rand() % Shurui;
    int old = t[d];
    t[d] = q;
    long long new_kotae = 0;
    hikukazu = 0;
    for (int i = 0; i < Date; i++) {
      atai = 0;
      atai = s[i][t[i]];
      hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
      last[t[i]] = i + 1;
      new_kotae += atai - hikukazu;
    }
    if (new_kotae < kotae) {
      t[d] = old;
    } else {
      kotae = new_kotae;
      for (int i = 0; i < Date; i = i + 13) {
        if (i >= Date)
          break;
        t[d + i] = q;
      }
    }
    end = clock();
    time = end - start;
    // if(time>1500000)cout<<time<<endl;
  }
  for (int i = 0; i < Date; i++) {
    cout << t[i] + 1 << endl;
  }
  return 0;
}