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

#include <bits/stdc++.h>
using namespace std;

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"

const int maxn = 5e4 + 10;
const int mod = 998244353;
const double pi = acos(-1);

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int lcm(int a, int b) { return a * b / gcd(a, b); }

int qpow(int a, int n) {
  int b = 1;
  while (n) {
    if (n & 1)
      b = a * b % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}

class Prime_Number {
public:
  int prime[maxn], s, v[maxn];
  void prime_init(int n) {
    for (int i = 2; i <= n; i++) {
      if (!v[i]) {
        prime[s++] = i;
        for (int j = 1; j * i <= n; j++)
          v[i * j] = 1;
      }
    }
  }
};

int p[maxn], a[maxn];
int v[maxn];
vector<int> huan[maxn];
int q[maxn];
int js(int x, int k) {
  int K = k;
  int sz = huan[x].size();
  q[0] = a[huan[x][0]];
  for (int i = 1; i < sz; i++)
    q[i] = q[i - 1] + a[huan[x][i]];
  int ans;
  ans = (k / sz) * q[sz - 1];
  int flag = 1;
  if (k < sz)
    flag = 0;
  else if (ans < 0)
    k = min(k, sz), flag = 0;
  else
    k = min(k, sz);
  int sum = -1e18;
  for (int y = 0; y <= k; y++) {
    if (y == 0) {
      if (q[sz - 1] > 0 && K >= sz) {
        sum = (K / sz) * q[sz - 1];
      }
      continue;
    }
    for (int j = 0; j < sz; j++) {
      if (j < y) {
        if (q[sz - 1] > 0)
          sum = max(sum, q[j] + q[sz - 1] - q[sz - (y - j)] +
                             ((K - y) / sz) * q[sz - 1]);
        else
          sum = max(sum, q[j] + q[sz - 1] - q[sz - (y - j)]);
      } else {
        if (q[sz - 1] > 0)
          sum = max(sum, q[j] - q[j - y] + ((K - y) / sz) * q[sz - 1]);
        else
          sum = max(sum, q[j] - q[j - y]);
      }
    }
  }
  return sum;
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    sll(p[i]);
  for (int i = 1; i <= n; i++)
    sll(a[i]);
  int s = 0;
  for (int i = 1; i <= n; i++)
    if (!v[i]) {
      int next = p[i];
      v[i] = 1;
      huan[s].push_back(i);
      while (!v[next]) {
        v[next] = 1;
        huan[s].push_back(next);
        next = p[next];
      }
      s++;
    }
  int ans = -1e18;
  for (int i = 0; i < s; i++)
    ans = max(ans, js(i, k));
  printf("%lld\\n", ans);
}
signed main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"

const int maxn = 5e3 + 10;
const int mod = 998244353;
const double pi = acos(-1);

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int lcm(int a, int b) { return a * b / gcd(a, b); }

int qpow(int a, int n) {
  int b = 1;
  while (n) {
    if (n & 1)
      b = a * b % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}

class Prime_Number {
public:
  int prime[maxn], s, v[maxn];
  void prime_init(int n) {
    for (int i = 2; i <= n; i++) {
      if (!v[i]) {
        prime[s++] = i;
        for (int j = 1; j * i <= n; j++)
          v[i * j] = 1;
      }
    }
  }
};
int p[maxn], a[maxn];
int v[maxn];
vector<int> huan[maxn];
int q[maxn];
int js(int x, int k) {
  int sz = huan[x].size();
  q[0] = a[huan[x][0]];
  for (int i = 1; i < sz; i++)
    q[i] = q[i - 1] + a[huan[x][i]];
  int ans;
  ans = k / sz * q[sz - 1];
  if (ans < 0)
    ans = 0;
  else
    k %= sz;
  int sum = -1e18;
  for (int y = 1; y <= k; y++) {
    for (int j = 0; j < sz; j++) {
      if (j < y) {
        sum = max(sum, q[j] + q[sz - 1] - q[sz - (y - j)]);
      } else {
        sum = max(sum, q[j] - q[j - y]);
      }
    }
  }
  if (sum < 0 && ans)
    sum = 0;
  return ans + sum;
}
void solve() {
  int n, k;
  while (cin >> n >> k) {
    mem(v, 0);
    for (int i = 1; i <= n; i++)
      sll(p[i]);
    for (int i = 1; i <= n; i++)
      sll(a[i]);
    int s = 0;
    for (int i = 1; i <= n; i++)
      if (!v[i]) {
        int next = p[i];
        v[i] = 1;
        huan[s].push_back(i);
        while (!v[next]) {
          v[next] = 1;
          huan[s].push_back(next);
          next = p[next];
        }
        s++;
      }
    int ans = -1e18;
    for (int i = 0; i < s; i++)
      ans = max(ans, js(i, k));
    printf("%lld\\n", ans);
  }
}
signed main() {
  solve();
  return 0;
}