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

#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

signed main(void) {
  int n, k;
  cin >> n >> k;
  int p[5005], c[5005];
  reps(i, 1, n + 1) cin >> p[i];
  reps(i, 1, n + 1) cin >> c[i];
  int a[5005] = {0}, r[2][3000], rn = 0, rp;
  reps(i, 1, n + 1) {
    if (a[i])
      continue;
    rp = i;
    r[1][rn] = 0;
    while (1) {
      r[1][rn]++;
      a[rp] = rn;
      r[0][rn] += c[rp];
      rp = p[rp];
      if (rp == i)
        break;
    }
    rn++;
  }
  rn--;
  // reps (i, 1, n + 1) cout << a[i];
  // cout << endl;
  int ans = -2000000000;
  reps(i, 1, n + 1) {
    int ans2 = 0, k2 = k;
    int tmp = k / r[1][a[i]] - 1;
    if (r[0][a[i]] > 0) {
      if (tmp > 0) {
        ans2 = r[0][a[i]] * tmp, k2 -= tmp * r[1][a[i]];
        if (ans2 > ans)
          ans = ans2;
      }
    } else {
      k2 = 5005;
    }
    int q = i;
    rep(j, k2) {
      q = p[q];
      ans2 += c[q];
      if (ans2 > ans)
        ans = ans2;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

signed main(void) {
  int n, k;
  cin >> n >> k;
  int p[5005], c[5005];
  reps(i, 1, n + 1) cin >> p[i];
  reps(i, 1, n + 1) cin >> c[i];
  int a[5005] = {0}, r[2][3000], rn = 0, rp;
  reps(i, 1, n + 1) {
    if (a[i])
      continue;
    rp = i;
    r[1][rn] = 0;
    while (1) {
      r[1][rn]++;
      a[rp] = rn;
      r[0][rn] += c[rp];
      rp = p[rp];
      if (rp == i)
        break;
    }
    rn++;
  }
  rn--;
  int ans = -2000000000;
  reps(i, 1, n + 1) {
    int ans2 = 0, k2 = k;
    int tmp = k / r[1][a[i]] - 1;
    if (r[0][a[i]] > 0 && tmp > 0) {
      ans2 = r[0][a[i]] * tmp, k2 -= tmp * r[1][a[i]];
      if (ans2 > ans)
        ans = ans2;
    }
    int q = i;
    rep(j, k2) {
      q = p[q];
      ans2 += c[q];
      if (ans2 > ans)
        ans = ans2;
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> 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) {
    ll x = si;
    ll tot = 0;
    vector<int> s;
    while (1) {
      x = p[x];
      tot += c[x];
      s.push_back(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 += e * tot;
      }
      ans = max(ans, now);
    }
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  long long k;
  long long p[6000];
  long long c[6000];
  long long maximum;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    if (i == 1)
      maximum = c[i];
    else if (maximum < c[i])
      maximum = c[i];
  }

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

    long long score = 0;
    long long times = 0;
    int place = i, f_place = place;
    while (1) {
      place = p[place];
      score += c[place];
      ans = max(ans, score);
      times++;
      if (place == f_place)
        break;
      if (times == k)
        break;
    }
    if (k > times && score > 0) {
      long long count = k / times;
      score = score * count;
      times = count * times;
      while (1) {
        place = p[place];
        score += c[place];
        ans = max(ans, score);
        times++;
        if (times == k)
          break;
      }
    }
  }

  if (maximum <= 0)
    cout << maximum;
  else
    cout << ans;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, k, a[5050], b[15050], v[5050];
long long ans = -1000000000;
bool vis[5050];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int tail = 0;
      int now = i;
      while (!vis[now]) {
        vis[now] = 1;
        b[++tail] = v[now];
        now = a[now];
      }
      long long sum = 0;
      for (int j = 1; j <= tail; j++)
        b[j + tail + tail] = b[j + tail] = b[j], sum += b[j];
      int ti = k;
      int ci = (ti - 1) / tail - 1;
      if (ci < 0)
        ci = 0;
      sum = sum > 0 ? ci * sum : 0;
      ti -= ci * tail;
      long long ma = -1000000000;
      for (int j = 1; j <= tail; j++) {
        long long tmp = 0;
        for (int kk = 0; kk < ti; kk++) {

          tmp += b[j + kk];
          ma = max(ma, tmp);
        }
      }
      ans = max(ans, sum + ma);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k, a[5050], b[15050], v[5050];
long long ans = -1000000000;
bool vis[5050];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int tail = 0;
      int now = i;
      while (!vis[now]) {
        vis[now] = 1;
        b[++tail] = v[now];
        now = a[now];
      }
      long long sum = 0;
      for (int j = 1; j <= tail; j++)
        b[j + tail] = b[j], sum += b[j];
      int ti = k;
      if (sum > 0) {
        sum *= (ti - 1) / tail;
        ti -= (ti - 1) / tail * tail;
      } else
        sum = 0;
      long long ma = -1000000000;
      for (int j = 1; j <= tail; j++) {
        long long tmp = 0;
        for (int kk = 0; kk < ti; kk++) {

          tmp += b[j + kk];
          ma = max(ma, tmp);
        }
      }
      ans = max(ans, sum + ma);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
int n;
long long k;
long long p[5010], c[5010];
long long mx = -1e18;
long long a[5010][5010];
long long sz[5010][5010];
/*
void dfs(int beg,int x,int num,long long sum){
        if(num>0){
                if(s.find(x)!=s.end()){
                        return ;
                }
                s.insert(x);
                mx1[beg]=max(mx1[beg],sum);
                a[beg][num]=sum;
                make[beg]=sum;
        }
        dfs(beg,p[x],num+1,sum+c[p[x]]);
}
*/
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      sz[i][j] = 1e9 + 10;
    }
  }
  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++) {
    int j = p[i];
    long long sum = c[p[i]];
    int num = 1;
    while (i != j) {
      a[i][j] = sum;
      sz[i][j] = num;
      sum += c[p[j]];
      j = p[j];
      num++;
    }
    a[i][i] = sum;
    sz[i][i] = num;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (sz[i][j] > k)
        continue;
      long long tmp = (k - sz[i][j]) / sz[i][i];
      if (a[i][i] > 0)
        mx = max(mx, tmp * a[i][i] + a[i][j]);
      else
        mx = max(mx, a[i][j]);
    }
  }
  cout << mx << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, k;
