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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> p(n);
  for (ll i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<ll> c(n);
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
  }

  ll ans = -inf;
  for (ll i = 0; i < n; i++) {
    vector<bool> used(n, false);
    ll pointer = i;
    while (!used[pointer]) {
      used[pointer] = true;
      pointer = p[pointer];
    }
    ll t = 0;
    ll loopscore = 0;
    rep(i, n) {
      if (used[i]) {
        t++;
        loopscore += c[i];
      }
    }

    ll tk = k;
    ll tmp = 0;
    if (tk / t >= 2) {
      ll g = tk / t - 1;
      tk -= g * t;
      if (loopscore > 0)
        tmp += loopscore * g;
    }
    tmp += c[i];
    ll mx = tmp;
    pointer = i;
    for (int j = 0; j < tk; j++) {
      mx = max(mx, tmp);
      pointer = p[pointer];
      tmp += c[pointer];
    }

    ans = max(ans, mx);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<ll> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }

  ll ans = -inf;
  for (int i = 0; i < n; i++) {
    ll tmp = c[p[i]];
    ll mx = tmp;
    ll x = i;
    for (int j = 0; j < k; j++) {
      mx = max(mx, tmp);
      x = p[x];
      tmp += c[p[x]];
    }
    ans = max(ans, mx);
  }
  cout << ans << endl;

  return 0;
}