Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

// copied code
#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

signed main(void) {
  int n, k;
  cin >> n >> k;
  int p[5005], c[5005];
  reps(i, 1, n + 1) cin >> p[i];
  reps(i, 1, n + 1) cin >> c[i];
  int a[5005] = {0}, r[2][3000], rn = 0, rp;
  reps(i, 1, n + 1) {
    if (a[i])
      continue;
    rp = i;
    r[1][rn] = 0;
    while (1) {
      r[1][rn]++;
      a[rp] = rn;
      r[0][rn] += c[rp];
      rp = p[rp];
      if (rp == i)
        break;
    }
    rn++;
  }
  rn--;
  // reps (i, 1, n + 1) cout << a[i];
  // cout << endl;
  int ans = -2000000000;
  reps(i, 1, n + 1) {
    int ans2 = 0, k2 = k;
    int tmp = k / r[1][a[i]] - 1;
    if (r[0][a[i]] > 0) {
      if (tmp > 0) {
        ans2 = r[0][a[i]] * tmp, k2 -= tmp * r[1][a[i]];
        if (ans2 > ans)
          ans = ans2;
      }
    } else {
      k2 = 5005;
    }
    int q = i;
    rep(j, k2) {
      q = p[q];
      ans2 += c[q];
      if (ans2 > ans)
        ans = ans2;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
const lli INF = 1e9 + 100;
const lli INF64 = (1LL << 61) + 100;
const lli MOD = 1e9 + 7;
const lli MH1 = 1599614977, MH2 = 2034417103, MH3 = 1090250123,
          MH4 = 2024491871;
lli bpow(lli baseee, lli exponenttt) {
  if (exponenttt < 0)
    return 0;
  lli res = 1;
  baseee %= MOD;
  while (exponenttt > 0) {
    if ((exponenttt & 1) == 1)
      res = (res * baseee) % MOD;
    baseee = (baseee * baseee) % MOD;
    exponenttt >>= 1;
  }
  return res;
}
lli multInverse(lli a) { // if_mod_is_prime
  return bpow(a, MOD - 2);
}
lli divm(lli a, lli b) { // if_mod_is_prime
  return (a * multInverse(b)) % MOD;
}
lli multm(lli a, lli b) { return (a * b) % MOD; }
lli subm(lli a, lli b) {
  a += MOD;
  lli r = a - b;
  if (r >= MOD)
    return r - MOD;
  return r;
}
lli addm(lli a, lli b) {
  lli r = a + b;
  if (r >= MOD)
    return r - MOD;
  return r;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //	freopen("input.txt", "r", stdin);
  //	freopen("output.txt", "w", stdout);
  lli n, k;
  cin >> n >> k;
  vector<lli> pp(n);
  vector<lli> cost(n);
  for (lli &i : pp) {
    cin >> i;
    i--;
  }
  for (lli &i : cost)
    cin >> i;

  lli ans = -INF64;
  lli fullCyc, kk;
  for (int i = 0; i < n; i++) {
    kk = k;
    fullCyc = 0;

    lli j = pp[i];
    lli sz = 0;
    while (j != i) {
      fullCyc += cost[j];
      j = pp[j];
      sz++;
    }
    fullCyc += cost[i];
    sz++;

    lli cc = 0;
    if (fullCyc >= 0) {
      if (k >= sz)
        cc += fullCyc * (kk / sz) - fullCyc;
      kk %= sz;
      if (k >= sz)
        kk += sz;
    }

    lli maxi;
    if (fullCyc >= 0 && k >= sz)
      maxi = 0;
    else
      maxi = -INF64;

    j = pp[i];
    fullCyc = 0;
    while (kk > 0) {
      fullCyc += cost[j];
      j = pp[j];
      kk--;
      maxi = max(fullCyc, maxi);
    }
    assert(maxi != -INF64);
    ans = max(ans, cc + maxi);
  }
  assert(ans != -INF64);
  cout << ans << "\\n";
  return 0;
}