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

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

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i <= n; i++)
#define FORI(i, n) for (int i = n - 1; i >= 0; i--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, (T)x);
}
template <class T, class U, typename... W>
void init(vector<T> &v, U x, size_t n, W... m) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int T = 1;
  //    cin >> T;
  FOR1(tt, T) {
    int n, k;
    cin >> n >> k;
    vector<int> p(n + 1);
    FOR1(i, n) cin >> p[i];
    vector<int> c(n + 1);
    FOR1(i, n) cin >> c[i];

    ll inf = 1e18;
    ll sol = -inf;
    FOR1(i, n) {
      int u = i;
      ll s = 0;
      int len = 0;
      do {
        u = p[u];
        s += c[u];
        len++;
        if (len <= k)
          umax(sol, s);
      } while (u != i);

      if (k >= 3 * len) {
        int q = (k - 3 * len) / len;
        len += q * len;
        s += q * s;
        umax(sol, s);
      }

      while (len < k) {
        u = p[u];
        s += c[u];
        umax(sol, s);
        len++;
      }
    }
    cout << sol << endl;
  }

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i <= n; i++)
#define FORI(i, n) for (int i = n - 1; i >= 0; i--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, (T)x);
}
template <class T, class U, typename... W>
void init(vector<T> &v, U x, size_t n, W... m) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int T = 1;
  //    cin >> T;
  FOR1(tt, T) {
    int n, k;
    cin >> n >> k;
    vector<int> p(n + 1);
    FOR1(i, n) cin >> p[i];
    vector<int> c(n + 1);
    FOR1(i, n) cin >> c[i];

    ll inf = 1e18;
    ll sol = -inf;
    FOR1(i, n) {
      int u = i;
      ll s = 0;
      int len = 0;
      do {
        u = p[u];
        s += c[u];
        len++;
        if (len <= k)
          umax(sol, s);
      } while (u != i);

      while (len < k) {
        u = p[u];
        s += c[u];
        umax(sol, s);
        len++;
      }

      //            if (len < k){
      //                if (k - len >= len){
      //                    int q = (k-len)/len;
      //                    len += q*len;
      //                    if (s >= 0) {
      //                        s += q * s;
      //                        umax(sol, s);
      //                        while(len < k){
      //                            u = p[u];
      //                            s += c[u];
      //                            umax(sol, s);
      //                            len++;
      //                        }
      //                    }
      //                }
      //                else {
      //
      //                }
      //            }
    }

    cout << sol << endl;
  }

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}