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

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef std::vector<int> VI;
typedef std::pair<int, int> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

const int maxn = 5005;
int n, k, p[maxn], c[maxn];
LL ans = 1e-15;

int main() {
  n = read(), k = read();
  REP(i, 1, n) p[i] = read();
  REP(i, 1, n) c[i] = read();
  REP(i, 1, n) {
    int cur = i, be = i;
    VI s;
    s.pb(cur);
    LL y = c[cur];
    cur = p[cur];
    while (cur != be) {
      s.pb(cur);
      y += c[cur];
      cur = p[cur];
    }
    LL t = 0;
    for (int i = 0; i < s.size(); i++) {
      t += c[s[i]];
      if (i >= k)
        break;
      LL temp = t;
      if (y > 0)
        temp += (k - i - 1) / s.size() * y;
      ans = max(ans, temp);
    }
  }
  if (ans == 0)
    cout << *max_element(c + 1, c + n + 1);
  else
    cout << ans;
  // cout<<ans;

  return 0;
}


Error: -11

Fixed submission:

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef std::vector<int> VI;
typedef std::pair<int, int> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

const int maxn = 5005;
int n, k, p[maxn], c[maxn], vis[maxn];
LL ans = -1e10, maxx[maxn];

void solve(VI a) {
  LL sum = 0;
  for (int i : a)
    sum += i;
  vector<LL> s(a.size() * 3);
  for (int i = 1; i <= a.size() * 2; i++) {
    int x = a[(i - 1) % a.size()];
    s[i] = c[x];
  }
  s[0] = 0;
  for (int i = 1; i <= a.size() * 2; i++)
    s[i] += s[i - 1];

  REP(i, 1, a.size()) maxx[i] = 1e-15;
  for (int len = 1; len <= a.size(); len++)
    for (int i = 1; i + len - 1 <= a.size() * 2; i++)
      maxx[len] = max(maxx[len], s[i] - s[i - len]);
  if (maxx[a.size()] <= 0) {
    ans = max(ans, *max_element(maxx + 1, maxx + a.size() + 1));
  } else {
    int t = k / a.size();
    if (t > 0 && t * a.size() != k)
      t--;
    LL temp = maxx[a.size()] * t;
    LL x = *max_element(maxx + 1, maxx + k - t * a.size() - a.size() + 1) +
           maxx[a.size()];
    LL y = *max_element(maxx + 1, maxx + a.size() + 1);
    ans = max(ans, temp + max(x, y));
  }
}

int main() {
  n = read(), k = read();
  REP(i, 1, n) p[i] = read();
  REP(i, 1, n) c[i] = read();
  REP(i, 1, n) if (!vis[i]) {
    VI s;
    int t = i;
    while (!vis[t]) {
      vis[t] = 1;
      s.pb(t);
      t = p[t];
    }
    solve(s);
  }
  if (ans == 0)
    cout << *max_element(c + 1, c + n + 1);
  else
    cout << ans;

  return 0;
}