Problem p02619 (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;

// すべて 0-indexで考える。結果の出力時に1を足す

struct Input {
  int D;
  VI c;
  VVI s;
};

Input get_input() {
  int D;
  cin >> D;
  VI c(N_CONTEST);
  VVI s(D, VI(N_CONTEST));

  for (int i = 0; i < c.size(); i++) {
    cin >> c[i];
  }
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < s[i].size(); j++) {
      cin >> s[i][j];
    }
  }

  Input input;
  input.D = D;
  input.c = c;
  input.s = s;

  return input;
}

void show_results(const VI &t) {
  for (auto x : t) {
    cout << x + 1 << endl;
  }
}

long long calc_score(const Input &input, const VI &t) {
  long long score = 0;
  VI lastdays(N_CONTEST, -1);
  for (int i = 0; i < t.size(); i++) {
    score += input.s[i][t[i]];

    lastdays[t[i]] = i;
    for (int j = 0; j < N_CONTEST; j++) {
      score -= (long long)input.c[j] * (i - lastdays[j]);
    }
  }

  return score;
}

void solve_b() {
  auto input = get_input();
  int tmp;
  VI t;
  VL scores(input.D);
  for (int i = 0; i < input.D; i++) {
    cin >> tmp;
    // 0-index
    t.emplace_back(tmp - 1);
    scores[i] = calc_score(input, t);
  }

  for (auto x : scores) {
    cout << x << endl;
  }
}

int main() {
  solve_b();
  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;
}