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

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 100005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

typedef pair<ll, ll> pp;
typedef vector<ll> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const int inf = INT_MAX;

ll power(ll a, ll n, ll mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }
vector<vector<ll>> adj;
vector<bool> vis;

void solve(ll test) {

  long long int N, K, res = -1000000009;
  vector<long long int> P, C, score;
  cin >> N >> K;

  P.resize(N + 1);
  C.resize(N + 1);
  score.resize(N + 1);

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

  for (int i = 1; i < N + 1; i++) {
    int tmpIdx = i, loopL = 0;
    long long int loopS = 0, score = 0;

    do {
      loopL++;
      tmpIdx = P[tmpIdx];
      loopS += C[tmpIdx];
      score += C[tmpIdx];

      if (loopL <= K) {
        res = max(res, score);
      }
    } while (tmpIdx != i);
    // loop部分を使わない場合はこのループで網羅

    // tmpIdx は i
    score = 0;
    if (K >= loopL && loopS > 0) {
      for (int j = 1; j < loopL + 1; j++) {
        tmpIdx = P[tmpIdx];
        score += C[tmpIdx];
        res = max(res, score + loopS * ((K - j) / loopL));
      }
    }
  }

  cout << res;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    cout << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 100005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

typedef pair<ll, ll> pp;
typedef vector<ll> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const int inf = INT_MAX;

ll power(ll a, ll n, ll mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }
vector<vector<ll>> adj;
vector<bool> vis;

void solve(ll test) {

  ll n, k;
  cin >> n >> k;
  ll a[n];
  map<ll, ll> mp;

  fr(i, 0, n, 1) {
    cin >> a[i];
    a[i]--;
  }
  fr(i, 0, n, 1) {
    ll x;
    cin >> x;
    mp[i] = x;
  }

  ll ans = -inf;
  fr(i, 0, n, 1) {
    ll sum = 0;
    ll moves = 0;
    ll index = a[i];
    while (moves < k) {
      sum += mp[index];
      index = a[index];
      ans = max(sum, ans);
      moves++;
    }
  }

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    cout << endl;
  }
  return 0;
}