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

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<long long>;
using VVL = vector<VL>;

const long long INF = 1e18;
const int N_CONTEST = 26;

int D;
VI c;
VVI s;

long long calc_sat_constest_day(int contest, int day, const VI &lastdays) {
  long long ret = s[day][contest];
  for (int i = 1; i <= N_CONTEST; i++) {
    if (i != contest) {
      ret -= (long long)c[i] * (day - lastdays[i]);
    }
  }
  return ret;
}

VL calc_sats(const VL &t, bool shows = false) {
  long long sat = 0;
  VL sats(D + 1, 0);
  VI lastdays(N_CONTEST + 1, 0);

  for (int i = 1; i <= D; i++) {
    int contest = t[i];

    lastdays[contest] = i;
    long long sat_day = calc_sat_constest_day(contest, i, lastdays);
    sat += sat_day;
    sats[i] = sat;
  }

  if (shows) {
    for (int i = 0; i <= D; i++) {
      printf("%3d: %15lld\\n", i, sats[i]);
    }
  }

  return sats;
}

VL solve_greedy() {
  VL t(D + 1, -1);

  VI lastdays(N_CONTEST + 1, 0);
  for (int i = 1; i <= D; i++) {
    long long maxi = -INF;
    int cand = -1;
    for (int j = 1; j <= N_CONTEST; j++) {
      long long sc = calc_sat_constest_day(j, i, lastdays);
      if (sc > maxi) {
        maxi = sc;
        cand = j;
      }
    }

    t[i] = cand;
    lastdays[cand] = i;
  }

  return t;
}

int main() {
  cin >> D;

  c.assign(N_CONTEST + 1, 0);
  for (int i = 1; i <= N_CONTEST; i++) {
    cin >> c[i];
  }
  s.assign(D + 1, VI(N_CONTEST + 1, 0));
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= N_CONTEST + 1; j++) {
      cin >> s[i][j];
    }
  }

  VL t = solve_greedy();
  for (int i = 1; i <= D; i++) {
    cout << t[i] << endl;
  }
  auto sats = calc_sats(t);
  auto sat = sats.back();
  auto score = max<long long>(0, sat + 1000000);
  // cout << sats.back() << endl;
  // cout << score << endl;

  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<long long>;
using VVL = vector<VL>;

const long long INF = 1e18;
const int N_CONTEST = 26;

int D;
VI c;
VVI s;

long long calc_sat_constest_day(int contest, int day, const VI &lastdays) {
  long long ret = s[day][contest];
  for (int i = 1; i <= N_CONTEST; i++) {
    if (i != contest) {
      ret -= (long long)c[i] * (day - lastdays[i]);
    }
  }
  return ret;
}

VL calc_sats(const VL &t, bool shows = false) {
  long long sat = 0;
  VL sats(D + 1, 0);
  VI lastdays(N_CONTEST + 1, 0);

  for (int i = 1; i <= D; i++) {
    int contest = t[i];

    lastdays[contest] = i;
    long long sat_day = calc_sat_constest_day(contest, i, lastdays);
    sat += sat_day;
    sats[i] = sat;
  }

  if (shows) {
    for (int i = 0; i <= D; i++) {
      printf("%3d: %15lld\\n", i, sats[i]);
    }
  }

  return sats;
}

VL solve_greedy() {
  VL t(D, -1);

  VI lastdays(N_CONTEST + 1, 0);
  for (int i = 1; i <= D; i++) {
    long long maxi = -INF;
    int cand = -1;
    for (int j = 1; j <= N_CONTEST; j++) {
      long long sc = calc_sat_constest_day(j, i, lastdays);
      if (sc > maxi) {
        maxi = sc;
        cand = j;
      }
    }

    t[i] = cand;
    lastdays[cand] = i;

    // printf("%3d: ", i);
    // for (int j = 1; j <= N_CONTEST; j++) {
    // 	printf("%3d ", i - lastdays[j]);
    // }
    // printf("\\n");
  }

  return t;
}

int main() {
  cin >> D;

  c.assign(N_CONTEST + 1, 0);
  for (int i = 1; i <= N_CONTEST; i++) {
    cin >> c[i];
  }
  s.assign(D + 1, VI(N_CONTEST + 1, 0));
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= N_CONTEST + 1; j++) {
      cin >> s[i][j];
    }
  }

  VL t = solve_greedy();
  for (int i = 1; i <= D; i++) {
    cout << t[i] << endl;
  }
  auto sats = calc_sats(t);
  auto sat = sats.back();
  auto score = max<long long>(0, sat + 1000000);
  // cout << sats.back() << endl;
  // cout << score << endl;

  return 0;
}