alexjercan/bugnet huggingface dataset:
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using pii = pair<int, int>;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define debug(...)                                                             \\
  cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

constexpr ll INF = TEN(18);

int main() {
  int N, K;
  cin >> N >> K;
  V<int> P(N), C(N);
  rep(i, N) cin >> P[i], --P[i];
  rep(i, N) cin >> C[i];

  ll ans = -INF;

  rep(i, N) {
    int v = i;
    ll cycle_sum = 0;
    int cycle_cnt = 0;

    while (true) {
      cycle_cnt++;
      cycle_sum += C[v];
      v = P[v];
      if (v == i)
        break;
    }

    ll path = 0;
    int cnt = 0;

    while (true) {
      cnt++;
      path += C[v];

      if (cnt > K)
        break;

      int num = (K - cnt) / cycle_cnt;
      ll score = path + max(0ll, cycle_sum) * num;
      chmax(ans, score);

      v = P[v];
      if (v == i)
        break;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<int> p(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> dp(n + 1);
    int strt = i;
    ll scr = 0;
    ll tmp = k;
    int cycle = 0;
    vector<int> dc(n + 1);
    while (dp[strt] == 0 && tmp > 0) {
      dp[strt] = scr;
      strt = p[strt];
      scr += c[strt];
      ans = max(ans, scr);
      dc[cycle] = scr;
      tmp--;
      cycle++;
    }
    if (tmp != 0) {
      scr += scr * (tmp / cycle);
      scr += dc[tmp % cycle];
    }

    ans = max(ans, scr);
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <ctime>
#include <initializer_list>
#include <iostream>
#include <tuple>
#include <vector>

#define tii tuple<int, int>
#define vi vector<int>
#define vii vector<vi>
#define ll long long
#define vl vector<ll>
#define vll vector<vl>

#define mymod 1000000007
#define mx 99999999
#define mn -99999999
#define sz 101

using namespace std;
ll n, k;

int main() {
  clock_t tstart = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // Start
  cin >> n >> k;
  vl p(n);
  vl c(n);
  for (auto &x : p)
    cin >> x;
  for (auto &x : p)
    x--;
  for (auto &x : c)
    cin >> x;
  ll ans = *max_element(c.begin(), c.end());
  for (ll i = 0; i < n; i++) {
    vl vec;
    vec.push_back(c[i]);
    ll j = p[i];
    ll s = c[i];
    while (j != i) {
      s += c[j];
      vec.push_back(s);
      j = p[j];
    }
    ll len = vec.size();
    for (ll j1 = 0; j1 < len; j1++) {
      ll rest = k - j1 - 1;
      if (rest < 0)
        break;
      ans = max(ans, vec[j1] + (rest / len) * max(s, 0LL));
    }
  }
  cout << ans;
  // cout<<"\\n\\nTotal Time Taken : "<<(double)(-tstart +
  // clock())/CLOCKS_PER_SEC<<"sec\\n\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <ctime>
#include <initializer_list>
#include <iostream>
#include <tuple>
#include <vector>

#define tii tuple<int, int>
#define vi vector<int>
#define vii vector<vi>
#define ll long long int
#define vl vector<ll>
#define vll vector<vl>

#define mymod 1000000007
#define mx 99999999
#define mn -99999999
#define sz 101

using namespace std;

int main() {
  clock_t tstart = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // Start
  int n, k;
  cin >> n >> k;
  vl p(n);
  vl c(n);
  for (auto &x : p)
    cin >> x;
  for (auto &x : p)
    x--;
  for (auto &x : c)
    cin >> x;
  ll ans = *max_element(c.begin(), c.end());
  for (int i = 0; i < n; i++) {
    int vis = 0;
    ll sum = 0;
    int j = i;
    while (vis < k) {
      sum += c[p[j]];
      ans = max(ans, sum);
      vis++;
      j = p[j];
    }
  }
  cout << ans;
  // cout<<"\\n\\nTotal Time Taken : "<<(double)(-tstart +
  // clock())/CLOCKS_PER_SEC<<"sec\\n\\n";
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
  ll n, k;
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    int x = i;
    now++;
    while (!vis[x]) {
      vis[x] = true;
      V[now].push_back(c[x]);
      x = p[x];
    }
  }
  ll ans = -2e18;
  for (int i = 1; i <= now; i++) {
    ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
    int num = 0;
    for (auto it : V[i])
      q[++num] = it;
    for (auto it : V[i])
      q[++num] = it;

    for (int j = 1; j <= min(siz, k1); j++) {
      ll po = 0, mx = -2e18, no = 0;
      for (int l = 1; l <= num; l++) {
        no += q[l];
        if (l - po > j)
          no -= q[++po];
        mx = max(mx, no);
      }
      u[j] = mx;
      ans = max(ans, mx);
    }

    for (auto it : V[i])
      sum += it;
    ll t = (k1 - 1) / siz;
    k1 -= t * siz;
    if (sum > 0)
      sum1 += t * sum;
    for (int j = 1; j <= k1; j++)
      ans = max(ans, sum1 + u[j]);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
  ll n, k;
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    int x = i;
    now++;
    while (!vis[x]) {
      vis[x] = true;
      V[now].push_back(c[x]);
      x = p[x];
    }
  }
  ll ans = -2e18;
  for (int i = 1; i <= now; i++) {
    ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
    int num = 0;
    for (auto it : V[i])
      q[++num] = it;
    for (auto it : V[i])
      q[++num] = it;

    for (int j = 1; j <= k1; j++) {
      ll po = 0, mx = -2e18, no = 0;
      for (int l = 1; l <= num; l++) {
        no += q[l];
        if (l - po > j)
          no -= q[++po];
        mx = max(mx, no);
      }
      u[j] = mx;
      ans = max(ans, mx);
    }

    for (auto it : V[i])
      sum += it;
    ll t = (k1 - 1) / siz;
    k1 -= t * siz;
    if (sum > 0)
      sum1 += t * sum;
    for (int j = 1; j <= k1; j++)
      ans = max(ans, sum1 + u[j]);
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];

  ll res = -1e+15;
  rep(i, n) {
    vector<ll> dp(1, 0);
    ll maxi = -1e+15;
    ll tmp = 0, cnt = 0;
    int idx = i;
    while (cnt < k) {
      cnt++;
      idx = p[idx];
      tmp += c[idx];
      chmax(maxi, tmp);
      dp.push_back(maxi);
      if (i == idx)
        break;
    }
    if (cnt < k && tmp > 0) {
      ll m = k / cnt;
      chmax(res, m * tmp + dp[k - m * cnt]);
      m--;
      chmax(res, m * tmp + maxi);
    } else {
      chmax(res, maxi);
    }
  }
  cout << res << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];

  ll res = -1e+15;
  rep(i, n) {
    vector<ll> dp(n, 0);
    ll maxi = -1e+15;
    ll tmp = 0, cnt = 0;
    int idx = i;
    while (cnt < k) {
      cnt++;
      idx = p[idx];
      tmp += c[idx];
      chmax(maxi, tmp);
      dp[cnt] = maxi;
      if (i == idx)
        break;
    }
    if (cnt < k && tmp > 0) {
      ll m = k / cnt;
      chmax(res, m * tmp + dp[k - m * cnt]);
      m--;
      chmax(res, m * tmp + maxi);
    } else {
      chmax(res, maxi);
    }
  }
  cout << res << "\\n";

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long p[n], c[n];
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  long ans = -INF;
  long flag[n][n];
  rep(i, n) rep(j, n) flag[i][j] = 0;
  long tmp_score[n];
  rep(i, n) tmp_score[i] = 0;
  rep(i, n) {
    long tmp_max = -INF, tmp = 0, now = i, iter = 0;
    while (1) {
      if (flag[i][now] == 1 || iter >= k)
        break;
      flag[i][now] = 1;
      now = p[now];
      tmp += c[now];
      tmp_score[iter] = tmp;
      if (tmp > tmp_max)
        tmp_max = tmp;
      iter++;
    }
    if (tmp_max > ans)
      ans = tmp_max;
    if (tmp > 0 && iter < k) {
      rep(j, iter) {
        long score = tmp_score[j] + tmp * (k / iter - 1);
        if (k % iter >= j + 1)
          score += tmp;
        if (ans < score)
          ans = score;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long p[n], c[n];
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  long maxsum = -INF, ans = -INF;
  long flag[n][n];
  rep(i, n) rep(j, n) flag[i][j] = 0;
  rep(i, n) {
    long tmp_max = -INF, tmp = 0, now = i, iter = 0;
    while (1) {
      if (flag[i][now] == 1 || iter >= k)
        break;
      now = p[now];
      tmp += c[now];
      if (tmp > tmp_max)
        tmp_max = tmp;
      iter++;
    }
    if (tmp_max > ans)
      ans = tmp_max;
    if (iter < k) {
      long score = (k / iter) * tmp + (k % iter) * tmp_max;
      if (score > ans)
        ans = score;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int maxn = 5e3 + 1;
const ll inf = 1e18;
bool vis[maxn];

void solve() {

  ll n, k, ans = -inf;
  cin >> n >> k;
  vector<ll> a(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];

  for (int i = 1; i <= n; i++) {
    memset(vis, false, sizeof(vis));
    vector<ll> p;
    p.push_back(0ll);
    int x = i;
    while (!vis[x]) {
      vis[x] = true;
      p.push_back(c[x]);
      x = a[x];
    }
    ll m = p.size() - 1;
    for (int i = 1; i <= m; i++) {
      p[i] += p[i - 1];
    }
    for (int i = 1; i <= m; i++) {
      ll rem = k - i;
      if (rem < 0)
        break;
      ans = max({ans, p[i] + p[m] * (rem / m), p[i]});
    }
  }
  cout << ans << '\\n';
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
  freopen("outp.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Imposter                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update> // s.order_of_key(val)
                                          // *s.find_by_order(ind)
using namespace std;
#define f(n) for (ll i = 0; i < n; i++)
#define rep(i, a, b) for (i = a; i < b; i++)
#define input(arr, n)                                                          \\
  for (ll i1 = 0; i1 < n; i1++)                                                \\
  cin >> arr[i1]
#define vll vector<ll>
#define vlll vector<vll>
#define all(x) x.begin(), x.end()

inline void solve() {
  ll n, k;
  cin >> n >> k;
  ll p[n + 1], c[n + 1];
  ll q = k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll ultra = -1e18;
  for (int i = 1; i <= n; i++) {
    q = k;
    ll j = p[i];
    ll sum = 0, maxi = -1e18;
    while (q--) {
      sum += c[j];
      maxi = max(sum, maxi);
      j = p[j];
    }
    ultra = max(ultra, maxi);
  }
  cout << ultra << endl;
}
int main() {
  Imposter

      int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];

  ll res = -LINF;
  rep(i, n) {
    ll v = i, c_sum = 0, c_cnt = 0;

    while (true) {
      c_sum += c[v];
      c_cnt += 1;
      v = p[v];
      if (v == i)
        break;
    }
    ll sum = 0, cnt = 0;
    while (true) {
      cnt += 1;
      sum += c[v];
      if (cnt > k)
        break;
      res = max(max(0LL, (k - cnt) / c_cnt * c_sum) + sum, res);
      v = p[v];
      if (v == i)
        break;
    }
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];
  ll res = -LINF;
  rep(i, n) {
    vector<pair<ll, ll>> dp(n);
    rep(i, n) dp[i] = {-LINF, 0};
    priority_queue<tuple<ll, ll, ll>> que;
    que.emplace(c[i], i, 1);
    dp[i] = {c[i], 1};
    while (!que.empty()) {
      tuple<ll, ll, ll> tp = que.top();
      que.pop();
      ll cost = get<0>(tp), index = get<1>(tp), num = get<2>(tp);
      ll nv = p[index];
      if (dp[nv].first < c[nv] + cost && dp[index].second < k) {
        dp[nv] = {c[nv] + cost, dp[index].second + 1};
        que.emplace(dp[nv].first, nv, dp[nv].second);
      }
    }
    rep(i, n) res = max(res, dp[i].first);
  }
  cout << res << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

void _main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  cin >> p >> c;
  auto go = [&](int i) -> ll {
    ll cnt = 0;
    vector<ll> cycle;
    vector<bool> vis(n);
    for (int rep = 0; rep < k; rep++) {
      i = p[i - 1];
      if (vis[i - 1])
        break;
      vis[i - 1] = 1;
      cnt += c[i - 1];
      cycle.emplace_back(cnt);
    }

    ll res = -1e18;

    for (int j = 1; j <= sz(cycle); j++) {
      if (k - j < 0)
        break;
      int cn = (k - j) / sz(cycle);
      res = max(res, cycle[j - 1] + max(0LL, cn * cycle.back()));
    }

    return res;
  };

  ll ans = -1e18;
  for (int i = 0; i < n; i++) {
    ans = max(ans, go(i + 1));
  }

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace ::std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

void _main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  cin >> p >> c;

  auto go = [&](int i) -> ll {
    ll ans = -1e18, cnt = 0;
    int moves = 0;
    while (moves + 1 <= k) {
      i = p[i - 1];
      cnt += c[i - 1];
      ans = max(ans, cnt);
      ++moves;
    }
    return ans;
  };

  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, go(i));
  }

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
ll a[5005], p[5005], vis[5005];
int main() {
  ll n, k, ans = -2e9;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i <= n; i++) {
    ll now = i, sum = 0;
    vector<ll> c;
    c.push_back(0);
    while (vis[now] != i) {
      vis[now] = i;
      sum += a[now];
      c.push_back(sum);
      now = p[now];
    }
    if (sum > 0) {
      ll mx = -2e9, s = c.size() - 1;
      for (ll j = (k < s); j <= k && j < c.size(); j++) {
        mx = max(mx, c[j] + (k - j) / s * sum);
      }
      ans = max(ans, mx);
    } else {
      ll mx = -2e9;
      for (ll j = 1; j <= k && j < c.size(); j++) {
        mx = max(mx, c[j]);
      }
      ans = max(ans, mx);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
ll a[5005], p[5005];
int main() {
  ll n, k, ans = -2e9;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i <= n; i++) {
    ll now = i, sum = 0;
    vector<ll> c;
    map<ll, ll> vis;
    c.push_back(0);
    while (!vis[now]) {
      vis[now] = 1;
      sum += a[now];
      c.push_back(sum);
      now = p[now];
    }
    if (sum > 0) {
      ll mx = -2e9, s = c.size() - 1;
      for (ll j = (k < s); j <= k && j < c.size(); j++) {
        mx = max(mx, c[j] + (k - j) / s * sum);
      }
      ans = max(ans, mx);
    } else {
      ll mx = -2e9;
      for (ll j = 1; j <= k && j < c.size(); j++) {
        mx = max(mx, c[j]);
      }
      ans = max(ans, mx);
    }
  }
  cout << ans << endl;
}