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

#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <cfenv>
#include <cinttypes>
#include <cstdint>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <chrono>
#include <forward_list>
#include <initializer_list>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

ll gcd(ll x, ll y) {
  x = x % y;
  return x == 0 ? y : gcd(y, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int main() {

  ll n, k;
  cin >> n >> k;
  vector<ll> p(n);
  vector<ll> c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n) p[i]--;
  ll ans = -1e18;
  ll total = 0;
  rep(i, n) {
    int t = i;
    int times = 0;
    total = 0;
    vector<int> s;
    rep(j, n) {
      t = p[t];
      s.push_back(c[t]);
      total += c[t];
      times++;
      if (t == i)
        break;
    }
    ll sum = 0;
    rep(j, s.size()) {
      if (j + 1 > k)
        break;
      sum += s[j];
      ll now = sum;
      if (total > 0) {
        int e = (k - j - 1) / times;
        now += total * e;
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <cfenv>
#include <cinttypes>
#include <cstdint>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <chrono>
#include <forward_list>
#include <initializer_list>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

ll gcd(ll x, ll y) {
  x = x % y;
  return x == 0 ? y : gcd(y, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int main() {

  ll n, k;
  vector<ll> p(n);
  vector<ll> c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  vector<ll> f(n, -INF);
  int ans = 0;
  ll tt = 0;
  priority_queue<pair<ll, ll>> loop;
  rep(i, n) {
    int t = p[i];
    int times = 0;
    tt = 0;
    rep(j, n) {
      if (f[t] != -INF)
        continue;
      f[t] = 1;
      times++;
      tt += c[t];
      t = p[t];
    }
    loop.push(make_pair(tt, times));
  }
  auto a = loop.top();
  int b = k / a.second;
  cout << b * a.first << endl;

  return 0;
}