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

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define pii pair<int, int>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define fio                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()

using namespace std;

ll nxt() {
  ll x;
  cin >> x;
  return x;
}

ll it;

void dfs(int i, int j) {
  it++;
  if (i + 1 < 3000)
    dfs(i + 1, j);
  if (j + 1 < 3000)
    dfs(i, j + 1);
}

int main() {
#ifndef ONLINE_JUDGE
  fio
#endif
      IOS

          ll n = nxt(),
             k = nxt();
  vector<ll> p(n), c(n);
  generate(all(p), nxt);
  generate(all(c), nxt);

  vector<bool> visited(n, 0);
  vector<vector<ll>> groups;
  ll res = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = true;
      vector<ll> g;
      int j = i;
      while (!visited[p[j] - 1]) {
        j = p[j] - 1;
        visited[j] = true;
        g.pb(c[j]);
      }
      g.pb(c[i]);
      groups.pb(g);
    }
  }

  for (vector<ll> g : groups) {
    ll m = g.size();
    if (m > k) {
      for (int i = 0; i < m; i++) {
        ll kk = k;
        int j = i;
        ll tmp = 0;
        while (kk--) {
          tmp += g[j % m];
          j++;
          res = max(res, tmp);
        }
      }
    } else {
      for (int i = 0; i < m; i++) {
        ll kk = m;
        int j = i;
        ll tmp = 0;
        while (kk--) {
          tmp += g[j % m];
          j++;
          res = max(res, tmp);
        }
      }

      ll tot = 0;
      for (int i = 0; i < m; i++) {
        tot += g[i];
        res = max(res, tot);
      }

      if (k / m > 1) {
        ll ttot = max(tot, (k / m - 1) * tot);
        for (int i = 0; i < m; i++) {
          ll kk = m;
          int j = i;
          ll tmp = 0;
          while (kk--) {
            tmp += g[j % m];
            j++;
            res = max(res, tmp);
            res = max(res, ttot + tmp);
          }
        }
      }

      tot = max(tot, (k / m) * tot);
      res = max(res, tot);
      ll rem = k % m;
      for (int i = 0; i < m; i++) {
        ll kk = rem;
        int j = i;
        ll tmp = 0;
        while (kk--) {
          tmp += g[j % m];
          j++;
          res = max(res, tmp);
          res = max(res, tot + tmp);
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define pii pair<int, int>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define fio                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()

using namespace std;

ll nxt() {
  ll x;
  cin >> x;
  return x;
}

ll it;

void dfs(int i, int j) {
  it++;
  if (i + 1 < 3000)
    dfs(i + 1, j);
  if (j + 1 < 3000)
    dfs(i, j + 1);
}

int main() {
#ifndef ONLINE_JUDGE
  fio
#endif
      IOS

          ll n = nxt(),
             k = nxt();
  vector<ll> p(n), c(n);
  generate(all(p), nxt);
  generate(all(c), nxt);

  vector<bool> visited(n, 0);
  vector<vector<ll>> groups;
  ll res = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = true;
      vector<ll> g;
      int j = i;
      while (!visited[p[j] - 1]) {
        j = p[j] - 1;
        visited[j] = true;
        g.pb(c[j]);
      }
      g.pb(c[i]);
      groups.pb(g);
    }
  }

  for (vector<ll> g : groups) {
    ll m = g.size();
    for (int i = 0; i < m; i++) {
      ll tmp = 0;
      ll kk = k;
      int j = i;
      while (kk--) {
        tmp += g[j % m];
        j++;
        res = max(res, tmp);
      }
    }
  }

  cout << res << endl;

  return 0;
}