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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  clock_t start = clock();
  ll D;
  cin >> D;
  vector<ll> C(26);
  rep(i, 26) cin >> C[i];
  vector<vector<ll>> S(D, vector<ll>(26));
  rep(i, D) {
    rep(j, 26) { cin >> S[i][j]; }
  }
  vector<ll> T(D);

  rep(i, D) {
    ll m = -INF;
    rep(j, 26) {
      if (S[i][j] > m) {
        T[i] = j + 1;
        m = S[i][j];
      }
    }
  }
  vector<ll> last(26, 0);

  ll sum = 0;
  auto f = [&] {
    sum = 0;
    last.assign(26, 0);
    rep(d, D) {
      ll t = T[d];
      sum += S[d][t - 1];
      last[t - 1] = d + 1;
      rep(i, 26) { sum -= C[i] * ((d + 1) - last[i]); }
    }
    return sum;
  };
  ll ans = -INF;
  clock_t end = clock();
  double time = (end - start) / 1000;
  while (time <= 1900) {
    ll r = rand() % 26;
    r++;
    ll d = rand() % 365;
    ll old = T[d];
    T[d] = r;
    ll res = f();
    if (res > ans) {
      ans = res;
    } else {
      T[d] = old;
    }
    // cout << ans << '\\n';
    end = clock();
    time = (end - start) / 1000;
  }
  // cout << time << '\\n';
  rep(d, D) { cout << T[d] << '\\n'; }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  clock_t start = clock();
  ll D;
  cin >> D;
  vector<ll> C(26);
  rep(i, 26) cin >> C[i];
  vector<vector<ll>> S(D, vector<ll>(26));
  rep(i, D) {
    rep(j, 26) { cin >> S[i][j]; }
  }
  vector<ll> T(D);

  rep(i, D) {
    ll m = -INF;
    rep(j, 26) {
      if (S[i][j] > m) {
        T[i] = j;
        m = S[i][j];
      }
    }
  }
  vector<ll> last(26, 0);

  ll sum = 0;
  auto f = [&] {
    sum = 0;
    last.assign(26, 0);
    rep(d, D) {
      ll t = T[d];
      sum += S[d][t - 1];
      last[t - 1] = d + 1;
      rep(i, 26) { sum -= C[i] * ((d + 1) - last[i]); }
    }
    return sum;
  };
  ll ans = -INF;
  clock_t end = clock();
  double time = (end - start) / 1000;
  while (time <= 1900) {
    ll r = rand() % 26;
    r++;
    ll d = rand() % 365;
    ll old = T[d];
    T[d] = r;
    ll res = f();
    if (res > ans) {
      ans = res;
    } else {
      T[d] = old;
    }
    // cout << ans << '\\n';
    end = clock();
    time = (end - start) / 1000;
  }
  // cout << time << '\\n';
  rep(d, D) { cout << T[d] << '\\n'; }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}