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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

const ll mod = 3;
const int N = 5e3 + 10, M = 1e6 + 10;
const ll INF = 2e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

#define pi acos(-1)

int p[N];
int c[N];
int n, k;
int cnt;
bool vis[N];
int way[N];
__int128 sum[N];

void dfs(int x) {
  if (!vis[p[x]]) {
    way[++cnt] = p[x];
    vis[p[x]] = 1;
    dfs(p[x]);
  }
}

inline void print(__int128 x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  __int128 res = -1000000000000000000000000000000000000000000;
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    cnt = 0;
    dfs(i);
    if (k <= cnt) {
      __int128 ans = -1000000000000000000000000000000000000000000;
      for (int j = 1; j <= k; j++) {
        sum[j] = sum[j - 1] + c[way[j]];
        ans = max(ans, sum[j]);
      }
      res = max(res, ans);
    } else {
      for (int j = 1; j <= cnt; j++) {
        sum[j] = sum[j - 1] + c[way[j]];
      }
      if (sum[cnt] >= 0) {
        if (k % cnt) {
          __int128 s = (k / cnt) * sum[cnt];
          int r = k % cnt;
          for (int j = 0; j <= r; j++)
            res = max(res, s + sum[j]);
        } else {
          __int128 s = (k / cnt - 1) * sum[cnt];
          for (int j = 0; j <= cnt; j++)
            res = max(res, s + sum[j]);
        }
      } else {
        for (int j = 1; j <= cnt; j++)
          res = max(res, sum[j]);
      }
    }
  }
  print(res);
  return 0;
}
/*

*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

const ll mod = 3;
const int N = 5e3 + 10, M = 1e6 + 10;
const ll INF = 2e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

#define pi acos(-1)

int p[N];
int c[N];
int n, k;
int cnt;
bool vis[N];
int way[N];
ll sum[N];

void dfs(int x) {
  if (!vis[p[x]]) {
    way[++cnt] = p[x];
    vis[p[x]] = 1;
    dfs(p[x]);
  }
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  ll res = -INF;
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, (n + 2) << 2);
    cnt = 0;
    dfs(i);
    if (k <= cnt) {
      ll ans = -INF;
      for (int j = 1; j <= k; j++) {
        sum[j] = sum[j - 1] + c[way[j]];
        ans = max(ans, sum[j]);
      }
      res = max(res, ans);
    } else {
      for (int j = 1; j <= cnt; j++) {
        sum[j] = sum[j - 1] + c[way[j]];
      }
      if (sum[cnt] >= 0) {
        if (k % cnt) {
          ll s = (k / cnt) * sum[cnt];
          int r = k % cnt;
          for (int j = 1; j <= r; j++)
            res = max(res, s + sum[j]);
        } else {
          ll s = (k / cnt - 1) * sum[cnt];
          for (int j = 1; j <= cnt; j++)
            res = max(res, s + sum[j]);
        }
      } else {
        for (int j = 1; j <= cnt; j++)
          res = max(res, sum[j]);
      }
    }
  }
  cout << res << endl;
  return 0;
}
/*

*/