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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;

#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  vector<ll> P(N + 1, 0);
  vector<ll> C(N + 1, 0);
  for (ll i = 0; i < N; ++i)
    cin >> P[i + 1];
  for (ll i = 0; i < N; ++i)
    cin >> C[i + 1];
  vector<ll> real(N, 0);
  real[0] = 1;
  for (ll i = 1; i < N; ++i) {
    real[i] = P[real[i - 1]];
  }
  ll ans = -INF;
  for (ll i = 1; i <= N; ++i) {
    ll start = i;
    ll now = i;
    now = P[start];
    vl score = {C[now]};
    while (1) {
      now = P[now];
      score.emplace_back(C[now]);
      if (now == start)
        break;
    }
    //  debug(score)
    ll siz = score.size();
    vector<ll> sumuntil(siz, 0);
    sumuntil[0] = score[0];
    for (ll i = 1; i < siz; ++i) {
      sumuntil[i] = sumuntil[i - 1] + score[i];
    }
    ll ret = sumuntil[0];
    ll sum = sumuntil[siz - 1];
    // debug(sumuntil)
    if (sum > 0) {
      for (ll i = 0; i < min(siz, K); ++i) {
        chmax(ret, sumuntil[i] + sum * max(0LL, ((K - i - 1) / siz)));
      }
    }
    if (sum <= 0) {
      for (ll i = 0; i < min(siz, K); ++i) {
        chmax(ret, sumuntil[i]);
      }
    }
    chmax(ans, ret);
  }
  prt(ans);

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;

#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  vector<ll> P(N + 1, 0);
  vector<ll> C(N + 1, 0);
  for (ll i = 0; i < N; ++i)
    cin >> P[i + 1];
  for (ll i = 0; i < N; ++i)
    cin >> C[i + 1];
  vector<ll> real(N, 0);
  real[0] = 1;
  for (ll i = 1; i < N; ++i) {
    real[i] = P[real[i - 1]];
  }
  ll ans = -INF;
  for (ll i = 1; i <= N; ++i) {
    ll start = i;
    ll now = i;
    now = P[start];
    vl score = {C[now]};
    while (1) {
      now = P[now];
      score.emplace_back(C[now]);
      if (now == start)
        break;
    }
    //  debug(score)
    ll siz = score.size();
    vector<ll> sumuntil(siz, 0);
    sumuntil[0] = score[0];
    for (ll i = 1; i < siz; ++i) {
      sumuntil[i] = sumuntil[i - 1] + score[i];
    }
    ll ret = sumuntil[0];
    ll sum = sumuntil[siz - 1];
    // debug(sumuntil)
    if (sum > 0) {
      for (ll i = 0; i < max(siz, K); ++i) {
        chmax(ret, sumuntil[i] + sum * max(0LL, ((K - i - 1) / siz)));
      }
    }
    if (sum <= 0) {
      for (ll i = 0; i < max(siz, K); ++i) {
        chmax(ret, sumuntil[i]);
      }
    }
    chmax(ans, ret);
  }
  prt(ans);

  return 0;
}