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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i))

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  rep(i, 26) cin >> c[i];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  vector<int> t(D);
  ll v = 0;
  vector<vector<ll>> Cost(D, vector<ll>(26, 0));
  rep(i, 26) Cost[0][i] = c[i];
  rep(i, D) {
    vector<ll> X(26);
    rep(j, 26) X[j] = s[i][j] + Cost[i][j];
    auto it = max_element(X.begin(), X.end());
    int index = it - X.begin();
    t[i] = index;
    Cost[i][index] = 0;
    v += s[i][index];
    rep(j, 26) {
      v -= Cost[i][j];
      if (i + 1 < D)
        Cost[i + 1][j] = Cost[i][j] + c[j];
    }
  }
  // rep(i, D) cout << t[i] + 1 << endl;

  end = chrono::system_clock::now();
  auto elapsed =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();
  srand(time(NULL));
  while (elapsed < 1900) {
    int d, q;
    d = rand() % D;
    q = rand() % 26;
    vector<vector<ll>> Cost2 = Cost;
    ll v2 = v;
    int x = d + 1;
    v2 += s[d][q] - s[d][t[d]];
    v2 += Cost2[d][q];
    Cost2[d][q] = 0;
    while (x < D && t[x] != q) {
      v2 += Cost2[x][q];
      Cost2[x][q] = Cost2[x - 1][q] + c[q];
      v2 -= Cost2[x][q];
      x++;
    }

    x = d + 1;
    int y = t[d];
    if (d == 0) {
      Cost2[d][y] = c[y];
      v2 -= Cost2[d][y];
    } else {
      Cost2[d][y] = Cost2[d - 1][y] + c[y];
      v2 -= Cost2[d][y];
    }
    while (x < D && t[x] != y) {
      v2 += Cost2[x][y];
      Cost2[x][y] = Cost2[x - 1][y] + c[y];
      v2 -= Cost2[x][y];
      x++;
    }
    if (v < v2) {
      t[d] = q;
      Cost = Cost2;
      v = v2;
    }
    end = chrono::system_clock::now();
    elapsed = chrono::duration_cast<chrono::milliseconds>(end - start).count();
    // cout << v << endl;
  }
  rep(i, D) cout << t[i] + 1 << endl;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i))

int main() {
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();

  int D;
  cin >> D;
  vector<int> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  rep(i, 26) cin >> c[i];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  vector<int> t(D);
  rep(i, D) {
    cin >> t[i];
    t[i]--;
  }

  vector<int> ans(D);
  vector<vector<ll>> Cost(D, vector<ll>(26, 0));
  rep(i, 26) Cost[0][i] = c[i];
  ll v = 0;
  rep(i, D) {
    int index = t[i];
    Cost[i][index] = 0;
    v += s[i][index];
    rep(j, 26) {
      v -= Cost[i][j];
      if (i + 1 < D)
        Cost[i + 1][j] = Cost[i][j] + c[j];
    }
    // cout << v << endl;
  }
  // cout << v << endl;
  // int M;
  // cin >> M;
  end = chrono::system_clock::now();
  auto elapsed =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();
  srand(time(NULL));
  while (elapsed < 1800) {
    int d, q;
    d = rand() % D;
    q = rand() % 26;
    vector<vector<ll>> Cost2 = Cost;
    ll v2 = v;
    int x = d + 1;
    v2 += s[d][q] - s[d][t[d]];
    v2 += Cost2[d][q];
    Cost2[d][q] = 0;
    while (x < D && t[x] != q) {
      v2 += Cost2[x][q];
      Cost2[x][q] = Cost2[x - 1][q] + c[q];
      v2 -= Cost2[x][q];
      x++;
    }

    x = d + 1;
    int y = t[d];
    if (d == 0) {
      Cost2[d][y] = c[y];
      v2 -= Cost2[d][y];
    } else {
      Cost2[d][y] = Cost2[d - 1][y] + c[y];
      v2 -= Cost2[d][y];
    }
    while (x < D && t[x] != y) {
      v2 += Cost2[x][y];
      Cost2[x][y] = Cost2[x - 1][y] + c[y];
      v2 -= Cost2[x][y];
      x++;
    }
    if (v < v2) {
      t[d] = q;
      Cost = Cost2;
      v = v2;
    }
    end = chrono::system_clock::now();
    elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
                  .count();
    // cout << v << endl;
  }
  rep(i, D - 1) cout << ans[i] << " ";
  cout << ans[D - 1] << endl;
}