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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T>

void view(const std::vector<T> &v) {
#ifndef ONLINE_JUDGE
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
#endif
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vll p(n), c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) p[i]--;
  rep(i, n) cin >> c[i];
  vint vis(n);
  int loopId = 1;
  vvll g;
  rep(i, n) {
    if (vis[i])
      continue;
    vll now = vll();
    int j = p[i];
    now.emplace_back(c[i]);
    while (j != i) {
      vis[j] = loopId;
      now.emplace_back(c[j]);
      j = p[j];
    }
    g.emplace_back(now);
    loopId++;
  }

  ll ans = INT64_MIN;
  for (vll now : g) {
    int m = now.size();
    ll loopScore = 0;
    rep(i, m) { loopScore += now[i]; }
    ll maxScore = INT64_MIN;
    rep(i, m) {
      int j = i;
      ll sc = 0;
      rep(l, min(m, k)) {
        j = (i + l) % m;
        sc += now[j];
        ll loops = ((k - l - 1) / m);
        ll nowScore = loopScore * loops + sc;
        // printf("%d %d %d %d %d %d\\n", loopScore * loops, k - l - 1, m,
        // loopScore, sc, nowScore);
        nowScore = max(nowScore, sc);
        maxScore = max(nowScore, maxScore);
      }
    }

    ans = max(ans, maxScore);
  }

  cout << ans << endl;

  return 0;
}

Error: -8

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T>

void view(const std::vector<T> &v) {
#ifndef ONLINE_JUDGE
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
#endif
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

vint vis;
int loop = 1;

vll p, c;
void loop_det(int i) {
  if (vis[i])
    return;
  vis[i] = loop;
  loop_det(p[i]);
};

int main() {
  ll n, k;
  cin >> n >> k;
  p = vll(n);
  c = vll(n);
  vis = vint(n, 0);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }

  map<int, int> entry;
  rep(i, n) {
    if (vis[i])
      continue;
    loop_det(i);
    loop++;
    entry[loop] = i;
  }

  ll maxLoop = INT64_MIN;
  int loopSize = 0;
  int enter = 0;
  for (P pr : entry) {
    int i = pr.second;
    ll score = c[i];
    int now = p[i];
    int cnt = 1;
    while (now != i) {
      score += c[now];
      now = p[now];
      cnt++;
    }
    if (score * (k / loopSize) > maxLoop) {
      maxLoop = score;
      loopSize = cnt;
      enter = i;
    }
  }

  if (maxLoop < 0 || k <= loopSize) {
    ll ans = INT64_MIN;
    rep(i, n) {
      ll score = 0;
      int now = i;
      rep(j, min(n, k)) {
        score += c[now];
        now = p[now];
        if (ans < score) {
          ans = score;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }

  int g = loopSize;
  ll maxScore = INT64_MIN;
  int id = vis[enter];
  rep(i, n) {
    if (vis[i] != id)
      continue;

    ll score = 0;
    int now = i;
    int use = 0;
    rep(j, g) {
      use++;
      int l = k - use;
      score += c[now];
      ll nowSc = maxLoop * (l / loopSize) + score;
      now = p[now];
      if (nowSc > maxScore) {
        maxScore = nowSc;
      }
    }
  }

  cout << maxScore << endl;
  return 0;
}