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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P.at(i);
    P.at(i)--;
  }
  vector<ll> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
  }
  ll ret = -INF;
  for (int i = 0; i < N; i++) {
    vector<bool> used(N);
    vector<int> roop;
    int itr = i;
    while (true) {
      itr = P.at(itr);
      if (used.at(itr) == true) {
        break;
      }
      used.at(itr) = true;
      roop.push_back(C.at(itr));
    }
    ll siz = roop.size();
    ll Sum = 0;
    for (int j = 0; j < roop.size(); j++) {
      Sum += roop.at(j);
    }
    ll Sum2 = 0;
    ll res = -INF;
    for (int j = 0; j < roop.size(); j++) {
      if (K <= j) {
        continue;
      }
      Sum2 += roop.at(j);
      ll res2 = 0;
      ll cnt = K - (j + 1);
      if (Sum > 0) {
        res2 += Sum * (cnt / siz);
      }
      res2 += Sum2;
      chmax(res, res2);
    }
    chmax(ret, res);
  }
  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P.at(i);
    P.at(i)--;
  }
  vector<ll> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
  }
  ll ret = -INF;
  for (int i = 0; i < N; i++) {
    vector<bool> used(N);
    vector<int> roop;
    int itr = i;
    while (true) {
      itr = P.at(itr);
      if (used.at(itr) == true) {
        break;
      }
      used.at(itr) = true;
      roop.push_back(C.at(itr));
    }
    ll siz = roop.size();
    ll Sum = 0;
    for (int j = 0; j < roop.size(); j++) {
      Sum += roop.at(j);
    }
    ll res = -INF;
    for (int j = 0; j < roop.size(); j++) {
      if (K <= j) {
        continue;
      }
      ll res2 = 0;
      ll cnt = K - (j + 1);
      if (Sum > 0) {
        res2 += Sum * (cnt / siz);
      }
      for (int k = 0; k <= j; k++) {
        res2 += roop.at(k);
      }
      chmax(res, res2);
    }
    chmax(ret, res);
  }
  cout << ret << endl;
}