alexjercan/bugnet huggingface dataset:
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 lint = long long;

const long long INF = 1LL << 61;

vector<vector<int>> cycles(const vector<int> &f) {
  int n = f.size();
  vector<vector<int>> res;
  vector<int> color(n, -1);
  rep(u, n) if (color[u] == -1) {
    int v = u;
    do {
      color[v] = u;
      v = f[v];
    } while (color[v] == -1);
    if (color[v] == u) {
      vector<int> C;
      int w = v;
      do {
        C.emplace_back(v);
        v = f[v];
      } while (v != w);
      res.emplace_back(C);
    }
  }
  return res;
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> p(n), w(n);
  rep(u, n) scanf("%d", &p[u]), p[u]--;
  rep(u, n) scanf("%d", &w[u]);

  auto C = cycles(p);
  vector<int> id(n);
  rep(i, C.size()) for (int u : C[i]) id[u] = i;

  vector<lint> wsum(C.size());
  rep(i, C.size()) for (int u : C[i]) wsum[i] += w[u];

  lint ans = -INF;
  rep(u0, n) {
    int k_tmp = k;

    lint res = 0;
    int len = C[id[u0]].size();
    int m = max(k / len - 1, 0);
    k -= m * len;
    if (wsum[id[u0]] > 0) {
      res = m * wsum[id[u0]];
      ans = max(ans, res);
    }
    for (int u = u0; k > 0; k--) {
      u = p[u];
      res += w[u];
      ans = max(ans, res);
    }

    k = k_tmp;
  }
  printf("%lld\\n", ans);

  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 lint = long long;

const long long INF = 1LL << 61;

vector<vector<int>> cycles(const vector<int> &f) {
  int n = f.size();
  vector<vector<int>> res;
  vector<int> color(n, -1);
  rep(u, n) if (color[u] == -1) {
    int v = u;
    do {
      color[v] = u;
      v = f[v];
    } while (color[v] == -1);
    if (color[v] == u) {
      vector<int> C;
      int w = v;
      do {
        C.emplace_back(v);
        v = f[v];
      } while (v != w);
      res.emplace_back(C);
    }
  }
  return res;
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> p(n), w(n);
  rep(u, n) scanf("%d", &p[u]), p[u]--;
  rep(u, n) scanf("%d", &w[u]);

  auto C = cycles(p);
  vector<int> id(n);
  rep(i, C.size()) for (int u : C[i]) id[u] = i;

  vector<lint> wsum(C.size());
  rep(i, C.size()) for (int u : C[i]) wsum[i] += w[u];

  lint ans = -INF;
  rep(u0, n) {
    int k_tmp = k;

    lint res = 0;
    int len = C[id[u0]].size();
    if (wsum[id[u0]] > 0) {
      int m = max(k / len - 1, 0);
      res = m * wsum[id[u0]];
      k -= m * len;
      ans = max(ans, res);
    }
    for (int u = u0; k > 0; k--) {
      u = p[u];
      res += w[u];
      ans = max(ans, res);
    }

    k = k_tmp;
  }
  printf("%lld\\n", ans);

  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++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int p[6000], c[6000];
int par[30][6000];
ll dp[30][6000], dp2[30][6000];

int main() {
  int n, K;
  cin >> n >> K;
  rep(i, n) scanf("%d", &p[i]), p[i]--;
  rep(i, n) scanf("%d", &c[i]);
  rep(i, 30) rep(j, n) dp[i][j] = -INFL;
  rep(i, n) {
    dp[0][i] = dp2[0][i] = c[p[i]];
    par[0][i] = p[i];
  }
  for (int i = 1; i < 30; i++)
    rep(j, n) {
      par[i][j] = par[i - 1][par[i - 1][j]];
      dp2[i][j] = dp2[i - 1][j] + dp2[i - 1][par[i - 1][j]];
      dp[i][j] = max(dp[i - 1][j], dp2[i - 1][j] + dp[i - 1][par[i - 1][j]]);
    }
  ll Max = -INFL;
  rep(i, n) {
    int pos = i;
    ll score = 0;
    rep(j, 30) {
      if (K >> j & 1) {
        Max = max(Max, score + dp[j][pos]);
        score += dp2[j][pos];
        pos = par[j][pos];
      }
    }
  }
  cout << Max << endl;
}

Error: TLE

Fixed submission:

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

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

int p[6000], c[6000];
int main() {
  int n, K;
  cin >> n >> K;
  rep(i, n) scanf("%d", &p[i]), p[i]--;
  rep(i, n) scanf("%d", &c[i]);
  ll Max = -INFL;
  rep(i, n) {
    ll sum = 0;
    int pos = i;
    rep(j, K) {
      pos = p[pos];
      sum += c[pos];
      Max = max(Max, sum);
    }
  }
  cout << Max << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}

long long n, k, p[5555], c[5555], ans;

int main() {
  n = read(), k = read(), ans = -0x7f7f7f7f;
  for (int i = 1; i <= n; i++)
    p[i] = read();
  for (int i = 1; i <= n; i++)
    c[i] = read();
  for (int i = 1; i <= n; i++) {
    long long last = i, sum = 0, cnt = 0;
    for (int j = 1; j <= k; j++) {
      sum += c[p[last]];
      ans = max(sum, ans);
      last = p[last];
      cnt++;
      if (last == i && sum <= 0)
        break;
      if (last == i) {
        sum = (k / cnt - 1) * sum;
        ans = max(sum, ans);
        int k_k = k % cnt + cnt;
        while (k_k--) {
          sum += c[p[last]];
          ans = max(sum, ans);
          last = p[last];
        }
        break;
      }
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

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

inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}

long long n, k, p[5555], c[5555], ans;

int main() {
  n = read(), k = read(), ans = -0x7f7f7f7f;
  for (int i = 1; i <= n; i++)
    p[i] = read();
  for (int i = 1; i <= n; i++)
    c[i] = read();
  for (int i = 1; i <= n; i++) {
    long long last = i, sum = 0;
    for (int j = 1; j <= k; j++) {
      sum += c[p[last]];
      ans = max(sum, ans);
      last = p[last];
      if (last == i && sum <= 0)
        break;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 5;
ll n, k;
ll p[N], c[N], vis[N];
vector<ll> adj[N];
vector<ll> s;
ll score;
void dfs(ll parent) {
  vis[parent] = 1;
  score += c[parent];
  s.pb(score);
  for (auto child : adj[parent]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    adj[i].pb(p[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  ll ans = -(ll)1e18;
  for (int i = 1; i <= n; i++) {
    score = 0;
    memset(vis, 0, sizeof(vis));
    dfs(i);
    ll moves = k / sz(s);
    ll mx = -(ll)1e18;
    if (s.back() > 0 && k >= sz(s)) {
      if (moves >= 1) {
        for (int j = 0; j < sz(s); j++) {
          mx = max(mx, s[j]);
        }
        ans = max(ans, (moves - 1) * s.back() + mx);
      }
      mx = 0;
      ll rem = k % sz(s);
      for (int j = 0; j < rem; j++) {
        mx = max(mx, s[j]);
      }
      ans = max(ans, moves * s.back() + mx);
    } else {
      for (int j = 0; j < k; j++) {
        if (j < sz(s)) {
          ans = max(ans, s[j]);
        } else {
          break;
        }
      }
    }
    s.clear();
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>

using namespace std;

#define ll long long int
#define ld long double
#define loop(i, n) for (int i = 0; i < n; i++)
#define loops(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 5;
ll n, k;
ll p[N], c[N], vis[N];
vector<ll> adj[N];
ll ans, score;
void dfs(ll parent, ll moves) {
  score += c[parent];
  ans = max(ans, score);
  for (auto child : adj[parent]) {
    if (moves > 0) {
      dfs(child, moves - 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    adj[i].pb(p[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  ans = INT_MIN;
  for (int i = 1; i <= n; i++) {
    score = 0;
    dfs(p[i], k - 1);
  }
  cout << ans;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  ll n, k;
  ll ans = -INF;
  cin >> n >> k;

  vector<ll> 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];
  }

  for (int i = 0; i < n; i++) {
    ll cycle_sum = 0;
    ll next_i = i;
    ll cycle_cnt = 0;

    while (true) {
      cycle_cnt++;
      next_i = p[next_i];
      cycle_sum += c[next_i];
      if (next_i == i) {
        break;
      }
    }

    next_i = i;
    ll cnt = 0;
    ll path = 0;

    while (true) {
      cnt++;
      path += c[next_i];

      if (cnt > k) {
        break;
      }

      ll score = path + max(0ll, cycle_sum) * ((k - cnt) / cycle_cnt);

      ans = max(ans, score);

      next_i = p[next_i];

      if (next_i == i) {
        break;
      }
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  ll n, k;
  ll ans = -INF;
  cin >> n >> k;

  vector<ll> 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];
  }

  for (int i = 0; i < n; i++) {
    ll cycle_sum = 0;
    ll next_i = i;
    ll cycle_cnt = 0;

    while (true) {
      cycle_cnt++;
      next_i = p[next_i];
      cycle_sum += c[next_i];
      if (next_i == i) {
        break;
      }
    }

    next_i = i;
    ll cnt = 0;
    ll path = 0;

    while (true) {
      cnt++;
      path += c[next_i];

      if (cnt > k) {
        break;
      }

      ll score = path + max(0ll, cycle_sum) * ((k - cnt) / cycle_cnt);

      ans = max(ans, score);

      next_i = p[next_i];
    }
  }

  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  for (i = 0; i < n; ++i)
    cin >> p[i];
  for (i = 0; i < n; ++i)
    cin >> c[i];
  vector<vector<long int>> dp(n, vector<long int>(0));
  for (i = 0; i < n; ++i) {
    int cnt = 0;
    vector<bool> seen(n, false);
    for (int j = i; seen[j] == false;) {
      seen[j] = true;
      j = p[j] - 1;
      dp[i].push_back(c[j]);
      if (cnt > 0)
        dp[i][cnt] += dp[i][cnt - 1];
      ++cnt;
    }
  }
  long int score = 0, sum = 0, j = 0;
  int l, q;
  for (i = 0; i < n; ++i) {
    l = dp[i].size();
    if (l >= k || dp[i][l - 1] <= 0) {
      q = (k > l ? l : k);
      for (l = 0; l < q; ++l)
        if (l == 0 || j < dp[i][l])
          j = dp[i][l];
    } else {
      q = k;
      j = dp[i][l - 1] * ((q - 1) / l);
      q -= ((q - 1) / l) * l;
      for (int p = 0; p < q; ++p)
        if (p == 0 || sum < dp[i][p % l])
          sum = dp[i][p % l];
      j += sum;
    }
    if (score < j || i == 0)
      score = j;
  }
  cout << score << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  for (i = 0; i < n; ++i)
    cin >> p[i];
  for (i = 0; i < n; ++i)
    cin >> c[i];
  vector<vector<long int>> dp(n, vector<long int>(0));
  for (i = 0; i < n; ++i) {
    int cnt = 0;
    vector<bool> seen(n, false);
    for (int j = i; seen[j] == false;) {
      seen[j] = true;
      j = p[j] - 1;
      dp[i].push_back(c[j]);
      if (cnt > 0)
        dp[i][cnt] += dp[i][cnt - 1];
      ++cnt;
    }
  }
  long int score, sum, j;
  int q;
  for (i = 0; i < n; ++i) {
    int l = dp[i].size();
    if (l >= k || dp[i][l - 1] <= 0) {
      if (k > l)
        q = l;
      for (l = 0; l < q; ++l)
        if (l == 0 || sum < dp[i][l])
          sum = dp[i][l];
      j = sum;
    } else {
      q = k;
      j = dp[i][l - 1] * (q / l);
      q -= (q / l) * l;
      for (int p = 0; p < q; ++p)
        if (p == 0 || sum < dp[i][p % l])
          sum = dp[i][p % l];
      j += sum;
    }
    if (score < j || i == 0)
      score = j;
  }
  cout << score << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long ll;

struct Cycle {
  ll score;
  ll size;

  Cycle() {
    score = 0;
    size = 0;
  }
};

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];

  vector<bool> vis(n, false);
  vector<int> toCycleID(n, -1);
  vector<Cycle> cycles;

  for (int i = 0; i < n; i++) {

    ll num = i;

    if (vis[num])
      continue;

    unordered_set<int> vis2;
    vis2.insert(num);

    while (1) {

      // 前の探索で検索済み -> 何もしない
      if (vis[num])
        break;
      vis[num] = true;

      // 今回の探索で一度通った -> サイクル
      if (vis2.count(num)) {
        int cycleId = cycles.size();
        Cycle cycle;
        while (toCycleID[num] == -1) {
          toCycleID[num] = cycleId;
          cycle.score += c[num];
          cycle.size++;
          num = p[num];
        }
        cycles.push_back(cycle);
        break;
      }

      // どちらでもない -> つぎのノードへ
      num = p[num];
      vis2.insert(num);
    }
  }

  ll max = c[0];
  for (int i = 0; i < n; i++) {
    int num = p[i];
    ll sum = c[num];
    ll sum_max = sum;
    bool cycleFlag = true;

    for (int rem = k - 1; rem > 0; rem--) {
      num = p[num];

      if (cycleFlag && toCycleID[num] != -1) {
        Cycle &cycle = cycles[toCycleID[num]];

        if (rem > cycle.size * 2) {
          int cnt = rem / cycle.size - 1;

          // サイクルが正のスコア
          if (cycle.score > 0) {
            sum += cnt * cycle.score;
            rem -= cnt * cycle.size;
          }
          // サイクルが負のスコア
          else {
            rem -= cnt * cycle.size;
          }
        }
        cycleFlag = false;
      }

      sum += c[num];
      if (sum_max < sum)
        sum_max = sum;
    }

    if (max < sum_max)
      max = sum_max;
  }

  cout << max << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];

  ll max = ll(-1'000'000'000'000'000'000);
  for (int i = 0; i < n; i++) {
    int num = i;
    ll sum = 0;

    num = p[num];
    sum += c[num];

    ll sum_max = sum;

    for (int j = 0; j < k - 1; j++) {
      num = p[num];
      sum += c[num];
      if (sum_max < sum)
        sum_max = sum;
    }

    if (max < sum_max)
      max = sum_max;
  }

  cout << max << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n) p[i]--;
  ll ans = -1e18;
  rep(si, n) {
    int x = si;
    vector<int> s;
    ll tot = 0;
    while (1) {
      x = p[x];
      s.push_back(c[x]);
      tot += c[x];
      if (x == si)
        break;
    }
    int l = s.size();
    ll t = 0;
    rep(i, l) {
      t += s[i];
      if (i + 1 > k)
        break;
      ll now = t;
      if (tot > 0) {
        ll e = (k - (i + 1)) / l;
        now += tot * e;
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  rep(i, n) {
    int m;
    cin >> m;
    m--;
    p[i] = m;
  }
  vector<vector<int>> dp(n, vector<int>(k));
  rep(i, n) {
    int m;
    cin >> m;
    c[i] = m;
  }
  ll aans(-100000000000);
  rep(i, n) {
    ll ans(0), mans(-1000000000000);
    int kk = k;
    int s = p[i];

    while (kk) {

      if (dp[s][kk] != 0) {
        mans = dp[s][kk];
        break;
      }
      ans += c[s];
      s = p[s];
      --kk;
      mans = max(mans, ans);
      dp[s][kk] = mans;
      if (s == i) {
        break;
      }
    }
    // cout << mans << endl;
    aans = max(aans, mans);
  }
  cout << aans;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  ll N, K;
  cin >> N >> K;
  ll 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];
  ll ans = -big;
  ll cycle[N], now;
  ll tmpans;
  ll get1[N];
  for (int i = 0; i < N; ++i) {
    now = i;
    get1[i] = 0;
    cycle[i] = -1;
    for (ll j = 0; j < K; ++j) {
      now = P[now];
      get1[i] += C[now];
      ans = max(ans, get1[i]);
      if (now == i) {
        cycle[i] = j + 1;
        break;
      }
    }
  }
  ll num;
  for (int i = 0; i < N; ++i) {
    if (cycle[i] == -1 || get1[i] <= 0)
      continue;
    get1[i] = (K / cycle[i] - 1) * get1[i];
    now = i;
    num = K / cycle[i] - 1;
    for (int j = 0; j < K - num * cycle[i]; ++j) {
      now = P[now];
      get1[i] += C[now];
      ans = max(ans, get1[i]);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  ll N, K;
  cin >> N >> K;
  ll 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];
  ll ans = -big;
  ll cycle[N], now;
  ll tmpans;
  ll get1[N];
  for (int i = 0; i < N; ++i) {
    map<int, int> amap;
    now = i;
    get1[i] = 0;
    cycle[i] = -1;
    for (ll j = 0; j < K; ++j) {
      amap[now] = j + 1;
      now = P[now];
      get1[i] += C[now];
      ans = max(ans, get1[i]);
      if (amap[now] > 0) {
        cycle[i] = j - amap[now] + 2;
        break;
      }
    }
  }
  ll num;
  for (int i = 0; i < N; ++i) {
    if (cycle[i] == -1 || get1[i] < 0)
      continue;
    get1[i] = (K / cycle[i] - 1) * get1[i];
    now = i;
    num = K / cycle[i] - 1;
    for (int j = 0; j < K - num * cycle[i]; ++j) {
      now = P[now];
      get1[i] += C[now];
      ans = max(ans, get1[i]);
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> P(n);
  vector<ll> C(n);
  rep(i, n) {
    cin >> P.at(i);
    P.at(i)--;
  }
  rep(i, n) { cin >> C.at(i); }
  ll ans = -1e18;
  rep(start, n) {
    ll tot = 0;
    vector<ll> S;
    ll now = start;
    // 周期を求める．
    while (1) {
      now = P[now];        // 場所の更新
      S.push_back(C[now]); // 移動先の配列をとる．
      tot += C[now]; // 一周した後の合計が正か負かで場合分け
      if (now == start)
        break; // 一周したらbreak
    }
    ll l = S.size();
    ll tem = 0;
    ll t = 0;
    rep(i, l) {
      t += S[i];
      if (i + 1 > k)
        break;
      ll now = t;
      if (tot > 0) {
        ll e = (k - i - 1) / l;
        now += tot * e;
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> P(n);
  vector<ll> C(n);
  rep(i, n) {
    cin >> P.at(i);
    P.at(i)--;
  }
  rep(i, n) { cin >> C.at(i); }
  ll ans = -1e18;
  rep(start, n) {
    ll tem = 0;
    ll now = start;
    rep(move, k) {
      tem += C[P[now]];
      ans = max(tem, ans);
      now = P[now];
    }
    // ans = max(tem,ans) ;
  }
  cout << ans << endl;
}