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

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

using ll = long long;
using pii = pair<int, int>;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define debug(...)                                                             \\
  cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

constexpr ll INF = TEN(18);

int main() {
  int N, K;
  cin >> N >> K;
  V<int> P(N), C(N);
  rep(i, N) cin >> P[i], --P[i];
  rep(i, N) cin >> C[i];

  ll ans = -INF;

  rep(i, N) {
    int v = i;
    ll cycle_sum = 0;
    int cycle_cnt = 0;

    while (true) {
      cycle_cnt++;
      cycle_sum += C[v];
      v = P[v];
      if (v == i)
        break;
    }

    ll path = 0;
    int cnt = 0;

    while (true) {
      cnt++;
      path += C[v];

      if (cnt > K)
        break;

      int num = (K - cnt) / cycle_cnt;
      ll score = path + max(0ll, cycle_sum) * num;
      chmax(ans, score);

      v = P[v];
      if (v == i)
        break;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
constexpr ll INF = TEN(18);

template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N, K;
  cin >> N >> K;
  int P[5010], C[5010];

  for (int i = 0; i < N; i++) {
    cin >> P[i];
    cin >> C[i];
    P[i]--;
  }

  ll ans = -INF;

  rep(i, N) {
    int v = i;
    ll cycle_cnt = 0;
    ll cycle_sum = 0;

    while (true) {
      cycle_cnt++;
      cycle_sum += C[v];
      v = P[i];
      if (v == i)
        break;
    }

    ll path = 0;
    ll cnt = 0;

    while (true) {

      cnt++;
      path += C[v];

      if (cnt > K)
        break;

      int num = (K - cnt) / cycle_cnt;
      ll score = path + max(0ll, cycle_sum) * num;
      chmax(ans, score);

      v = P[i];
      if (v == i)
        break;
    }
  }
  cout << ans << endl;
}