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

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int reduce_ene(int day, int except_num, vector<int> &c, vector<int> &last) {
  int i;
  int sum_reduce;
  sum_reduce = 0;

  for (i = 0; i < 26; i++) {
    if (i != except_num - 1) {
      sum_reduce += c[i] * (day + 1 - last[i]);
    }
  }
  return sum_reduce;
}

int plus_ene(int day, vector<int> &t, vector<vector<int>> &s) {
  return s[day][t[day] - 1];
}

int ene_cal(int D, vector<int> &t, vector<vector<int>> &s, vector<int> &c) {
  int day;
  int ene;
  int i;
  ene = 0;
  vector<int> last;
  for (i = 0; i < 26; i++) {
    last.push_back(0);
  }
  for (day = 0; day < D; day++) {
    ene += plus_ene(day, t, s);
    ene -= reduce_ene(day, t[day], c, last);
    // cout << ene << endl;
    last[t[day] - 1] = day + 1;
  }
  return ene;
}

int main() {
  clock_t start, end;
  /* init */

  int i, j;
  int D;
  vector<int> c;
  vector<int> t;

  cin >> D;
  int sum_c;
  sum_c = 0;
  for (i = 0; i < 26; i++) {
    int ci;
    cin >> ci;
    c.push_back(ci);
    sum_c += ci;
  }

  vector<vector<int>> s(D, vector<int>(26));

  for (j = 0; j < D; j++) {
    for (i = 0; i < 26; i++) {
      int sij;
      cin >> sij;
      s.at(j).at(i) = sij;
    }
  }
  start = clock();

  for (i = 0; i < D; i++) {
    int ti;
    ti = rand() % 26 + 1;
    t.push_back(ti);
  }

  // end init
  int ene;

  ene = ene_cal(D, t, s, c);
  int ene_old;
  int t_old;
  int rand_d;
  int rand_q;
  bool flag;
  flag = true;

  while (flag == true) {
    rand_d = rand() % D;
    rand_q = rand() % 26;

    t_old = t[rand_d];
    ene_old = ene;
    t[rand_d] = rand_q + 1;
    ene = ene_cal(D, t, s, c);
    if (ene < ene_old) {
      t[rand_d] = t_old;
      ene = ene_old;
    }
    end = clock();
    if (double(end - start) > 1800.00) {
      flag = false;
      break;
    }
  }

  for (i = 0; i < D; i++) {
    cout << t[i] << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int reduce_ene(int day, int except_num, vector<int> &c, vector<int> &last) {
  int i;
  int sum_reduce;
  sum_reduce = 0;

  for (i = 0; i < 26; i++) {
    if (i != except_num - 1) {
      sum_reduce += c[i] * (day + 1 - last[i]);
    }
  }
  return sum_reduce;
}

int plus_ene(int day, vector<int> &t, vector<vector<int>> &s) {
  return s[day][t[day] - 1];
}

int ene_cal(int D, vector<int> &t, vector<vector<int>> &s, vector<int> &c) {
  int day;
  int ene;
  ene = 0;
  vector<int> last(26, 0);
  for (day = 0; day < D; day++) {
    ene += plus_ene(day, t, s);
    ene -= reduce_ene(day, t[day], c, last);
    // cout << ene << endl;
    last[t[day] - 1] = day + 1;
  }
  return ene;
}

int main() {
  clock_t start, end;
  /* init */

  int i, j;
  int D;
  vector<int> c;
  vector<int> t;

  cin >> D;
  int sum_c;
  sum_c = 0;
  for (i = 0; i < 26; i++) {
    int ci;
    cin >> ci;
    c.push_back(ci);
    sum_c += ci;
  }

  vector<vector<int>> s(D, vector<int>(26));

  for (j = 0; j < D; j++) {
    for (i = 0; i < 26; i++) {
      int sij;
      cin >> sij;
      s.at(j).at(i) = sij;
    }
  }
  start = clock();

  /*
  for(i=0;i<D;i++){
      int ti;
      cin >> ti;
      t.push_back(ti);
  }
  */
  for (i = 0; i < D; i++) {
    int ti;
    ti = rand() % 26;
    t.push_back(ti);
  }

  // end init
  int ene;

  ene = ene_cal(D, t, s, c);
  int ene_old;
  int t_old;
  int rand_d;
  int rand_q;
  bool flag;
  flag = true;

  while (flag == true) {
    rand_d = rand() % D;
    rand_q = rand() % 26;

    t_old = t[rand_d];
    ene_old = ene;
    t[rand_d] = rand_q + 1;
    ene = ene_cal(D, t, s, c);
    if (ene < ene_old) {
      t[rand_d] = t_old;
      ene = ene_old;
    }
    end = clock();
    if (double(end - start) > 1800.00) {
      flag = false;
      break;
    }
  }

  for (i = 0; i < D; i++) {
    cout << t[i] << endl;
  }

  return 0;
}