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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int D = 365;
vector<int> c(26);
vector<vector<int>> s(365, vector<int>(26));

ll calc(const vector<int> &t, int d, int q) {
  ll res = 0;
  vector<int> lst(26, -1);
  rep(i, D) {
    if (i != d) {
      if (t[i] != -1) {
        res += s[i][t[i]];
        lst[t[i]] = i;
      }
    } else {
      res += s[i][q];
      lst[q] = i;
    }
    rep(j, 26) { res -= c[j] * (i - lst[j]); }
  }
  // cerr << res << '\\n';
  return res;
}

ll calc2(const vector<int> &t, int d1, int d2) {
  ll res = 0;
  vector<int> lst(26, -1);
  rep(i, D) {
    if (i != d1 && i != d2) {
      res += s[i][t[i]];
      lst[t[i]] = i;
    } else if (i == d1) {
      res += s[i][d1];
      lst[t[d1]] = i;
    } else if (i == d2) {
      res += s[i][d2];
      lst[t[d2]] = i;
    }
    rep(j, 26) { res -= c[j] * (i - lst[j]); }
  }
  // cerr << res << '\\n';
  return res;
}

int main() {
  /*
  ifstream in("input.in");
  cin.rdbuf(in.rdbuf());
  ofstream ofs("output.txt");
  cout.rdbuf(ofs.rdbuf());
  //*/
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  mt19937 mt(184);
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(y, D) rep(x, 26) cin >> s[y][x];
  vector<int> t(D, -1);
  ll now = calc(t, -1, -1);
  rep(d, D) {
    int k = -1;
    ll prenow = -1;
    rep(q, 26) {
      ll pre = calc(t, d, q);
      if (now < pre) {
        if (mt() & 1 || mt() & 1 || mt() & 1) {
          now = pre;
          t[d] = q;
        }
        if (k == -1) {
          k = q;
          prenow = pre;
        }
      }
      if (chmax(now, calc(t, d, q))) {
        t[d] = q;
      }
      end = chrono::system_clock::now();
      double ela =
          chrono::duration_cast<chrono::milliseconds>(end - start).count();
      if (ela > 1900)
        break;
    }
    if (t[d] == -1) {
      t[d] = k;
      now = prenow;
    }
    // timer
    end = chrono::system_clock::now();
    double ela =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    if (ela > 1900)
      break;
  }
  while (1) {
    int d1 = mt() % 365, d2 = mt() % 365;
    if (d1 == d2)
      continue;
    if (chmax(now, calc2(t, d1, d2))) {
      int tmp = t[d1];
      t[d1] = t[d2];
      t[d2] = tmp;
    }
    end = chrono::system_clock::now();
    double ela =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    if (ela > 1970)
      break;
  }
  rep(i, D) cout << t[i] + 1 << '\\n';
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int D = 365;
vector<int> c(26);
vector<vector<int>> s(365, vector<int>(26));

ll calc(const vector<int> &t, int d, int q) {
  ll res = 0;
  vector<int> lst(26, -1);
  rep(i, D) {
    if (i != d) {
      if (t[i] != -1) {
        res += s[i][t[i]];
        lst[t[i]] = i;
      }
    } else {
      res += s[i][q];
      lst[q] = i;
    }
    rep(j, 26) { res -= c[j] * (i - lst[j]); }
  }
  // cerr << res << '\\n';
  return res;
}

ll calc2(const vector<int> &t, int d1, int d2) {
  ll res = 0;
  vector<int> lst(26, -1);
  rep(i, D) {
    if (i != d1 && i != d2) {
      res += s[i][t[i]];
      lst[t[i]] = i;
    } else if (i == d1) {
      res += s[i][d1];
      lst[d1] = i;
    } else if (i == d2) {
      res += s[i][d2];
      lst[d2] = i;
    }
    rep(j, 26) { res -= c[j] * (i - lst[j]); }
  }
  // cerr << res << '\\n';
  return res;
}

int main() {
  /*
  ifstream in("input.in");
  cin.rdbuf(in.rdbuf());
  ofstream ofs("output.txt");
  cout.rdbuf(ofs.rdbuf());
  //*/
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  mt19937 mt(184);
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(y, D) rep(x, 26) cin >> s[y][x];
  vector<int> t(D, -1);
  ll now = calc(t, -1, -1);
  rep(d, D) {
    int k = -1;
    ll prenow = -1;
    rep(q, 26) {
      ll pre = calc(t, d, q);
      if (now < pre) {
        if (mt() & 1 || mt() & 1 || mt() & 1) {
          now = pre;
          t[d] = q;
        }
        if (k == -1) {
          k = q;
          prenow = pre;
        }
      }
      if (chmax(now, calc(t, d, q))) {
        t[d] = q;
      }
      end = chrono::system_clock::now();
      double ela =
          chrono::duration_cast<chrono::milliseconds>(end - start).count();
      if (ela > 1900)
        break;
    }
    if (t[d] == -1) {
      t[d] = k;
      now = prenow;
    }
    // timer
    end = chrono::system_clock::now();
    double ela =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    if (ela > 1900)
      break;
  }
  while (1) {
    int d1 = mt() % 365, d2 = mt() % 365;
    if (d1 == d2)
      continue;
    if (chmax(now, calc2(t, d1, d2))) {
      int tmp = t[d1];
      t[d1] = t[d2];
      t[d2] = tmp;
    }
    end = chrono::system_clock::now();
    double ela =
        chrono::duration_cast<chrono::milliseconds>(end - start).count();
    if (ela > 1970)
      break;
  }
  rep(i, D) cout << t[i] + 1 << '\\n';
  return 0;
}