Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64

#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };

void solve() {
  int N, K;
  cin >> N >> K;
  vector<int> P(N), C(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }
  REP(i, N) cin >> C[i];
  ll ans = -LINF;
  vector<int> cnt(N), sum(N);
  REP(i, N) {
    int pos = i;
    int res = 0;
    REP(_, N) {
      pos = P[pos];
      res += C[pos];
      cnt[i]++;
      if (pos == i)
        break;
    }
    sum[i] = res;
  }
  REP(i, N) {
    ll res = 0;
    if (sum[i] > 0 && K > cnt[i] * 2) {
      ll c = K / cnt[i] - 1;
      res += c * sum[i];
      int rem = K - c * cnt[i];
      if (c > 0)
        chmax(ans, res);
      int pos = i;
      REP(_, rem) {
        pos = P[pos];
        res += C[pos];
        chmax(ans, res);
      }
    } else {
      int pos = i;
      REP(_, K) {
        pos = P[pos];
        res += C[pos];
        chmax(ans, res);
      }
    }
  }
  print(ans);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q;
  // cin >> q;
  // while (q--)
  solve();
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64

#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };

void solve() {
  int N, K;
  cin >> N >> K;
  vector<int> P(N), C(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }
  REP(i, N) cin >> C[i];
  ll ans = -LINF;
  vector<int> cnt(N), sum(N);
  REP(i, N) {
    int pos = i;
    int res = 0;
    REP(_, N) {
      pos = P[pos];
      res += C[pos];
      cnt[i]++;
      if (pos == i)
        break;
    }
    sum[i] = res;
  }
  REP(i, N) {
    ll res = 0;
    if (K > cnt[i] * 2) {
      if (sum[i] > 0 && K > cnt[i] * 2) {
        ll c = K / cnt[i] - 1;
        res += c * sum[i];
        int rem = K - c * cnt[i];
        if (c > 0)
          chmax(ans, res);
        int pos = i;
        REP(_, rem) {
          pos = P[pos];
          res += C[pos];
          chmax(ans, res);
        }
      } else {
        int pos = i;
        REP(_, min(N, K)) {
          pos = P[pos];
          res += C[pos];
          chmax(ans, res);
        }
      }
    } else {
      int pos = i;
      REP(_, K) {
        pos = P[pos];
        res += C[pos];
        chmax(ans, res);
      }
    }
  }
  print(ans);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int q;
  // cin >> q;
  // while (q--)
  solve();
}