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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int p[5010], c[5010];
long long s[5010][35];
int d[5010][35];

long long cal(int x, int y) {
  if (y <= 0)
    return -1e9;
  long long ret = 0;
  for (int i = 29; i >= 0; i--) {
    if (y >= (1 << i)) {
      y -= (1 << i);
      ret += s[x][i];
      x = d[x][i];
    }
  }
  return ret;
}

int main() {
  int n, m, 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]);
  }
  for (int i = 1; i <= n; i++) {
    d[i][0] = p[i];
  }
  for (int j = 1; j < 30; j++) {
    for (int i = 1; i <= n; i++) {
      d[i][j] = d[d[i][j - 1]][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    s[i][0] = c[p[i]];
  }
  for (int j = 1; j < 30; j++) {
    for (int i = 1; i <= n; i++) {
      s[i][j] = s[i][j - 1] + s[d[i][j - 1]][j - 1];
    }
  }
  long long ans = -1e9;
  for (int i = 1; i <= n; i++) {
    long long now = 0, np = p[i];
    for (int j = 1; j <= min(n, k); j++) {
      now += c[np];
      np = p[np];
      ans = max(ans, now);
    }
    for (int j = k; j >= k - n; j--) {
      ans = max(ans, cal(i, j));
    }
  }
  printf("%lld\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int p[5010], c[5010];
long long s[5010][35];
int d[5010][35];

long long cal(int x, int y) {
  if (y <= 0)
    return -1e9;
  long long ret = 0;
  for (int i = 29; i >= 0; i--) {
    if (y >= (1 << i)) {
      y -= (1 << i);
      ret += s[x][i];
      x = d[x][i];
    }
  }
  return ret;
}

int main() {
  int n, m, 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]);
  }
  for (int i = 1; i <= n; i++) {
    d[i][0] = p[i];
  }
  for (int j = 1; j < 30; j++) {
    for (int i = 1; i <= n; i++) {
      d[i][j] = d[d[i][j - 1]][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    s[i][0] = c[p[i]];
  }
  for (int j = 1; j < 30; j++) {
    for (int i = 1; i <= n; i++) {
      s[i][j] = s[i][j - 1] + s[d[i][j - 1]][j - 1];
    }
  }
  long long ans = -1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= min(n, k); j++) {
      ans = max(ans, cal(i, j));
    }
    for (int j = k; j >= k - n; j--) {
      ans = max(ans, cal(i, j));
    }
  }
  printf("%lld\\n", ans);

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

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 ans = -1000000000;
  rep(i, n) {
    ll loop_num = k;
    ll loop_score = -1000000000;
    ll score = 0;
    vector<ll> scores;
    ll next = p[i];
    rep(j, k) {
      score += c[next];
      next = p[next];
      scores.push_back(score);
      if (next == p[i]) {
        loop_num = j + 1;
        loop_score = score;
        break;
      }
    }
    ll a1 = 0, a2 = 0;
    if (loop_num > 0) {
      a1 = k / loop_num; // 一周できる回数
      a2 = k % loop_num; // 一周繰り返した後の余り
    }
    if (a1 > 0 && loop_score > 0) {
      rep(j, loop_num) {
        ll p = 0;
        if (j >= a2)
          p = -1;
        ans = max(ans, scores[j] + loop_score * (a1 + p));
      }
    } else {
      rep(j, loop_num) { ans = max(ans, scores[j]); }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

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 ans = -1000000000;
  rep(i, n) {
    ll loop_num = 1000000000;
    ll loop_score = -1000000000;
    ll score = 0;
    vector<ll> scores;
    ll next = p[i];
    rep(j, k) {
      score += c[next];
      next = p[next];
      scores.push_back(score);
      if (next == p[i]) {
        loop_num = j + 1;
        loop_score = score;
        break;
      }
    }
    ll a1 = 0, a2 = 0;
    if (loop_num > 0) {
      a1 = k / loop_num; // 一周できる回数
      a2 = k % loop_num; // 一周繰り返した後の余り
    }
    if (a1 > 0 && loop_score > 0) {
      rep(j, loop_num) {
        ll p = 0;
        if (j >= a2)
          p = -1;
        ans = max(ans, scores[j] + loop_score * (a1 + p));
      }
    } else {
      rep(j, loop_num) { ans = max(ans, scores[j]); }
    }
  }
  cout << ans << endl;

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

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

vector<int> cycle(int ind, vector<int> Dest) {

  vector<int> c = {};
  while (Dest.at(ind) != -1) {
    c.push_back(Dest.at(ind) - 1);
    int ind2 = ind;
    ind = Dest.at(ind) - 1;
    Dest.at(ind2) = -1;
  }

  return c;
}

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> P(N), C(N);

  for (int i = 0; i < N; i++) {
    cin >> P.at(i);
  }

  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
  }

  int64_t score = -1000000000;
  int64_t score2 = -1000000000;

  for (int i = 0; i < N; i++) {
    vector<int> cyc = cycle(i, P);
    int n = cyc.size();
    int64_t m = 0;
    int64_t l = 0;
    for (int j = 0; j < min(K, K % n + n); j++) {
      m += C.at(cyc.at(j % n));
      if (m > score2) {
        score2 = m;
      }
      if (j == n - 1) {
        l = m;
      }
    }

    if (K >= 2 * n && l > 0) {
      score2 += l * (K / n - 1);
    }

    if (score <= score2) {
      score = score2;
    }
    score2 = -1000000000;
  }

  cout << score << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> cycle(int ind, vector<int> Dest) {

  vector<int> c = {};
  while (Dest.at(ind) != -1) {
    c.push_back(Dest.at(ind) - 1);
    int ind2 = ind;
    ind = Dest.at(ind) - 1;
    Dest.at(ind2) = -1;
  }

  return c;
}

int64_t game(int Wid, int Step, vector<int> Dest, vector<int> Point, int Pos) {

  if (Step <= 1) {
    return (int64_t)Point.at(Dest.at(Pos) - 1);
  }

  return game(Wid, Step - 1, Dest, Point, Dest.at(Pos) - 1) +
         Point.at(Dest.at(Pos) - 1);
}

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> P(N), C(N);

  for (int i = 0; i < N; i++) {
    cin >> P.at(i);
  }

  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
  }

  int64_t score = -1000000000;
  int64_t score2 = -1000000000;

  for (int i = 0; i < N; i++) {
    vector<int> cyc = cycle(i, P);
    int n = cyc.size();
    int64_t m = 0;
    for (int j = 0; j < K; j++) {
      m += C.at(cyc.at(j % n));
      if (m > score2) {
        score2 = m;
      }
    }

    if (score <= score2) {
      score = score2;
    }
    score2 = -1000000000;
  }

  cout << score << endl;

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

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int p[5010];
ll c[5010];
bool flag[5010];
vector<int> v;
ll solve(int x, int n, ll k) {
  for (int i = 0; i < n; i++)
    flag[i] = false;
  ll sc = 0, ans = 0;
  v.clear();
  x = p[x];
  ans += c[x];
  k--;
  int now = x, cnt = 0;
  while (!flag[p[now]]) {
    sc += c[now];
    flag[p[now]] = true;
    cnt++;
    v.push_back(p[now]);
    now = p[now];
  }
  // cout << x << " " << cnt << " " << sc << endl;
  for (int i = 0; i < cnt; i++)
    v.push_back(v[i]);
  if (sc > 0) {
    ll r = k % (ll)v.size();
    if (k > v.size()) {
      ans += 2 * sc * ((k - (ll)v.size() - r) / (ll)v.size());
      k = v.size() + r;
    }
    for (int i = 0; i < r; i++)
      v.push_back(v[i]);
    ll ans1 = 0, ans2 = 0;
    for (int i = 0; i < k; i++) {
      ans1 += c[v[i]];
      ans2 = max(ans1, ans2);
    }
    ans += ans2;
  } else {
    ll ans1 = 0, ans2 = 0;
    for (int i = 0; i < min(k, (ll)v.size()); i++) {
      ans1 += c[v[i]];
      ans2 = max(ans1, ans2);
    }
    ans += ans2;
  }
  // cout << x << " " << ans << endl;
  return ans;
}

int main() {
  int i, n, k;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++)
    cin >> c[i];
  ll ans = -1000000000000000000;
  for (i = 0; i < n; i++) {
    ll z = solve(i, n, k);
    ans = max(ans, z);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int p[5010];
ll c[5010];
bool flag[5010];
vector<int> v;
ll solve(int x, int n, ll k) {
  for (int i = 0; i < n; i++)
    flag[i] = false;
  ll sc = 0, ans = 0;
  v.clear();
  ans += c[x];
  x = p[x];
  k--;
  int now = x, cnt = 0;
  while (!flag[now]) {
    v.push_back(now);
    flag[now] = true;
    sc += c[now];
    cnt++;
    now = p[now];
  }
  for (int i = 0; i < cnt; i++)
    v.push_back(v[i]);
  if (sc > 0) {
    ll r = k % (ll)v.size();
    if (k > v.size()) {
      ans += 2 * sc * (k - (ll)v.size() - r) / (ll)v.size();
      k = v.size() + r;
    }
    for (int i = 0; i < r; i++)
      v.push_back(v[i]);
    ll ans1 = 0, ans2 = 0;
    for (int i = 0; i < k; i++) {
      ans1 += c[v[i]];
      ans2 = max(ans1, ans2);
    }
    ans += ans2;
    exit(1);
  } else {
    ll ans1 = 0, ans2 = 0;
    for (int i = 0; i < min(k, (ll)v.size()); i++) {
      ans1 += c[v[i]];
      ans2 = max(ans1, ans2);
    }
    ans += ans2;
  }
  // cout << x << " " << ans << endl;
  return ans;
}

int main() {
  int i, n, k;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++)
    cin >> c[i];
  ll ans = -1000000000000000000;
  for (i = 0; i < n; i++) {
    ll z = solve(i, n, k);
    ans = max(ans, z);
  }
  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++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll f(vll v, vll c, ll k) {
  ll n = v.size();
  rep(i, n) v.push_back(v[i]);
  ll ret1 = -1100000000000000000LL;
  rep(i, n) {
    ll tmp = 0LL;
    rep(j, min(n, k)) {
      tmp += c[v[i + j]];
      ret1 = max(ret1, tmp);
    }
  }
  ll sum = 0LL;
  rep(i, n) sum += c[v[i]];
  if (sum < 0 || k <= n)
    return ret1;
  ll l = k % n;
  ll m = k / n;
  ll ret2 = -1100000000000000000LL;
  rep(i, n) {
    ll tmp = 0LL;
    rep(j, l) {
      tmp += c[v[i + j]];
      ret2 = max(ret2, tmp);
    }
  }
  ret2 += sum * m;
  ll ret3 = sum * (m - 1) + ret1;
  ll ret = max(ret3, max(ret1, ret2));
  return ret;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  vector<bool> check(n, false);
  vector<vll> v;
  rep(i, n) {
    if (check[i])
      continue;
    check[i] = true;
    ll key = i;
    vll tmp;
    tmp.push_back(key);
    ll a = key;
    while (true) {
      a = p[a];
      if (a == key)
        break;
      tmp.push_back(a);
      check[a] = true;
    }
    v.push_back(tmp);
  }
  ll ans = -1100000000000000000LL;
  rep(i, v.size()) {
    ll tmp = f(v[i], c, k);
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll f(vll v, vll c, ll k) {
  ll n = v.size();
  rep(i, n) v.push_back(v[i]);
  ll ret1 = -1100000000000000000LL;
  rep(i, n) {
    ll tmp = 0LL;
    rep(j, min(n, k)) {
      tmp += c[v[i + j]];
      ret1 = max(ret1, tmp);
    }
  }
  ll sum = 0LL;
  rep(i, n) sum += c[v[i]];
  if (sum < 0 || k <= n)
    return ret1;
  ll m = 0;
  while (k > n) {
    k -= n;
    m++;
  }
  ll ret2 = -1100000000000000000LL;
  rep(i, n) {
    ll tmp = 0LL;
    rep(j, k) {
      tmp += c[v[i + j]];
      ret2 = max(ret2, tmp);
    }
  }
  ret2 += sum * m;
  return max(ret1, ret2);
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  vector<bool> check(n, false);
  vector<vll> v;
  rep(i, n) {
    if (check[i])
      continue;
    check[i] = true;
    ll key = i;
    vll tmp;
    tmp.push_back(key);
    ll a = key;
    while (true) {
      a = p[a];
      if (a == key)
        break;
      tmp.push_back(a);
      check[a] = true;
    }
    v.push_back(tmp);
  }
  ll ans = -1100000000000000000LL;
  rep(i, v.size()) {
    ll tmp = f(v[i], c, k);
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

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

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);

LL N, K;
vector<LL> P, C;

LL h(int i, int r) {
  LL ret = INF * (-1);
  LL u = 0;
  LL pos = P[i];
  for (int j = 0; j < r; j++) {
    u += C[pos];
    if (ret < u) {
      ret = u;
    }
    pos = P[pos];
  }
  return ret;
}

LL f(int i) {
  if (K <= N + 1) {
    return h(i, K);
  }

  LL wa = INF * (-1);
  LL wb = wa, wc = wa, wd = wa;

  LL pos = P[i];
  LL ipos = pos;
  LL count = 0, loop = 0, u = 0;
  // while(count < K){
  while (true) {
    u += C[pos];
    if (wa < u) {
      wa = u;
    }
    count++;
    pos = P[pos];
    if (pos == ipos) {
      loop = 1;
      break;
    }
  }

  // 周期はcount;
  if (loop == 1) {
    // K = q*count + r;
    LL q = K / count;
    LL r = K % count;
    LL s = h(i, r);
    wb = q * u + s;
    wc = h(i, count);
    wd = (q - 1) * u + wc;
#ifdef DEBUG1
    printf("i=%d, count=%lld, q=%lld, r=%lld, wa=%lld, wb=%lld\\n", i, count, q,
           r, wa, wb);
#endif
  }
  LL ret = 0;
  ret = max(wa, max(wb, max(wc, wd)));
  return ret;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    LL p;
    cin >> p;
    P.push_back(p - 1);
  }
  for (int i = 0; i < N; i++) {
    LL c;
    cin >> c;
    C.push_back(c);
  }

  LL max_point = INF * (-1);
  for (int i = 0; i < N; i++) {
    LL w = f(i);
    if (max_point < w) {
      max_point = w;
    }
  }
  printf("%lld\\n", max_point);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

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

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, K;
  vector<LL> P;
  vector<LL> C;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    LL p;
    cin >> p;
    P.push_back(p - 1);
  }
  for (int i = 0; i < N; i++) {
    LL c;
    cin >> c;
    C.push_back(c);
  }

  LL max_point = INF * (-1);
  for (int i = 0; i < N; i++) {
    LL w = INF * (-1);
    LL p = 0;
    LL pos = P[i];
    int count = 0;
    while (count < K) {
      p += C[pos];
      if (w == p) {
        break;
      }
      if (w < p) {
        w = p;
      }
      count++;
      pos = P[pos];
    }
    if (max_point < w) {
      max_point = w;
    }
  }
  printf("%lld\\n", max_point);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// wa&ac
#include <stdio.h>
#define N 5005
typedef long long ll;
int to[N][40];
ll sum[N][40], dp[N][40]; // dp[i][j]:i节点走2^j步得到的最大分
#define mx(a, b) ((a) > (b) ? (a) : (b))
int main() {
  int n, i, j, now, nex;
  ll k, ans = -1000000009, cnt, tot, packup;
  scanf("%d%lld", &n, &k);
  for (i = 1; i <= n; ++i)
    scanf("%d", to[i]);
  for (i = 1; i <= n; ++i)
    scanf("%lld", sum[i]);
  for (i = 1; i <= n; ++i)
    dp[i][0] = sum[i][0];
  for (i = 0; i < 33; ++i) {
    for (j = 1; j <= n; ++j) {
      nex = to[j][i];
      to[j][i + 1] = to[nex][i];
      sum[j][i + 1] = sum[j][i] + sum[nex][i];
      dp[j][i + 1] = mx(dp[j][i], sum[j][i] + dp[nex][i]);
    }
  }
  packup = k;
  for (i = 1; i <= n; ++i) {
    cnt = -1000000009;
    now = i;
    tot = 0;
    for (j = 0; k > 0; ++j) {
      if (k & 1)
        ans = mx(ans, mx(dp[i][j], tot + dp[now][j])), tot += sum[now][j],
        now = to[now][j];
      k /= 2;
    }
    k = packup;
  }
  printf("%lld", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int p[5005], a[5005], k;
long long travel(int now, int cnt) {
  if (cnt == k + 1)
    return 0;
  int to = p[now];
  long long ans = travel(to, cnt + 1);
  if (ans > 0)
    return ans + a[to];
  else
    return a[to];
}
int main() {
  int n, i, j;
  long long r, ans = -(1 << 30);
  scanf("%d%d", &n, &k);
  for (i = 1; i <= n; ++i)
    scanf("%d", p + i);
  for (i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (i = 1; i <= n; ++i) {
    r = travel(i, 1);
    if (r > ans)
      ans = r;
  }
  printf("%lld", ans);
  return 0;
}
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++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> PlP;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

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 M = min(N, K);
  ll ans = -1e18;
  rep(i, N) {
    ll pos = P[i];
    ll cur = 0;
    rep(j, M) {
      cur += C[pos];
      pos = P[pos];
      cmax(ans, cur);
    }
  }
  if (K <= N) {
    cout << ans << endl;
    return 0;
  }
  vector<Pll> v(N);
  rep(i, N) {
    ll pos = P[i];
    ll cur = 0;
    vector<ll> memo(N, 0);
    rep(j, N + 10) {
      if (memo[pos] == 1) {
        v[i] = Pll(cur, j);
        break;
      } else {
        memo[pos] = 1;
        cur += C[pos];
        pos = P[pos];
      }
    }
  }
  // rep(i,v.size()){
  //     cout << v[i].fi << " " << v[i].se << endl;
  // }
  rep(i, N) {
    ll sc = v[i].fi;
    ll cnt = v[i].se;
    ll cur = 0;
    // cout << "bbb:"<< v[0].se << endl;
    cur += (K / cnt) * sc;
    ll r = K % cnt;
    ll pos = P[i];
    // cout << "aaa"<<endl;
    cmax(ans, cur);
    rep(i, r) {
      cur += C[pos];
      cmax(ans, cur);
      pos = P[pos];
    }
  }
  rep(i, N) {
    ll sc = v[i].fi;
    ll cnt = v[i].se;
    ll cur = 0;
    cur += ((K / cnt) - 1) * sc;
    ll pos = P[i];
    cmax(ans, cur);
    rep(i, cnt) {
      cur += C[pos];
      cmax(ans, cur);
      pos = P[pos];
    }
  }
  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++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> PlP;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

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 ans = -1e18;
  rep(i, N) {
    ll cur = 0;
    ll pos = i;
    rep(j, K) {
      pos = P[pos];
      cur += C[pos];
      cmax(ans, cur);
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1e12;

int main() {
  // 愚直に
  int N, K;
  cin >> N >> K;
  vector<int> pos(N);
  rep(i, N) cin >> pos[i];
  rep(i, N)-- pos[i];
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  ll ans = -INF;
  rep(i, N) {
    // スタートがiの場合
    vector<int> score;
    ll firstCir = 0;
    int x = i;
    rep(j, min(N, K)) {
      x = pos[x];
      firstCir += C[x];
      score.push_back(C[x]);
      ans = max(firstCir, ans);
      if (x == i)
        break;
    }
    int length = sz(score);
    if (K > length) {
      ll temp = 0;
      rep(j, length) {
        temp += score[j];
        ll remainCir = (K - (j + 1)) / length;
        ll z = temp + (firstCir * remainCir);
        ans = max(ans, z);
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const ll INF = 1e15;

int main() {
  // 循環するものを配列に入れる、その回数での最大値を配列maで保持
  int N, K;
  cin >> N >> K;
  vector<int> Point(N);
  rep(i, N) cin >> Point[i];
  rep(i, N) Point[i]--;
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  vector<P> cur(N); // ポイントと初期位置を保持
  rep(i, N) cur[i] = P(0, i);
  // vector<int> pos(N);
  vector<ll> ma(N);
  rep(i, N) ma[i] = -INF;
  rep(i, N) {
    vector<P> prev(N);
    swap(cur, prev);
    for (int j = 0; j < N; j++) {
      cur[Point[j]].first = prev[j].first + C[Point[j]];
      cur[Point[j]].second = prev[j].second;
    }
    rep(k, N) ma[i] = max(ma[i], cur[k].first);
  }
  ll ans = -INF;
  if (K <= N) {
    rep(i, K) ans = max(ans, ma[i]);
  } else {
    // KがNを超える
    rep(i, K) ans = max(ans, ma[i]);
  }
  cout << ans << '\\n';
  return 0;
  // ll, 0, -, 1i, for s&g
}