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

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll nxt[MAX], need, c[MAX];
vector<ll> v;
void tour(int par, int k) {
  v.clear();
  ll sum = 0, ans = 0, it = 0, cur = par;
  while (nxt[cur] != par) {
    cur = nxt[cur];
    sum += c[cur];
    v.push_back(sum);
  }
  v.push_back(sum + c[par]);
}
ll test(ll k) {
  ll sz = v.size();
  ll mx = 0, ans = -1e18;
  if (sz == 0)
    return ans;
  if (k > sz) {
    ll cur = 0;
    cur = v[sz - 1];
    ll mul = k / sz;
    cur *= mul;
    ll rem = k % sz;
    mx = 0;
    for (int i = 0; i < rem; i++)
      mx = max(mx, v[i]);
    cur += mx;
    if (rem == 0) {
      for (int i = sz - 1; i >= 0; i--)
        ans = max(ans, cur - v[i]);
    }
    ans = max(ans, cur);
    for (int i = 0; i < sz; i++)
      ans = max(ans, v[i]);
  } else {
    for (int i = 0; i < k; i++)
      ans = max(ans, v[i]);
  }
  return ans;
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int n,
      k;
  cin >> n >> k;
  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    cin >> nxt[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    tour(i, k);
    ans = max(ans, test(k));
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll nxt[MAX], need, c[MAX];
vector<ll> v;
ll tour(int par, int k) {
  v.clear();
  ll sum = 0, ans = 0, it = 0, cur = par;
  while (nxt[cur] != par) {
    cur = nxt[cur];
    sum += c[cur];
    v.push_back(sum);
  }
  v.push_back(sum + c[par]);
}
ll test(int k) {
  int sz = v.size();
  ll mx = 0, ans = -1e18;
  if (sz == 0)
    return ans;
  if (k > sz) {
    ll cur = 0;
    cur = v[sz - 1];
    ll mul = k / sz;
    cur *= mul;
    ll rem = k % sz;
    mx = 0;
    for (int i = 0; i < rem; i++)
      mx = max(mx, v[i]);
    cur += mx;
    ans = max(ans, cur);
    for (int i = 0; i < sz; i++)
      ans = max(ans, v[i]);
  } else {
    for (int i = 0; i < k; i++)
      ans = max(ans, v[i]);
  }
  return ans;
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int n,
      k;
  cin >> n >> k;
  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    cin >> nxt[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    tour(i, k);
    ans = max(ans, test(k));
  }
  cout << ans << endl;

  return 0;
}