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

//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = int64_t;
using ull = uint64_t;

[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)

template <typename T> void chmin(T &x, T y) { x = min(x, y); }

template <typename T> void chmax(T &x, T y) { x = max(x, y); }

ll csum(ll x, ll y) {
  if (x > 0 && y > 0 && x > LL_MAX - y) {
    return LL_MAX;
  }
  if (x < 0 && y < 0 && x < LL_MIN - y) {
    return LL_MIN;
  }
  return x + y;
}

template <typename T> vector<T> make_vec_nd(T init, ll size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

ll readl() {
  ll ret;
  scanf("%lld", &ret);
  return ret;
}

vector<ll> readvl(ll n) {
  vector<ll> ret(n);
  rep(i, 0, n) { ret[i] = readl(); }
  return ret;
}

void writel(ll x) { printf("%lld\\n", x); }

int main() {
  ll N = readl();
  ll K = readl();
  K--;
  vector<ll> next = readvl(N);
  rep(i, 0, N) { next[i]--; }
  vector<ll> scores = readvl(N);
  ll ans = LL_MIN;
  rep(start, 0, N) {
    ll now = start;
    ll sum = 0;
    ll loop = 0;
    vector<ll> sum_max;
    sum_max.push_back(sum);
    vector<ll> sums;
    sums.push_back(0);
    do {
      now = next[now];
      sum += scores[now];
      loop++;
      sum_max.push_back(max(sum, sum_max.back()));
      sums.push_back(sum);
    } while (now != start);
    ll score = scores[start];
    ll score_sub = LL_MIN;
    rep(mod, 0, loop) {
      if (mod > K) {
        break;
      }
      ll score_sub_loc = sums[mod];
      chmax(score_sub, score_sub_loc);
      score_sub_loc += (K - mod) / loop * sum;
      chmax(score_sub, score_sub_loc);
    }
    score += score_sub;
    chmax(ans, score);
  }
  writel(ans);
}

/*

 5 6
 2 4 1 5 3
 1 2 4 -1 -4

 */


Error: -11

Fixed submission:

//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = __int128_t;
using ull = uint64_t;

[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)

template <typename T> void chmin(T &x, T y) { x = min(x, y); }

template <typename T> void chmax(T &x, T y) { x = max(x, y); }

ll csum(ll x, ll y) {
  if (x > 0 && y > 0 && x > LL_MAX - y) {
    return LL_MAX;
  }
  if (x < 0 && y < 0 && x < LL_MIN - y) {
    return LL_MIN;
  }
  return x + y;
}

template <typename T> vector<T> make_vec_nd(T init, ll size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

ll readl() {
  ll ret;
  scanf("%ld", &ret);
  return ret;
}

vector<ll> readvl(ll n) {
  vector<ll> ret(n);
  rep(i, 0, n) { ret[i] = readl(); }
  return ret;
}

void writel(ll x) { printf("%ld", x); }

int main() {
  ll N = readl();
  ll K = readl();
  K--;
  vector<ll> next = readvl(N);
  rep(i, 0, N) { next[i]--; }
  vector<ll> scores = readvl(N);
  ll ans = LL_MIN;
  rep(start, 0, N) {
    ll now = start;
    ll sum = 0;
    ll loop = 0;
    vector<ll> sum_max;
    sum_max.push_back(sum);
    do {
      now = next[now];
      sum += scores[now];
      loop++;
      sum_max.push_back(max(sum, sum_max.back()));
    } while (now != start);
    ll mod = K % loop;
    ll div = K / loop;
    ll score = scores[start];
    score += sum_max[mod];
    if (sum > 0) {
      score += div * sum;
    }
    chmax(ans, score);
  }
  writel(ans);
}

/*

2 100
2 1
0 1

*/