alexjercan/bugnet huggingface dataset:
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <iostream>
int mod = 1e9 + 7;
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k, p[5000], c[5000];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i], p[i]--;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  long long ans = -mod;
  long long mpsco[5000];
  int mpcnt[5000];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mpsco[j] = 0;
      mpcnt[j] = -1;
    }
    int pos = i, cnt = 0;
    long long sco = 0;
    while (true) {
      if (mpcnt[pos] != -1)
        break;
      else {
        mpsco[pos] = sco;
        mpcnt[pos] = cnt;
      }
      pos = p[pos];
      sco += c[pos];
      cnt++;
    }
    int per = cnt - mpcnt[pos];
    long long psc = sco - mpsco[pos];
    long long pans = -mod, tmp = 0;
    int tpos = i;
    for (int j = 0; j < min(k, cnt); j++) {
      tpos = p[tpos];
      tmp += c[tpos];
      pans = max(pans, tmp);
    }
    if (k - (k - mpcnt[pos]) % per - per >= mpcnt[pos]) {
      tmp = (k - (k - mpcnt[pos]) % per - per - mpcnt[pos]) / per * psc +
            mpsco[pos];
      tpos = pos;
      for (int j = k - (k - mpcnt[pos]) % per - per; j < k; j++) {
        tpos = p[tpos];
        tmp += c[tpos];
        pans = max(pans, tmp);
      }
    }
    ans = max(pans, ans);
  }
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
int mod = 1e9 + 7;
using namespace std;
signed main() {
  int n, k, p[5000], c[5000];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i], p[i]--;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  long long ans = -mod;
  for (int i = 0; i < n; i++) {
    map<int, pair<int, int>> mp;
    int pos = i, cnt = 0;
    long long sco = 0;
    while (true) {
      if (mp.find(pos) != mp.end())
        break;
      else
        mp[pos] = make_pair(sco, cnt);
      pos = p[pos];
      sco += c[pos];
      cnt++;
    }
    int per = cnt - mp[pos].second;
    long long psc = sco - mp[pos].first;
    long long pans = -mod, tmp = 0;
    int tpos = i;
    for (int j = 0; j < min(k, cnt); j++) {
      tpos = p[tpos];
      tmp += c[tpos];
      pans = max(pans, tmp);
    }
    if (k - (k - mp[pos].second) % per - per >= mp[pos].second) {
      tmp =
          (k - (k - mp[pos].second) % per - per - mp[pos].second) / per * psc +
          mp[pos].first;
      tpos = pos;
      for (int j = k - (k - mp[pos].second) % per - per; j < k; j++) {
        tpos = p[tpos];
        tmp += c[tpos];
        pans = max(pans, tmp);
      }
    }
    ans = max(pans, ans);
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  REP(i, n) cin >> p[i];
  REP(i, n) p[i]--;
  REP(i, n) cin >> c[i];

  ll maxi = -1e18;
  REP(i, n) {
    ll score = c[i];
    vector<ll> t;
    t.push_back(score);
    for (int j = p[i]; j != i; j = p[j]) {
      score += c[j];
      t.push_back(score);
    }
    int len = t.size();
    REP(j, len) {
      ll rest = k - j - 1;
      if (rest < 0)
        break;
      maxi = max(maxi, t[j] + (rest / len) * max(score, 0LL));
    }
  }

  cout << maxi << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  REP(i, n) cin >> p[i];
  REP(i, n) p[i]--;
  REP(i, n) cin >> c[i];

  ll maxi = -1e15;
  REP(i, n) {
    ll score = 0;
    int pos = p[i];
    REP(j, k) {
      score += c[pos];
      pos = p[pos];
      maxi = max(maxi, score);
    }
  }
  cout << maxi << '\\n';
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
// using graph = vector<vector<edge>>;
using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 5000;
int n, k;
int p[maxn];
int c[maxn];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;

  REP(i, n) cin >> p[i];
  REP(i, n) p[i]--;
  REP(i, n) cin >> c[i];

  int mm = (-1) * inf;
  REP(i, n) {
    vi pmax;
    pmax.push_back(-inf);
    int step = 0;
    int circ = 0;
    int next = i;
    while (true) {
      step++;
      if (step > k)
        break;
      next = p[next];
      circ += c[next];
      pmax.push_back(max(pmax[step - 1], circ));
      if (next == i)
        break;
    }
    if (step > k)
      chmax(mm, pmax[k]);
    else if (next == i) {
      if (circ <= 0)
        chmax(mm, pmax[step]);
      else {
        int rot = k / step;
        int rem = k % step;
        chmax(mm, (rot - 1) * circ + pmax[step]);
        chmax(mm, rot * circ + max(0LL, pmax[rem]));
      }
    }
  }
  cout << mm << "\\n";
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
// using graph = vector<vector<edge>>;
using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 5000;
int n, k;
int p[maxn];
int c[maxn];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;

  REP(i, n) cin >> p[i];
  REP(i, n) p[i]--;
  REP(i, n) cin >> c[i];

  int mmm = (-1) * inf;
  int mm;
  int tmp;
  int next;
  REP(i, n) {
    mm = (-1) * inf;
    tmp = 0;
    next = i;
    REP(j, k) {
      next = p[next];
      tmp += c[next];
      mm = max(mm, tmp);
    }
    mmm = max(mmm, mm);
  }
  cout << mmm << "\\n";
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n, k;
  ll r;
  cin >> n >> k;
  vector<vector<ll>> v(n);
  vector<ll> p(n), c(n), siz(n, 0);
  for (ll &i : p)
    cin >> i, --i;
  for (ll &i : c)
    cin >> i;
  r = c[0];
  for (ll i = 0; i < n; ++i) {
    ll cur = i, sum = 0;
    do {
      ++siz[i];
      v[i].push_back(sum += c[cur]);
      cur = p[cur];
    } while (cur != i);
  }
  for (ll i = 0; i < n; ++i) {
    ll kt = k, s = 0, tail = v[i].back();
    if (k > siz[i] * 2) {
      if (tail > 0) {
        s = tail * (k / siz[i] - 1);
        kt = k % siz[i] + siz[i];
      } else
        kt = siz[i];
    }
    for (ll j = 0; j < kt; ++j)
      r = max(r, s + v[i][j % siz[i]] + tail * (j >= siz[i]));
  }
  cout << r;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n, k;
  ll r;
  cin >> n >> k;
  vector<vector<ll>> v(n);
  vector<ll> p(n), c(n), siz(n, 0);
  for (ll &i : p)
    cin >> i, --i;
  for (ll &i : c)
    cin >> i;
  r = c[0];
  for (ll i = 0; i < n; ++i) {
    ll cur = i, sum = 0;
    do {
      ++siz[i];
      v[i].push_back(sum += c[cur]);
      cur = p[cur];
    } while (cur != i);
  }
  for (ll i = 0; i < n; ++i) {
    ll kt = k, s = 0, tail = v[i].back();
    if (kt > siz[i] * 2 && tail > 0) {
      s = tail * (k / siz[i] - 1);
      kt = k % siz[i] + siz[i];
    }
    for (ll j = 0; j < kt; ++j)
      r = max(r, s + v[i][j % siz[i]] + tail * (j >= siz[i]));
  }
  cout << r;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// #pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")

#include <bits/stdc++.h>
using namespace std;
#define rep(i, beg, end) for (int i = beg; i < end; i++)
#define rrep(i, beg, end) for (int i = beg; i >= end; i--)
typedef long long ll;
typedef pair<ll, ll> P;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const int N = 6000;
ull p_hash(ull x, ull y) { return (x + y) * (x + y + 1) / 2 + y; } // NxN->N

ll p[N];
ll c[N];
ll n, k;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  rep(i, 1, n + 1) { cin >> p[i]; }

  rep(i, 1, n + 1) { cin >> c[i]; }

  ll ans = -1e18;
  rep(i, 1, n + 1) {
    int x = i;
    ll tot = 0;
    vector<int> sc;
    while (true) {
      x = p[x];
      tot += c[x];
      sc.push_back(c[x]);
      if (x == i)
        break;
    }

    int l = sc.size();
    ll t = 0;
    rep(j, 0, l) {
      t += sc[j];
      if (j + 1 > k)
        break;
      ll now = t;
      if (tot > 0) {
        ll e = (k - j - 1) / l;
        now += tot * e;
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// #pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")

#include <bits/stdc++.h>
using namespace std;
#define rep(i, beg, end) for (int i = beg; i < end; i++)
#define rrep(i, beg, end) for (int i = beg; i >= end; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const int N = 6000;
ull p_hash(ull x, ull y) { return (x + y) * (x + y + 1) / 2 + y; } // NxN->N

ll p[N];
ll c[N];
ll n, k;

ll dfs(ll s, ll m) {
  if (s > k + 1)
    return 0;

  ll temp = 0;
  if (s == 0) {
    temp = -INFINITY;
    rep(i, 1, n + 1) {
      ll sc = dfs(s + 1, i);

      temp = max(temp, sc);
    }
  } else {
    ll next_m = p[m];
    ll sc = dfs(s + 1, next_m);
    if (s != 1)
      temp = max(c[m], c[m] + sc);
    else
      temp = sc;
  }

  return temp;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  rep(i, 1, n + 1) { cin >> p[i]; }

  rep(i, 1, n + 1) { cin >> c[i]; }

  cout << dfs(0, 0) << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N), C(N);
  for (auto &x : P)
    cin >> x, x--;
  for (auto &x : C)
    cin >> x;
  ll res = -1e18;
  for (int i = 0; i < N; i++) {
    vector<ll> sum;
    ll t = 0;
    int idx = i;
    do {
      t += C[idx];
      sum.push_back(t);
      idx = P[idx];
    } while (idx - i);
    for (int j = 0; j < min<int>(K, sum.size()); j++)
      res = max(res, sum[j]);
    ll sz = sum.size();
    for (int j = 0; j < sz; j++) {
      if (j < K % sz) {
        res = max(res, K / sz * sum.back() + sum[j]);
      } else {
        int cnt = K / sz - 1;
        if (cnt < 0)
          break;
        res = max(res, cnt * sum.back() + sum[j]);
      }
    }
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N), C(N);
  for (auto &x : P)
    cin >> x, x--;
  for (auto &x : C)
    cin >> x;
  ll res = -1e18;
  for (int i = 0; i < N; i++) {
    vector<ll> sum;
    ll t = 0;
    int idx = i;
    do {
      t += C[idx];
      sum.push_back(t);
      idx = P[idx];
    } while (idx - i);
    for (int j = 0; j < min<int>(K, sum.size()); j++)
      res = max(res, sum[j]);
    ll s = sum.back() * (K / sum.size());
    ll k = K % sum.size();
    for (int j = 0; j < k, sum.size(); j++) {
      res = max(res, s + sum[j]);
    }
  }
  cout << res << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using G = vector<vector<int>>;

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

  vector<vector<ll>> dp(n, vector<ll>(n + 1));
  vector<ll> anss(n, -2000000000);
  rep(i, n) {
    int move = p[i];
    int maxj;
    bool flag = false;
    int cycle = 0;
    for (int j = 1; j <= n; j++) {
      if (j > k)
        break;
      dp[i][j] = dp[i][j - 1] + c[move];
      // else dp[i][j] = max(dp[i][j-1], dp[i][j-1]+c[move]);
      move = p[move];
      if (dp[i][j] > anss[i]) {
        anss[i] = dp[i][j];
        maxj = j;
      }
      // anss[i] = max(anss[i], dp[i][j]);
      cycle++;
      if (flag) {
        if (dp[i][j] <= 0)
          break;
        else {
          int ind = k / cycle;
          // ind--;
          for (int l = (ind - 1) * cycle + 1; l <= ind * cycle; l++) {
            anss[i] = max(anss[i],
                          dp[i][j] * (ind - 1) + dp[i][l - (ind - 1) * cycle]);
          }
          for (int l = (ind)*cycle + 1; l <= k; l++) {
            anss[i] = max(anss[i], dp[i][j] * (ind) + dp[i][l - (ind)*cycle]);
          }

          break;
        }
      }
      if (move == i)
        flag = true;
    }
  }
  ll ans = *max_element(anss.begin(), anss.end());

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using G = vector<vector<int>>;

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

  vector<vector<ll>> dp(n, vector<ll>(k + 1));
  // vector<ll> anss(n, -2000000000);
  rep(i, n) {
    int move = p[i];
    for (int j = 1; j <= k; j++) {
      dp[i][j] = dp[i][j - 1] + c[move];
      // else dp[i][j] = max(dp[i][j-1], dp[i][j-1]+c[move]);
      move = p[move];
      // anss[i] = max(anss[i], dp[i][j]);
    }
  }
  // ll ans = *max_element(anss.begin(), anss.end());
  ll ans = -1001000101;
  rep(i, n) {
    ll l = *max_element(dp[i].begin(), dp[i].end());
    ans = max(ans, l);
  }
  cout << ans << endl;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
ll S[10005][5005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int p[n], c[n];
  rep(i, n) {
    scanf("%d", p + i);
    p[i]--;
  }
  rep(i, n) scanf("%d", c + i);
  rep(i, 10003) rep(j, n) S[i + 1][p[j]] = S[i][j] + c[p[j]];
  ll ans = -INF;
  rep(i, n) ans = max(ans, S[1][i]);
  vector<bool> seen(n, 0);
  rep(i, n) {
    if (seen[i])
      continue;
    else {
      seen[i] = 1;
      set<int> s;
      s.insert(i);
      int x = i;
      while (true) {
        x = p[x];
        seen[x] = 1;
        s.insert(x);
        if (x == i)
          break;
      }
      if (k <= s.size()) {
        repl(j, 1, k + 1) for (auto t : s) ans = max(ans, S[j][t]);
      } else if (S[s.size()][i] > 0) {
        ll c = (k / s.size() - 1) * S[s.size()][i];
        rep(j, k % s.size() + s.size() + 1) for (auto t : s) ans =
            max(ans, c + S[j][t]);
      } else {
        repl(j, 1, s.size() + 1) for (auto t : s) ans = max(ans, S[j][t]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
ll S[5005][5005];
int main() {
  ll n, k;
  scanf("%lld%lld", &n, &k);
  ll p[n], c[n];
  rep(i, n) {
    scanf("%lld", p + i);
    p[i]--;
  }
  rep(i, n) scanf("%lld", c + i);
  rep(i, 5003) rep(j, n) S[i + 1][p[j]] = S[i][j] + c[p[j]];
  ll ans = -INF;
  vector<bool> seen(n, 0);
  rep(i, n) {
    if (seen[i])
      continue;
    else {
      seen[i] = 1;
      set<int> s;
      s.insert(i);
      int now = i;
      while (true) {
        now = p[now];
        seen[now] = 1;
        s.insert(now);
        if (now == i)
          break;
      }
      if (k <= s.size()) {
        for (auto t : s)
          repl(j, 1, k + 1) ans = max(ans, S[j][t]);
      } else if (S[s.size()][i] > 0) {
        ll c = k / s.size() * S[s.size()][i];
        for (auto t : s)
          rep(j, k % s.size() + 1) ans = max(ans, c + S[j][t]);
      } else {
        assert(i < 0);
        for (auto t : s)
          repl(j, 1, s.size() + 1) ans = max(ans, S[j][t]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans = -1e18;
int n, k, a[10005], c[10005], tmp[10005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int s = 1; s <= n; s++) {
    ll tk = k;
    tmp[0] = 0;
    ll sum = 0;
    tmp[++tmp[0]] = c[a[s]], sum += c[a[s]];
    int now = a[s];
    while (now != s)
      tmp[++tmp[0]] = c[a[now]], sum += c[a[now]], now = a[now];
    if (sum > 0) {
      ll o = sum;
      sum *= k / tmp[0], k %= tmp[0];
      ll tmps = -1e18, tp = 0;
      if (k == 0)
        tmps = 0;
      for (int i = 1; i <= k; i++)
        tmps = max(tmps, tp += tmp[i]);
      ans = max(ans, sum + tmps);
      k = tk;
      if (k < tmp[0])
        continue;
      sum = o * (k / tmp[0] - 1);
      tmps = -1e18, tp = 0;
      for (int i = 1; i <= tmp[0]; i++)
        tmps = max(tmps, tp += tmp[i]);
      ans = max(ans, sum + tmps);
      continue;
    } else
      sum = 0;
    ll tmps = -1e18, tp = 0;
    if (k > tmp[0])
      k = tmp[0];
    if (k == 0)
      tmps = 0;
    for (int i = 1; i <= k; i++)
      tmps = max(tmps, tp += tmp[i]);
    ans = max(ans, sum + tmps);
    k = tk;
  }
  cout << ans;
}
/*
8 10
6 7 4 5 3 8 2 1
1 2 2 2 -3 -1 -4 -4
*/

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans = -1e18;
int n, k, a[10005], c[10005], tmp[10005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int s = 1; s <= n; s++) {
    ll tk = k;
    tmp[0] = 0;
    ll sum = 0;
    tmp[++tmp[0]] = c[a[s]], sum += c[a[s]];
    int now = a[s];
    while (now != s)
      tmp[++tmp[0]] = c[a[now]], sum += c[a[now]], now = a[now];
    if (sum > 0) {
      ll o = sum;
      sum *= k / tmp[0], k %= tmp[0];
      ll tmps = -1e18, tp = 0;
      if (k == 0)
        tmps = 0;
      for (int i = 1; i <= k; i++)
        tmps = max(tmps, tp += tmp[i]);
      ans = max(ans, sum + tmps);
      k = tk;
      if (k < tmp[0])
        continue;
      sum = o * (k / tmp[0] - 1);
      tmps = -1e18, tp = 0;
      for (int i = 1; i <= tmp[0]; i++)
        tmps = max(tmps, tp += tmp[i]);
      ans = max(ans, sum + tmps);
    } else
      sum = 0;
    ll tmps = -1e18, tp = 0;
    if (k == 0)
      tmps = 0;
    for (int i = 1; i <= k; i++)
      tmps = max(tmps, tp += tmp[i]);
    ans = max(ans, sum + tmps);
    k = tk;
  }
  cout << ans;
}
/*
8 10
6 7 4 5 3 8 2 1
1 2 2 2 -3 -1 -4 -4
*/
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 all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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];
  const ll INF = -1 * (1LL << 60);
  ll mx = INF;
  vector<pair<ll, ll>> score(
      n); // 各点の１周したときに得られるスコア、一周の長さ
  rep(i, n) {
    ll ns = c[i];
    int now = i;
    int syuki = 1;
    while (p[now] != i) {
      now = p[now];
      ns += c[now];
      syuki++;
    }
    score[i] = {ns, syuki};
    // cout << score[i].first << " " << score[i].second << endl;
  }
  rep(i, n) {
    ll now = 0;
    ll lef_mx = INF;
    ll sum = 0;
    int np = i;
    if (score[i].second > 1 && score[i].first > 0) {
      ll nk = k % score[i].second + score[i].second;
      now = (k / score[i].second - 1) * score[i].first;
      rep(j, nk) {
        np = p[np];
        sum += c[np];
        lef_mx = max(lef_mx, sum);
      }
    } else {
      rep(j, min(k, score[i].second)) {
        np = p[np];
        sum += c[np];
        lef_mx = max(lef_mx, sum);
      }
    }

    now += lef_mx;
    mx = max(mx, now);
  }
  cout << mx << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int 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];
  const ll INF = -1 * 1LL << 60;
  ll mx = INF;
  vector<pair<ll, int>> score(
      n); // 各点の１周したときに得られるスコア、一周の長さ
  rep(i, n) {
    ll ns = 0;
    int now = i;
    int syuki = 0;
    while (syuki > 0 && p[now] != i) {
      now = p[now];
      ns += c[now];
      syuki++;
    }
    score[i] = {ns, syuki};
  }
  rep(i, n) {
    ll now = 0;
    ll nk = k;
    if (score[i].first > 0) {
      if (score[i].second > nk) {

      } else {
        now += nk / score[i].second * score[i].first;
        nk %= k;
      }
    }
    ll lef_mx = INF;
    ll sum = 0;
    int np = i;
    for (int lk = nk; lk > 0; lk--) {
      np = p[np];
      sum += c[np];
      lef_mx = max(lef_mx, sum);
    }
    now += lef_mx;
    mx = max(mx, now);
  }
  cout << mx << endl;
  return 0;
}