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

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

#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, x, y)                                                           \\
  for (decltype(y) i = (x) - ((x) > (y)); i != (y) - ((x) > (y));              \\
       i += 1 - 2 * ((x) > (y)))
#define trav(i, a) for (auto &(i) : (a))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define clr(a, x) memset((a), (x), sizeof(a)) // array (0,-1)
#define dbg(x)                                                                 \\
  cerr << "STDERR => " << __LINE__ << ": " << #x << " = " << (x) << "  :(\\n";
#define pb push_back
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline void cpuTime() {
  cerr << "cpu time : " << double(clock()) / CLOCKS_PER_SEC << "\\n";
  return;
}
using ll = long long;
using ull = unsigned long long;
using F = float;
using D = double;
using vi = vector<int>;
using pii = pair<int, int>;

// calculate cycle length
vi cycle(int n, vi &p) {

  vector<bool> isVisited(n, false);
  vi clen(n, 0); // store cycle length

  rep(i, 0, n) {
    if (!isVisited[i]) {
      int in = i, cnt = 0;
      stack<int> q;
      while (!isVisited[in]) {
        ++cnt;
        q.push(in);
        isVisited[in] = true;
        in = p[in];
      }

      while (!q.empty()) {
        clen[q.top()] = cnt;
        q.pop();
      }
    }
  }

  return clen;
}

void solve() {

  int n = 0, k = 0;
  cin >> n >> k;

  vi p(n, 0), c(n, 0);
  rep(i, 0, n) {
    int tmp = 0;
    cin >> tmp;
    p[i] = tmp - 1;
  }
  rep(i, 0, n) cin >> c[i];

  vi clen = cycle(n, p);
  ll ans = LLONG_MIN;
  rep(i, 0, n) {
    int l = clen[i], in = i;
    ll cost = 0;
    int m = min(l, k);
    rep(j, 0, m) {
      cost += c[in];
      ans = max(ans, cost);
      in = p[in];
    }

    if (k > l) {
      cost = (k / l - 1) * cost;
      ans = max(ans, cost);

      rep(j, 0, l) {
        cost += c[in];
        ans = max(ans, cost);
        in = p[in];
      }

      rep(j, 0, k % l) {
        cost += c[in];
        ans = max(ans, cost);
        in = p[in];
      }
    }
  }

  cout << ans << "\\n";
  return;
}

/*
 read the question carefully ( what're the exact constraints? )
 look out for SPECIAL CASES (n=1?) and OVERFLOW (ll vs int?) ARRAY OUT OF BOUNDS
 Check datatypes
*/
int main() {

  fast_io;
  int t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }

  cpuTime();
  return 0;
}


Error: TLE

Fixed submission:

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

#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, x, y)                                                           \\
  for (decltype(y) i = (x) - ((x) > (y)); i != (y) - ((x) > (y));              \\
       i += 1 - 2 * ((x) > (y)))
#define trav(i, a) for (auto &(i) : (a))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define clr(a, x) memset((a), (x), sizeof(a)) // array (0,-1)
#define dbg(x)                                                                 \\
  cerr << "STDERR => " << __LINE__ << ": " << #x << " = " << (x) << "  :(\\n";
#define pb push_back
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline void cpuTime() {
  cerr << "cpu time : " << double(clock()) / CLOCKS_PER_SEC << "\\n";
  return;
}
using ll = long long;
using ull = unsigned long long;
using F = float;
using D = double;
using vi = vector<int>;
using pii = pair<int, int>;

void solve() {

  int n = 0, k = 0;
  cin >> n >> k;

  vi p(n, 0), c(n, 0);
  int tmp = 0;
  rep(i, 0, n) {
    cin >> tmp;
    p[i] = tmp - 1;
  }
  rep(i, 0, n) cin >> c[i];

  ll ans = LLONG_MIN;
  rep(i, 0, n) {
    int in = i;
    ll C = 0;
    rep(j, 0, k) {
      in = p[in];
      C += c[in];
      if (ans < C)
        ans = C;
    }
  }

  cout << ans << "\\n";

  return;
}

/*
 read the question carefully ( what're the exact constraints? )
 look out for SPECIAL CASES (n=1?) and OVERFLOW (ll vs int?) ARRAY OUT OF BOUNDS
 Check datatypes
*/
int main() {

  fast_io;
  int t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }

  cpuTime();
  return 0;
}