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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

struct point {
  ll next;
  ll score;
};

ll get_cluster_result(vector<point> &points, vector<bool> &done, ll start,
                      ll K) {
  ll loop_score = 0;
  vector<ll> cluster;
  for (point p = points[start]; true; p = points[p.next]) {
    cluster.push_back(p.next);
    done[p.next] = true;
    loop_score += points[p.next].score;
    if (p.next == start) {
      break;
    }
  }

  ll size = cluster.size();
  ll shu = 0;
  ll rest_move;
  if (loop_score > 0) {
    shu = max(0LL, K / size - 1LL);
    rest_move = K - shu * size;
  } else {
    rest_move = min(K, size);
  }

  ll max_rest = -INF;
  for (ll c : cluster) {
    ll rest_score = 0;
    point p = points[c];
    for (ll j = 0; j < rest_move; j++) {
      rest_score += points[p.next].score;
      p = points[p.next];
      max_rest = max(max_rest, rest_score);
    }
  }
  return max_rest + shu * loop_score;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<point> points(N + 1, {-1, 0});
  for (ll i = 1; i <= N; i++) {
    cin >> points[i].next;
  }
  for (ll i = 1; i <= N; i++) {
    cin >> points[i].score;
  }
  vector<bool> done(N + 1, false);

  ll gresult = -INF;
  for (ll i = 1; i <= N; i++) {
    if (!done[i]) {
      gresult = max(gresult, get_cluster_result(points, done, i, K));
    }
  }
  print(gresult);
}


Error: -6
free(): invalid size


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}

struct point {
  ll next;
  ll cost;
};
bool operator<(const point &l, const point &r) { return l.next < r.next; }
bool operator==(const point &l, const point &r) { return (l.next == r.next); }
bool operator>(const point &l, const point &r) { return !(l == r) && !(l < r); }
bool operator!=(const point &l, const point &r) { return !(l == r); }
bool operator>=(const point &l, const point &r) { return (l == r) || (l > r); }
bool operator<=(const point &l, const point &r) { return (l == r) || (l < r); }

int main() {
  ll N, K;

  cin >> N >> K;
  vector<point> points(N + 1);
  ll INF = 100000000000000;

  for (ll i = 1; i <= N; i++) {
    cin >> points[i].next;
  }

  for (ll i = 1; i <= N; i++) {
    cin >> points[i].cost;
  }

  ll gresult = -INF;
  for (ll i = 1; i <= N; i++) {
    ll isshu_cost = 0;
    point p = points[i];
    vector<ll> cluster;
    while (true) {
      point np = points[p.next];
      cluster.push_back(p.next);
      isshu_cost += np.cost;
      if (np == points[i]) {
        break;
      }
      p = np;
    }

    ll RK = -1;
    ll result = 0;
    ll size = cluster.size();
    if (isshu_cost > 0 && K > size) {
      ll shu = K / size;
      result += shu * isshu_cost;
      RK = K - shu * size;
    } else if (K > size) {
      RK = size - 1;
    } else {
      RK = K;
    }

    // スタート地点ごとの最大点
    vector<ll> max_move(size);
    for (ll k = 0; k < size; k++) {
      max_move[i] = -INF;
      ll total = 0;
      point pp = points[i];
      for (ll j = 0; j < RK; j++) {
        total += points[pp.next].cost;
        pp = points[pp.next];
        max_move[i] = max(total, max_move[i]);
      }
    }

    ll max_max_move = -INF;
    for (ll k = 0; k < size; k++) {
      max_max_move = max(max_max_move, max_move[i]);
    }
    result += max_max_move;
    gresult = max(result, gresult);
  }
  print(gresult);
}