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

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define MAXX 1e18

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

  ll N, K;
  cin >> N >> K;
  ll P[N];
  ll C[N];

  for (ll i = 0; i < N; ++i) {
    cin >> P[i];
    P[i]--;
  }

  for (ll i = 0; i < N; ++i)
    cin >> C[i];

  ll ans = LLONG_MIN;
  for (ll i = 0; i < N; ++i) {
    ll start = P[i];
    vector<ll> cost;

    while (start != i) {
      cost.push_back(C[start]);
      start = P[start];
    }

    cost.push_back(C[i]);

    ll mx_sm = LLONG_MIN;
    ll mx_in = -1;
    ll sum = 0;

    ll M = cost.size();
    for (ll j = 0; j < M; ++j) {
      sum += cost[j];
      if (mx_sm < sum) {
        mx_sm = sum;
        mx_in = j;
      }
    }

    mx_in++;

    if (mx_sm < 0 || K < mx_in || sum < 0) {
      sum = 0;
      ll cur_ans = LLONG_MIN;

      for (ll j = 0; j < min(K, M); ++j) {
        sum += cost[j];
        cur_ans = max(cur_ans, sum);
      }

      ans = max(ans, cur_ans);
    } else {
      ll term = (K - mx_in) / M;
      ans = max(ans, term * sum + mx_sm);

      term = K / M;
      ll rem = K % M;

      ll cur_ans = term * sum;
      sum = 0;
      for (ll j = 0; j < rem; ++j) {
        sum += cost[j];
        ans = max(ans, cur_ans + sum);
      }
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define MAXX 1e18

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

  ll N, K;
  cin >> N >> K;
  ll P[N];
  ll C[N];

  for (ll i = 0; i < N; ++i) {
    cin >> P[i];
    P[i]--;
  }

  for (ll i = 0; i < N; ++i)
    cin >> C[i];

  ll ans = LLONG_MIN;
  for (ll i = 0; i < N; ++i) {
    ll start = P[i];
    vector<ll> cost;

    while (start != i) {
      cost.push_back(C[start]);
      start = P[start];
    }
    cost.push_back(C[i]);

    ll mx_sm = LLONG_MIN;
    ll mx_in = -1;

    ll sum = 0;
    ll M = cost.size();
    for (ll j = 0; j < M; ++j) {
      sum += cost[j];
      if (mx_sm < sum) {
        mx_sm = sum;
        mx_in = j;
      }
    }

    mx_in++;

    if (mx_sm < 0 || K < mx_in) {
      sum = 0;
      ll cur_ans = LLONG_MIN;
      for (ll j = 0; j < K; ++j) {
        sum += cost[j];
        cur_ans = max(cur_ans, sum);
      }

      ans = max(ans, cur_ans);
    } else {
      ll term = (K - mx_in) / M;
      ans = max(ans, term * sum + mx_sm);
    }
  }

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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // d
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (auto &x : p) {
    cin >> x;
    x--;
  }
  vector<long long> c(n);
  for (auto &x : c)
    cin >> x;
  long long ans = -(1LL << 60);
  for (int i = 0; i < n; i++) {
    vector<bool> vis(n);
    vector<int> path;
    int cur = p[i];
    while (!vis[cur]) {
      path.push_back(cur);
      vis[cur] = true;
      cur = p[cur];
    }
    long long score = 0;
    long long kk = k;
    for (int j = 0; j < (int)path.size() && kk > 0; j++, kk--) {
      score += c[path[j]];
      ans = max(ans, score);
    }
    long long cycle = score;
    long long ccc = 1LL * (kk / (int)path.size());
    long long cc = max(0LL, ccc - 3);
    score += cc * cycle;
    kk -= cc * path.size();
    ans = max(ans, score);
    for (int j = 0; kk > 0; j++, kk--) {
      score += c[path[j % (int)path.size()]];
      ans = max(ans, score);
    }
    ans = max(ans, score);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // d
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (auto &x : p) {
    cin >> x;
    x--;
  }
  vector<long long> c(n);
  for (auto &x : c)
    cin >> x;
  long long ans = -(1LL << 60);
  for (int i = 0; i < n; i++) {
    vector<bool> vis(n);
    vector<int> path;
    int cur = p[i];
    while (!vis[cur]) {
      path.push_back(cur);
      vis[cur] = true;
      cur = p[cur];
    }
    for (auto &x : path)
      cerr << x << " ";
    cerr << endl;
    long long score = 0;
    long long kk = k;
    for (int j = 0; j < (int)path.size() && kk > 0; j++, kk--) {
      score += c[path[j]];
      ans = max(ans, score);
    }
    long long cycle = score;
    score += 1LL * (kk / (int)path.size()) * cycle;
    kk %= (int)path.size();
    ans = max(ans, score);
    for (int j = 0; j < (int)path.size() && kk > 0; j++, kk--) {
      score += c[path[j]];
      ans = max(ans, score);
    }
    ans = max(ans, score);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #pragma GCC optimize("trapv")
#define I inline void
#define S struct
#define vi vector<int>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 5e3 + 7, mod = 1e9 + 7;

// How interesting!

ll n, k;

ll ans;

ll a[N], b[N];

vector<bool> vis(N, 0);

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> k;
  ll mx = -1e18;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  for (int i = 0; i < n; i++) {
    cin >> b[i];
    mx = max(mx, b[i]);
  }
  if (mx <= 0)
    return cout << mx, 0;
  ll out = -1e18;
  for (int i = 0; i < n; i++) {
    ll cur = a[i];
    ll cyc = 0;
    for (int j = 0; j < N; j++) {
      vis[j] = 0;
    }
    vector<ll> vec;
    while (!vis[cur]) {
      vec.push_back(b[cur]);
      cyc += b[cur];
      vis[cur] = 1;
      cur = a[cur];
    }
    ans = 0;

    ll buckets = k / (int)vec.size();

    if (cyc > 0) {
      ans = buckets * cyc;
    }

    ll rem = k % (vec.size());

    ll add = 0, best = 0;
    for (int j = 0; j < rem; j++) {
      add += vec[j];
      best = max(best, add);
    }
    ans += best;

    if (buckets) {
      ll add2 = 0, best2 = 0;
      ll rem = k % (vec.size()) + (int)vec.size();
      for (int j = 0; j < rem; j++) {
        add2 += vec[j % (int)vec.size()];
        best2 = max(best2, add2);
      }
      ans = max(ans, max(0ll, (buckets - 1) * cyc) + best2);
    }
    out = max(out, ans);
  }

  cout << out;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #pragma GCC optimize("trapv")
#define I inline void
#define S struct
#define vi vector<int>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 5e3 + 7, mod = 1e9 + 7;

// How interesting!

ll n, k;

ll ans;

ll a[N], b[N];

vector<bool> vis(N, 0);

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> k;
  ll mx = -1e18;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  for (int i = 0; i < n; i++) {
    cin >> b[i];
    mx = max(mx, b[i]);
  }
  if (mx <= 0)
    return cout << mx, 0;
  ll out = -1e18;
  for (int i = 0; i < n; i++) {
    ll cur = a[i];
    ll cyc = 0;
    for (int j = 0; j < N; j++) {
      vis[j] = 0;
    }
    vector<ll> vec;
    while (!vis[cur]) {
      vec.push_back(b[cur]);
      cyc += b[cur];
      vis[cur] = 1;
      cur = a[cur];
    }
    ans = 0;
    if (!vec.size())
      while (1)
        ;
    ll buckets = k / (int)vec.size();

    if (cyc > 0) {
      ans = buckets * cyc;
    }

    ll rem = k % (vec.size());

    ll add = 0, best = 0;
    for (int j = 0; j < rem; j++) {
      add += vec[j];
      best = max(best, add);
    }
    ans += best;

    if (buckets) {
      ll add2 = 0, best2 = 0;
      ll rem = k % (vec.size()) + (int)vec.size();
      for (int j = 0; j < rem; j++) {
        add2 += vec[j];
        best2 = max(best2, add2);
      }
      ans = max(ans, max(0ll, (buckets - 1) * cyc) + best2);
    }
    out = max(out, ans);
  }

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

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<ll> P(N, 0);
  vector<ll> C(N, 0);
  for (ll i = 0; i < N; i++) {
    ll p;
    cin >> p;
    p--;
    P[i] = p;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }
  ll res = -1e18;
  for (ll start = 0; start < N; start++) {
    ll x = start;
    ll total = 0;
    vector<ll> loop;
    while (true) {
      x = P[x];
      total += C[x];
      loop.emplace_back(C[x]);
      if (x == start)
        break;
    }
    ll cur = 0;
    for (
        ll i = 0; i < loop.size();
        i++) // ループのうちどこまで進めるか調べる（進まない方が得することがあるため）
    {
      cur += loop[i];
      if (i + 1 > K)
        break;
      ll cutPoint = cur;
      if (0 < total) {
        ll e =
            (K - (i + 1)) /
            loop.size(); // K回から進んだ回数を引く。ここで、iが0になっているので＋１する。
        cutPoint += total * e;
      }
      res = max(cutPoint, res);
    }
  }
  cout << res << endl;
}

Error: -11

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<ll> P(N, 0);
  vector<ll> C(N, 0);
  for (ll i = 0; i < N; i++) {
    ll p;
    cin >> p;
    P[i] = --p;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }
  ll res = -1e18;
  for (ll start = 0; start < N; start++) {
    ll x = start;
    ll total = 0;
    vector<ll> loop;
    while (true) {
      int x = P[x];
      total += C[x];
      loop.emplace_back(C[x]);
      if (x == start)
        break;
    }
    ll cur = 0;
    for (
        ll i = 0; i < loop.size();
        i++) // ループのうちどこまで進めるか調べる（進まない方が得することがあるため）
    {
      cur += loop[i];
      if (i + 1 > K)
        break;
      ll cutPoint = cur;
      if (0 < total) {
        ll e =
            (K - (i + 1)) /
            loop.size(); // K回から進んだ回数を引く。ここで、iが0になっているので＋１する。
        cutPoint += total * e;
      }
      res = max(cutPoint, res);
    }
  }
  cout << res << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD (long long)(1e9 + 7)
using namespace std;
long long n, k, vis[5005];
long long p[5005], c[5005], sum[5005], ans = -1e18;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {

    long long s = c[i];

    vector<long long> ti;
    ti.push_back(s);

    for (int j = p[i]; j != i; j = p[j]) {
      s += c[j];
      ti.push_back(s);
    }
    int len = ti.size();
    for (int j = 0; j < len; j++) {

      long long rest = k - j - 1;
      if (rest < 0)
        break;
      ans = max(ans, rest / len * max(0LL, s) + ti[j]);
    }
  }

  cout << ans;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define MOD (long long)(1e9 + 7)
using namespace std;
long long n, k, vis[5005];
long long p[5005], c[5005], sum[5005], ans = -1e18;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {

    long long s = c[i];

    vector<long long> ti;
    ti.push_back(s);

    for (int j = p[i]; j != i; j = p[i]) {
      s += c[j];
      ti.push_back(s);
    }
    int len = ti.size();
    for (int j = 0; j < len; j++) {

      long long rest = k - j - 1;
      if (rest < 0)
        break;
      ans = max(ans, rest / len * max(0LL, s) + ti[j]);
    }
  }

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


#include <chrono>
#include <iomanip>
#include <iostream>
using namespace std;

void Main() {
  // input
  int64_t N, K;
  cin >> N >> K;
  int64_t P[N + 1], C[N + 1], D[N + 1], L[N + 1];

  P[0] = 0;
  C[0] = 0;
  D[0] = 0;
  L[0] = 0;
  for (int64_t i = 1; i <= N; i++) {
    cin >> P[i];
  }
  for (int64_t i = 1; i <= N; i++) {
    cin >> C[i];
  }

  // process
  for (int64_t startPos = 1; startPos <= N; startPos++) {
    int64_t pos = startPos;
    int64_t now = 0;
    int64_t length = 0;

    pos = P[pos];
    now += C[pos];
    length++;
    while (pos != startPos) {
      pos = P[pos];
      now += C[pos];
      length++;
    }
    D[startPos] = now;
    L[startPos] = length;
    // cout << startPos << ":" << now << ":" << length << endl;
  }

  int64_t ans = INT64_MIN;
  for (int64_t startPos = 1; startPos <= N; startPos++) {
    int64_t now = 0;
    int64_t pos = startPos;
    for (int64_t count = 1; count <= K && count <= N; count++) {
      pos = P[pos];
      now += C[pos];
      if (D[pos] > 0) {
        ans = max(ans, now + D[pos] * ((K - count) / L[pos]));
      } else {
        ans = max(ans, now);
      }
      // cout << startPos << ":" << ans << endl;
    }
  }

  // output
  cout << ans << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:


#include <chrono>
#include <iomanip>
#include <iostream>
using namespace std;

void Main() {
  // input
  int64_t N, K;
  cin >> N >> K;
  int64_t P[N + 1], C[N + 1];

  P[0] = 0;
  C[0] = 0;
  for (int64_t i = 1; i <= N; i++) {
    cin >> P[i];
  }
  for (int64_t i = 1; i <= N; i++) {
    cin >> C[i];
  }

  // process
  int64_t ans = INT64_MIN;
  for (int64_t startPos = 1; startPos <= N; startPos++) {
    int64_t now = 0;
    int64_t pos = startPos;
    for (int64_t count = 1; count <= K; count++) {
      pos = P[pos];
      now += C[pos];
      ans = max(now, ans);
      // cout << ans << endl;
    }
  }

  // output
  cout << ans << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> p(n), c(n);
  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 = -pow(10, 10);
  for (int i = 0; i < n; i++) {
    vector<long long> v(n);
    long long count = -pow(10, 10);
    long long sum = 0;
    int num = i;
    for (int j = 0; j < k; j++) {
      sum += c[p[num]];
      num = p[num];
      count = max(count, sum);
      v[j] = count;
      int x = count;

      if (num == i) {
        int t = k % (j + 1);
        if (sum >= 0) {
          count = sum * (k / (j + 1));
          if (t != 0) {
            count += v[t - 1];
          }
          int numnum = i;
          long long countcount = -pow(10, 10);
          long long sumsum = 0;
          for (int jj = 0; jj < j + t; jj++) {
            sumsum += c[p[numnum]];
            numnum = p[numnum];
            countcount = max(countcount, sumsum);
          }
          count = max(count, countcount + sum * (k / (j + 1) - 1));
        }
        break;
      }
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> p(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<vector<int>> grp(n);
  vector<vector<long long>> SUM(n);
  for (int i = 0; i < n; i++) {
    int num = i;
    long long sum = 0;
    for (int j = 0; j < n; j++) {
      num = p[num];
      sum += c[num];
      SUM[i].push_back(sum);
      grp[i].push_back(num);
      if (num == i)
        break;
    }
  }

  long long answer = -1000000000000000;
  for (int i = 0; i < n; i++) {
    long long ans = SUM[i][0];
    long long t = k % (grp[i].size());
    for (int j = 0; j < t; j++) {
      ans = max(ans, SUM[i][j]);
    }

    if (SUM[i][grp[i].size() - 1] > 0) {
      ans += SUM[i][grp[i].size() - 1] * (k / grp[i].size());
    } else {
      ans = -10000000000000000;
      long long l = grp[i].size();
      l = max(l, k);
      for (int j = 0; j < l; j++) {
        ans = max(ans, SUM[i][j]);
      }
    }
    answer = max(answer, ans);
  }
  cout << answer << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using s64 = signed long long;
using u64 = unsigned long long;
using f128 = long double;

int main() {
  s64 n, k;
  cin >> n >> k;
  vector<s64> p(n), c(n);
  for (s64 i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (s64 i = 0; i < n; i++) {
    cin >> c[i];
  }

  s64 maxScore = LLONG_MIN;
  for (s64 i = 0; i < n; i++) {
    s64 score = 0;
    s64 next = p[i] - 1;
    for (s64 j = 0; j < k; j++) {
      score += c[next];
      if (score > maxScore) {
        maxScore = score;
      }

      if (next == i) {
        s64 loopScore = score;
        if (loopScore > 0) {
          s64 leftMoveCount = k - j - 1;
          s64 loopLength = j + 1;
          s64 leftLoopCount = leftMoveCount / loopLength;

          // 完全ループ数-1まではスコアを加算できる
          score += loopScore * (leftLoopCount - 1);

          // (1ループ + 残りの移動回数)で最大値になるところ
          leftMoveCount -= (leftLoopCount - 1) * loopLength;
          next = p[i] - 1;
          for (s64 j2 = 0; j2 < leftMoveCount; j2++) {
            score += c[next];
            if (score > maxScore) {
              maxScore = score;
            }
            next = p[next] - 1;
          }
        }

        break;
      } else {
        next = p[next] - 1;
      }
    }
  }

  cout << maxScore << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using s64 = signed long long;
using u64 = unsigned long long;
using f128 = long double;

int main() {
  s64 n, k;
  cin >> n >> k;
  vector<s64> p(n), c(n);
  for (s64 i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (s64 i = 0; i < n; i++) {
    cin >> c[i];
  }

  s64 maxScore = LLONG_MIN;
  for (s64 i = 0; i < n; i++) {
    s64 score = 0;
    s64 next = p[i] - 1;

    for (s64 j = 0; j < k; j++) {
      score += c[next];
      if (score > maxScore) {
        maxScore = score;
      }
      next = p[next] - 1;
    }
  }

  cout << maxScore << endl;

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

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

typedef long long ll;

const int maxn = 2e5 + 10;
int n, k;
int sum[maxn], c[maxn], p[maxn];
bool vis[maxn];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    int t = i;
    vector<int> v;
    ll sum = 0;
    while (true) {
      v.push_back(c[p[t]]), sum += c[p[t]], t = p[t];
      if (t == i)
        break;
    }
    int tn = v.size();
    if (k <= tn || sum <= 0) {
      ll tmp = 0;
      for (int j = 0, z = 1; z <= k && j < tn; z++, j++)
        tmp += v[j], ans = max(tmp, ans);
    } else {
      ll tmp = 0;
      for (int j = 0; j < tn; j++) {
        tmp += v[j];
        ll r = (k - (j + 1)) / tn;
        ans = max(ans, tmp + r * sum);
      }
    }
  }
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

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

typedef long long ll;

const int maxn = 2e5 + 10;

ll sum[maxn], c[maxn], p[maxn];
bool vis[maxn];

vector<int> g[5005];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);

  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);

  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      int nex = p[i];
      while (vis[nex] == 0) {
        g[cnt].push_back(c[nex]);
        vis[nex] = true;
        nex = p[nex];
      }
      cnt++;
    }
  }
  ll ans = -1e18;
  for (int i = 0; i < cnt; i++) {
    sum[0] = 0;
    int m = g[i].size();
    for (int j = 0; j < g[i].size(); j++) {
      sum[j + 1] = g[i][j];
    }

    for (int j = m + 1; j <= 2 * m; j++)
      sum[j] = sum[j - m];
    for (int j = 1; j <= 2 * m; j++)
      sum[j] += sum[j - 1];
    ll res = -1e18;

    for (int j = 0; j <= m; j++) {
      for (int l = 1; l <= k; l++) {
        res = max(res, sum[j + l] - sum[j]);
      }
    }

    int num = k / m;
    int yu = k % m;
    for (int j = 0; j <= m; j++) {
      res = max(res, sum[yu + j] - sum[j] + sum[m] * num);
    }
    ans = max(ans, res);
  }
  printf("%lld\\n", ans);
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  int64_t res = -INF;
  for (int i = 0; i < N; i++) {
    int cur = i, cnt = 0;
    int64_t tot = 0;
    while (true) {
      tot += C[cur];
      cur = P[cur];
      cnt++;
      if (cur == i)
        break;
    }
    int c = 0;
    int64_t t = 0;
    while (true) {
      c++;
      t += C[cur];
      if (c > K)
        break;
      int n = (K - c) / cnt;
      res = max(res, t + max(INT64_C(0), tot) * n);
      cur = P[cur];
      if (cur == i)
        break;
    }
  }
  cout << res << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  int64_t res = -INF;
  for (int i = 0; i < N; i++) {
    int cur = i;
    vector<int64_t> D(N, 0);
    vector<int> E(N, 0);
    for (int k = 0; k < K; k++) {
      int nxt = P[cur];
      if ((i == nxt || D[nxt] != 0) && K - k - 1 < k - E[nxt]) {
        D[nxt] += ((K - k - 1) / (k + 1 - E[nxt])) * (D[nxt] - D[cur]);
        k += ((K - k - 1) / (k + 1 - E[nxt])) * (k + 1 - E[nxt]);
        E[nxt] = k + 1;
        D[nxt] = D[cur] + C[nxt];
      } else {
        E[nxt] = k + 1;
        D[nxt] = D[cur] + C[nxt];
      }
      cur = nxt;
    }
    for (int i = 0; i < N; i++) {
      if (E[i] != 0)
        res = max(res, D[i]);
    }
  }
  cout << res << '\\n';
  return 0;
}