long long dp[3010][3010][5];
int a[3010], b[3010];
long long v[3010][3010];
int main() {
  cin >> n >> m >> k;
  long long c;
  for (int i = 1; i <= k; i++) {
    cin >> a[i] >> b[i] >> c;
    v[a[i]][b[i]] = c;
  }
  long long mx = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int kk = 1; kk <= 3; kk++) {
        dp[i][j][kk] =
            max(dp[i - 1][j][3] + v[i][j],
                max(dp[i][j - 1][kk - 1] + v[i][j], dp[i][j - 1][kk]));
        mx = max(mx, dp[i][j][kk]);
      }
    }
  }
  cout << mx << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
  ll N, K;
  cin >> N >> K;
  ll round_sum = 0;
  vector<ll> P(N), C(N);
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, N) { cin >> C[i]; }
  ll ans = vmax(C);
  ll tmp, cnt, cnt2, mx, mx2, memo, now;
  bool flag;
  rep(i, N) {
    tmp = P[i];
    cnt = C[tmp];
    mx = cnt;
    memo = 1;
    flag = false;
    while (tmp != i) {
      tmp = P[tmp];
      cnt += C[tmp];
      chmax(mx, cnt);
      memo++;
      if (memo == K) {
        flag = true;
        break;
      }
    }
    if (flag || cnt < 0) {
      chmax(ans, mx);
    } else {
      tmp = P[i];
      cnt2 = C[tmp];
      mx2 = cnt2;
      now = 0;
      while (now < K % memo + memo - 1) {
        tmp = P[tmp];
        cnt2 += C[tmp];
        chmax(mx2, cnt2);
        now++;
      }
      chmax(ans, cnt * (K / memo - 1) + mx2);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
  ll N, K;
  cin >> N >> K;
  ll round_sum = 0;
  vector<ll> P(N), C(N);
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, N) {
    cin >> C[i];
    round_sum += C[i];
  }
  ll ans = vmax(C);
  ll tmp, cnt, mx;
  ll r = K % N;
  ll d = K / N;
  rep(i, N) {
    tmp = i;
    cnt = 0;
    mx = C[P[tmp]];
    rep(j, K) {
      tmp = P[tmp];
      cnt += C[tmp];
      chmax(mx, cnt);
    }
    // ans = max({ans, mx, mx+round_sum*d});
    chmax(ans, mx);
    /*if (K>=N) {
        tmp = i; cnt = 0; mx = C[P[tmp]];
        rep(j, N+r) {
            tmp = P[tmp];
            cnt += C[tmp];
            chmax(mx, cnt);
        }
        ans = max({ans, mx, mx+round_sum*(d-1), round_sum*d});
    }*/
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

const ll inf = 9982443530;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

int p[5005];
ll c[5005];
ll ans = -inf;
int ma[5005], pa, n;

int main() {
  int k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &p[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &c[i]);
  }
  for (int i = 1; i <= n; i++) {
    memset(ma, 0, sizeof(ma));
    pa = k;
    int w = i, flag = 0;
    ll sum = 0, pi = -inf;
    while (pa) {
      w = p[w];
      // printf("%d %d %d\\n",w,pa,sum);
      if (ma[w] == 1 && flag == 0) {
        ll bi = k / (k - pa);
        // printf("%lld\\n",bi);
        if (sum >= 1) {
          ans = max(sum * (bi - 1) + pi, ans);
          sum = sum * bi;
        } else {
          break;
        }
        pa = k % (k - pa);
        flag = 1;
      }
      if (pa == 0) {
        break;
      }
      sum = sum + c[w];
      pa--;
      ma[w]++;
      ans = max(ans, sum);
      pi = max(sum, pi);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

const ll inf = 9982443530;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

int p[5005];
ll c[5005];
ll ans = -inf;
int ma[5005], pa, n;

int main() {
  int k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &p[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &c[i]);
  }
  for (int i = 1; i <= n; i++) {
    memset(ma, 0, sizeof(ma));
    pa = k;
    int w = i, flag = 0;
    ll sum = 0, pi = -inf;
    while (pa) {
      w = p[w];
      // printf("%d %d\\n",w,pa);
      if (ma[w] == 1 && flag == 0) {
        ll bi = k / (k - pa);
        if (sum >= 0) {
          ans = (sum * (bi - 1) + pi, ans);
          sum = sum * bi;
        } else {
          break;
        }
        pa = k % (k - pa);
        flag = 1;
      }
      sum = sum + c[w];
      pa--;
      ma[w]++;
      ans = max(ans, sum);
      pi = max(sum, pi);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}

int main() {

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

  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> points[i];
  }

  for (int i = 1; i <= N; i++) {
    vector<int> nodes;
    int initIdx = i;
    if (memo[i].first != 0)
      continue;

    int currIdx = i;
    ll point_sum = 0;
    int cnt = 0;
    do {
      cnt++;
      currIdx = arr[currIdx];
      point_sum += points[currIdx];
      nodes.push_back(currIdx);
    } while (currIdx != initIdx);

    for (int i = 0; i < nodes.size(); i++) {
      memo[nodes[i]] = {cnt, point_sum};
    }
  }

  ll maxim = -10e20;
  for (int i = 1; i <= N; i++) {
    int tempK = K;
    ll sum = 0;
    double multiple = (double)tempK / memo[i].first;
    if (multiple > 2) {
      if (memo[i].second > 0) {
        sum += ((int)multiple - 1) * memo[i].second;
      }
      tempK -= ((int)multiple - 1) * memo[i].first;
    }

    int currIdx = i;
    for (int j = 0; j < tempK; j++) {
      currIdx = arr[currIdx];
      sum += points[currIdx];
      maxim = max(maxim, sum);
    }
  }

  cout << maxim << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}

int main() {

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

  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> points[i];
  }

  for (int i = 1; i <= N; i++) {
    vector<int> nodes;
    int initIdx = i;
    if (memo[i].first != 0)
      continue;

    int currIdx = i;
    ll point_sum = 0;
    int cnt = 0;
    do {
      cnt++;
      currIdx = arr[currIdx];
      point_sum += points[currIdx];
      nodes.push_back(currIdx);
    } while (currIdx != initIdx);

    for (int i = 0; i < nodes.size(); i++) {
      memo[nodes[i]] = {cnt, point_sum};
    }
  }

  ll maxim = -10e20;
  for (int i = 1; i <= N; i++) {
    int tempK = K;
    ll sum = 0;

    int currIdx = i;
    for (int j = 0; j < tempK; j++) {
      currIdx = arr[currIdx];
      sum += points[currIdx];
      maxim = max(maxim, sum);
    }
  }

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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
  vector<int> parent, s;
  UnionFind(int n) : parent(n), s(n) {
    rep(i, n) {
      parent[i] = i;
      s[i] = 1;
    }
  }
  int root(int x) { return parent[x] == x ? x : parent[x] = root(parent[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return s[root(x)]; }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (s[x] < s[y]) {
      parent[x] = y;
      s[y] += s[x];
    } else {
      parent[y] = x;
      s[x] += s[y];
    }
  }
};

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

  UnionFind u(n);
  rep(i, n) u.unite(i, p[i]);
  vector<ll> s(n);
  rep(i, n) s[u.root(i)] += c[i];
  rep(i, n) s[i] = s[u.root(i)];

  ll ans = -inf;
  rep(i, n) {
    ll res = -inf, t = 0;
    int m = k / u.size(i);
    if (m >= 2 && s[i] > 0)
      t += s[i] * (m - 1);
    int v = i;
    m = min(k, k % u.size(i) + u.size(i));
    rep(_, m) {
      t += c[v = p[v]];
      res = max(res, t);
    }
    ans = max({ans, res, t});
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
  vector<int> parent, s;
  UnionFind(int n) : parent(n), s(n) {
    rep(i, n) {
      parent[i] = i;
      s[i] = 1;
    }
  }
  int root(int x) { return parent[x] == x ? x : parent[x] = root(parent[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return s[root(x)]; }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (s[x] < s[y]) {
      parent[x] = y;
      s[y] += s[x];
    } else {
      parent[y] = x;
      s[x] += s[y];
    }
  }
};

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

  UnionFind u(n);
  rep(i, n) u.unite(i, p[i]);
  vector<ll> s(n);
  rep(i, n) s[u.root(i)] += c[i];
  rep(i, n) s[i] = s[u.root(i)];

  ll ans = -inf;
  rep(i, n) {
    ll res = -inf, t = 0;
    int m = u.size(i), ki = k;
    if (s[i] > 0 && ki >= 2 * m) {
      t += s[i] * (ki / m - 1);
      ki -= m * (ki / m - 1);
      res = max(res, t);
    }
    int v = i;
    rep(_, ki) {
      t += c[v = p[v]];
      res = max(res, t);
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll ans = -1000000000;

  // 各マスから開始した時の1ループの長さ、1ループ時のスコアを計算
  vector<int> len(n);
  vector<ll> total_p(n);
  rep(i, n) {
    len[i] = 0, total_p[i] = 0;
    int masu = i;
    do {
      masu = p[masu];
      total_p[i] += c[masu];
      len[i]++;
    } while (masu != i);
    // cout << i << " " << "len:" << len[i] << ",total" << total_p[i] << endl;
  }

  rep(i, n) {
    // マスiから開始したときのスコアの最大値を計算
    ll point = 0;
    ll nokori_k = k;
    if (k >= 2 * len[i]) {
      ll num_lp = k / len[i] - 2;
      if (total_p[i] >= 0)
        point += total_p[i] * num_lp;
      nokori_k = k - num_lp * len[i];
    }
    ll zan_point = 0;
    ll max_point = -1000000000;
    int masu = i;
    rep(j, nokori_k) {
      masu = p[masu];
      zan_point += c[masu];
      max_point = max(zan_point, max_point);
    }
    point += max_point;

    // ansを更新
    ans = max(ans, point);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  ll ans = -1000000000;

  // 各マスから開始した時の1ループの長さ、1ループ時のスコアを計算
  vector<int> len(n);
  vector<ll> total_p(n);
  rep(i, n) {
    len[i] = 0, total_p[i] = 0;
    int masu = i;
    do {
      masu = p[masu];
      total_p[i] += c[masu];
      len[i]++;
    } while (masu != i);
    // cout << i << " " << "len:" << len[i] << ",total" << total_p[i] << endl;
  }

  rep(i, n) {
    // マスiから開始したときのスコアの最大値を計算
    ll point = 0;
    ll nokori_k = k;
    if (total_p[i] >= 0 && k >= 2 * len[i]) {
      ll num_lp = k / len[i] - 2;
      point += total_p[i] * num_lp;
      nokori_k = k - num_lp * len[i];
    }
    ll zan_point = 0;
    ll max_point = -1000000000;
    int masu = i;
    rep(j, nokori_k) {
      masu = p[masu];
      zan_point += c[masu];
      max_point = max(zan_point, max_point);
    }
    point += max_point;

    // ansを更新
    ans = max(ans, point);
  }

  cout << ans << endl;

  return 0;
}