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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
// static const int INF = (1<<23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
constexpr ll INF = TEN(18);
int main() {

  ll n, k;
  cin >> n >> k;
  ll ans = -1 * INF;
  vector<ll> p(n, 0);
  vector<ll> c(n, 0);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }

  rep(i, n) {
    ll ansk = 0;
    ll circl_sum = 0;
    ll circle_len = 0;
    ll v = p[i];

    while (true) {
      circl_sum += c[v];
      circle_len++;
      if (v == i)
        break;
      v = p[v];
    }

    // cout<<circl_sum<<" "<<circle_len<<endl;

    if (circle_len < k && circl_sum > 0) {
      vector<ll> s;
      v = p[i];
      ll path = 0;
      ll ma = -1 * INF;

      while (true) {
        path += c[v];
        ma = max(ma, path);
        s.push_back(path);
        if (v == i)
          break;
        v = p[v];
      }

      ll send = s[s.size() - 1];
      ll score1 = send * ((k / circle_len) - 1);
      score1 += ma;
      ll score2 = send * ((k / circle_len));
      ll r = k % circle_len;
      int l = 0;
      path = -1 * INF;

      if (r != 0) {
        for (int j = 0; j < r; j++) {
          path = max(path, s[j]);
        }
        score2 += max(0ll, path);
      }
      ll score = max(score2, score1);
      ans = max(score, ans);

    } else {

      ll kremain = 0;
      if (circle_len >= k) {
        kremain = k;
      } else if (circl_sum <= 0) {
        kremain = circle_len;
      }
      ll kremain_sum = -1 * INF;
      // cout<<kremain<<"aaaa"<<ansk<<endl;

      ll ck = 0;
      ll vck = i;
      for (int j = 0; j < kremain; j++) {
        vck = p[vck];
        ck += c[vck];
        kremain_sum = max(kremain_sum, ck);
      }

      if (kremain != 0) {
        ansk += kremain_sum;
      }

      //  cout<<ansk<<endl;
      ans = max(ans, ansk);
    }
  }
  if (ans == -1 * INF) {
    cout << 0 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
// static const int INF = (1<<23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
constexpr ll INF = TEN(18);
int main() {

  ll n, k;
  cin >> n >> k;
  ll ans = -1 * INF;
  vector<ll> p(n, 0);
  vector<ll> c(n, 0);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }

  rep(i, n) {
    ll ansk = 0;
    ll circl_sum = 0;
    ll circle_len = 0;
    ll v = p[i];

    while (true) {
      circl_sum += c[v];
      circle_len++;
      if (v == i)
        break;
      v = p[v];
    }

    // cout<<circl_sum<<" "<<circle_len<<endl;

    if (circle_len < k && circl_sum > 0) {
      ll ansk = 0;
      ll a = -1 * INF;
      ll kremain = 0;
      if (circle_len / k <= 2) {
        kremain = k;
      } else {
        ansk += (circle_len / k - 2) * circl_sum;
        kremain = k - (circle_len / k - 2) * circle_len;
      }
      ll path = 0;
      ll v = i;
      for (int j = 0; j < kremain; j++) {
        v = p[v];
        path += c[v];
        a = max(a, path);
      }
      if (kremain != 0) {
        ansk += a;
      }
      ans = max(ansk, ans);

    } else {

      ll kremain = 0;
      if (circle_len >= k) {
        kremain = k;
      } else if (circl_sum <= 0) {
        kremain = circle_len;
      }
      ll kremain_sum = -1 * INF;
      // cout<<kremain<<"aaaa"<<ansk<<endl;

      ll ck = 0;
      ll vck = i;
      for (int j = 0; j < kremain; j++) {
        vck = p[vck];
        ck += c[vck];
        kremain_sum = max(kremain_sum, ck);
      }

      if (kremain != 0) {
        ansk += kremain_sum;
      }

      //  cout<<ansk<<endl;
      ans = max(ans, ansk);
    }
  }
  if (ans == -1 * INF) {
    cout << 0 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}