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

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
// #define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
typedef long long ll;
const int MAX_N = 1e5 + 5;
const ll inf = 1e18;
inline int qi() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline ll ql() {
  ll f = 0;
  int fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
ll c[MAX_N], p[MAX_N];
bool vis[MAX_N];
ll b[MAX_N], sum[MAX_N];
ll cal(vector<ll> a, ll k) {

  b[0] = sum[0] = 0;
  for (auto x : a)
    b[++b[0]] = c[x];

  repi(i, 1, b[0]) b[b[0] + i] = b[2 * b[0] + i] = b[i];
  repi(i, 1, 3 * b[0]) sum[i] = sum[i - 1] + b[i];
  ll res = 0, tk = k, tmp = -inf;
  if (sum[b[0]] > 0) {
    if ((tk / b[0]) > 2)
      res += sum[b[0]] * ((tk / b[0]) - 1), tk -= b[0] * ((tk / b[0]) - 1);
    repi(i, 1, tk) repi(j, 1, b[0]) tmp = max(tmp, sum[j + i - 1] - sum[j - 1]);
  } else {
    tk = min(tk, b[0] - 1);
    repi(i, 1, tk) repi(j, 1, b[0]) tmp = max(tmp, sum[j + i - 1] - sum[j - 1]);
  }
  return res + tmp;
}
int main() {
  ll n = ql(), k = ql();
  repi(i, 1, n) p[i] = ql();
  repi(i, 1, n) c[i] = ql();
  ll ans = -inf;
  repi(i, 1, n) if (!vis[i]) {
    ll now = i;
    vector<ll> tmp;
    while (p[now] != i)
      tmp.pb(now), vis[now] = true, now = p[now];
    vis[now] = true, tmp.pb(now);
    ans = max(ans, cal(tmp, k));
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pr(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
const int MAX_N = 1e2 + 5;
const ll inf = 1e18;
inline int qi() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline ll ql() {
  ll f = 0;
  int fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
ll c[MAX_N], p[MAX_N];
bool vis[MAX_N];
ll b[MAX_N], sum[MAX_N];
ll cal(vector<ll> a, ll k) {

  b[0] = sum[0] = 0;
  for (auto x : a)
    b[++b[0]] = c[x];

  repi(i, 1, b[0]) b[b[0] + i] = b[i];
  repi(i, 1, 2 * b[0]) sum[i] = sum[i - 1] + b[i];
  ll res = 0;
  if (sum[b[0]] > 0)
    res += sum[b[0]] * (k / b[0]), k %= b[0];
  else if (sum[b[0]] <= 0)
    k = min(k, b[0] - 1);
  ll tmp = -inf;
  repi(i, 1, k) repi(j, 1, b[0]) tmp = max(tmp, sum[j + i - 1] - sum[j - 1]);
  return res + tmp;
}
int main() {
  ll n = ql(), k = ql();
  repi(i, 1, n) p[i] = ql();
  repi(i, 1, n) c[i] = ql();
  ll ans = -inf;
  repi(i, 1, n) if (!vis[i]) {
    ll now = i;
    vector<ll> tmp;
    while (p[now] != i)
      tmp.pb(now), vis[now] = true, now = p[now];
    vis[now] = true, tmp.pb(now);
    ans = max(ans, cal(tmp, k));
  }
  printf("%lld\\n", ans);
}