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

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

  vector<int> p(n);
  vector<ll> 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>> v(n);

  for (int i = 0; i < n; i++) {
    int j = i;
    while (p[j] != i) {
      v[i].push_back(p[j]);
      j = p[j];
    }
    v[i].push_back(i);
  }

  /*
  for(int i = 0; i < n; i++) {
      for(auto x : v[i]) {
          cout << x << " ";
      }
      cout << endl;
  }
   */

  ll ans = LONG_LONG_MIN;
  for (int i = 0; i < n; i++) {
    int s = v[i].size();

    ll tmp = 0;
    ll wa = 0;

    int q = k / s;
    int r = k % s;

    vector<ll> mx(s, LONG_LONG_MIN);
    mx[0] = c[v[i][0]];
    wa = c[v[i][0]];

    for (int j = 1; j < s; j++) {
      wa += c[v[i][j]];
      mx[j] = max(mx[j - 1], wa);
    }

    // for(auto x : v[i]) wa += c[x];

    if (wa >= 0 && q >= 1)
      tmp = wa * (ll)(q - 1);

    if (q == 0)
      tmp += mx[r - 1];
    if (q != 0 && r == 0)
      tmp += mx[s - 1];
    if (q != 0 && r >= 1)
      tmp += max(mx[s - 1], wa + mx[r - 1]);

    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

  vector<int> p(n);
  vector<ll> 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>> v(n);

  for (int i = 0; i < n; i++) {
    int j = i;
    while (p[j] != i) {
      v[i].push_back(p[j]);
      j = p[j];
    }
    v[i].push_back(i);
  }

  /*
  for(int i = 0; i < n; i++) {
      for(auto x : v[i]) {
          cout << x << " ";
      }
      cout << endl;
  }
   */

  ll ans = LONG_LONG_MIN;
  for (int i = 0; i < n; i++) {
    int s = v[i].size();

    ll tmp = 0;
    ll wa = 0;

    int q = k / s;
    int r = k % s;

    vector<ll> mx(s, LONG_LONG_MIN);
    mx[0] = c[v[i][0]];
    wa = c[v[i][0]];

    for (int j = 1; j < s; j++) {
      wa += c[v[i][j]];
      mx[j] = max(mx[j - 1], wa);
    }

    // for(auto x : v[i]) wa += c[x];

    if (wa >= 0 && q >= 1)
      tmp = wa * (ll)(q - 1);
    tmp += max(mx[s - 1], wa + mx[q]);

    ans = max(ans, tmp);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long dp[5555][5555], p[5555], score[5555], loop[5555];
int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> score[i];
  for (int i = 0; i < n; i++) {
    int cn = i;
    for (int j = 1; j <= n; j++) {
      cn = p[cn];
      dp[i][j] = dp[i][j - 1] + score[cn];
      if (cn == i) {
        loop[i] = j;
        break;
      }
    }
  }
  long long ans = -1e18;
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= min(loop[i], k); j++)
      if (dp[i][loop[i]] > 0) {
        ans = max(ans, dp[i][j] + (k - j) / loop[i] * dp[i][loop[i]]);
      } else
        ans = max(ans, dp[i][j]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
int p[5555], c[5555];
long long ans = -1e18;
void dfs(int x, int cnt, long long sum) {
  if (cnt == k)
    return;
  sum += c[x];
  ans = max(sum, ans);
  dfs(p[x], cnt + 1, sum);
}
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++) {
    dfs(i, 0, 0);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

ll N, K;

const ll MAXN = 5000;
ll P[MAXN];
ll C[MAXN];

ll Score[MAXN];
ll Step[MAXN];

ll solve(ll startIndex) {
  fill(Step, Step + N, -1);

  ll res = C[startIndex];

  ll score = 0;
  ll index = startIndex;

  ll k = 0;

  while (Step[index] == -1) {
    Step[index] = k;
    Score[index] = score;

    const auto nextIndex = P[index];
    index = nextIndex;
    score += C[nextIndex];

    res = max(res, score);
    ++k;
    if (k == K) {
      return res;
    }
  }

  const auto dscore = score - Score[index];
  const auto dstep = k - Step[index];

  const auto dk = ((K - k) / dstep) - 1;

  if (dk > 0) {
    if (dscore >= 0) {
      score += dscore * dk;
    }
    k += dk * dstep;

    res = max(res, score);
  }

  while (k < K) {
    index = P[index];
    score += C[index];

    ++k;

    res = max(res, score);
  }

  return res;
}

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> P[i];
    --P[i];
  }
  rep(i, N) { cin >> C[i]; }

  ll ans = 0;

  rep(i, N) {
    const auto res = solve(i);
    ans = i == 0 ? res : max(ans, res);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

ll N, K;

const ll MAXN = 5000;
ll P[MAXN];
ll C[MAXN];

ll Score[MAXN];
ll Step[MAXN];

ll solve(ll startIndex) {
  fill(Step, Step + N, -1);

  ll res = C[startIndex];

  ll score = 0;
  ll index = startIndex;

  ll k = 0;

  // とりあえずループするまで回す
  while (Step[index] == -1) {
    Step[index] = k;
    Score[index] = score;

    const auto nextIndex = P[index];
    index = nextIndex;
    score += C[nextIndex];

    res = max(res, score);
    ++k;
    if (k == K) {
      return res;
    }
  }

  const auto dscore = score - Score[index];
  const auto dstep = k - Step[index];

  const auto dk = ((K - k) / dstep) - 1;

  if (dscore >= 0 && dk > 0) {
    score += dscore * dk;
    k += dstep * dk;

    res = max(res, score);
  }

  while (k < K) {
    index = P[index];
    score += C[index];

    ++k;

    res = max(res, score);
  }

  return res;
}

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> P[i];
    --P[i];
  }
  rep(i, N) { cin >> C[i]; }

  ll ans = 0;

  rep(i, N) {
    const auto res = solve(i);
    ans = i == 0 ? res : max(ans, res);
  }

  cout << ans << endl;

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

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

ll f(int pos, int k, vector<int> &v, vector<int> &pt) {
  if (k == 0)
    return 0;
  int cnt = 1, npos = v[pos];
  ll npt = pt[npos];
  vector<ll> mxpt(v.size() + 1);
  mxpt[0] = 0;
  mxpt[1] = max((ll)0, npt);
  while (npos != pos) {
    npos = v[npos];
    npt += pt[npos];
    ++cnt;
    mxpt[cnt] = max(mxpt[cnt - 1], npt);
  }
  ll ans;
  if (npt < 0)
    ans = mxpt[min(k, cnt)];
  else {
    ans = max((ll)k / cnt * npt - npt + mxpt[cnt],
              (ll)k / cnt * npt + mxpt[k % cnt]);
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> pn(n), pt(n);
  for (int i = 0; i < n; ++i) {
    cin >> pn[i];
    --pn[i];
  }
  bool isplus = false;
  for (int i = 0; i < n; ++i) {
    cin >> pt[i];
    if (pt[i] >= 0)
      isplus = true;
  }
  if (!isplus) {
    int ans = -1001001001;
    for (int i = 0; i < n; ++i) {
      ans = max(ans, pt[i]);
    }
    cout << ans << endl;
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = i, cnt = k;
    ll nans = 0;
    vector<bool> ok(n, true);
    while (ok[pos] && cnt > 0) {
      ok[pos] = false;
      pos = pn[pos];
      nans += pt[pos];
      ans = max(ans, nans);
      --cnt;
    }
    // cout << "i:" << i+1 << " pos:" << pos+1 << " 残り回数:" << cnt << " 現pt"
    // << nans << endl;
    nans += f(pos, cnt, pn, pt);
    // cout << "-答え" << nans << endl;
    ans = max(ans, nans);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

ll f(int pos, int k, vector<int> &v, vector<int> &pt) {
  if (k == 0)
    return 0;
  int cnt = 1, npos = v[pos];
  ll npt = pt[npos];
  vector<ll> mxpt(v.size());
  mxpt[1] = max((ll)0, npt);
  while (npos != pos) {
    npos = v[npos];
    npt += pt[npos];
    ++cnt;
    mxpt[cnt] = max(mxpt[cnt - 1], npt);
  }
  ll ans;
  if (npt < 0)
    ans = mxpt[k % cnt];
  else {
    ans = (ll)k / cnt * npt + mxpt[k % cnt];
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> pn(n), pt(n);
  for (int i = 0; i < n; ++i) {
    cin >> pn[i];
    --pn[i];
  }
  bool isplus = false;
  for (int i = 0; i < n; ++i) {
    cin >> pt[i];
    if (pt[i] >= 0)
      isplus = true;
  }
  if (!isplus) {
    int ans = -1001001001;
    for (int i = 0; i < n; ++i) {
      ans = max(ans, pt[i]);
    }
    cout << ans << endl;
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int pos = i, cnt = k;
    ll nans = 0;
    vector<bool> ok(n, true);
    while (ok[pos] && cnt > 0) {
      ok[pos] = false;
      pos = pn[pos];
      nans += pt[pos];
      ans = max(ans, nans);
      --cnt;
    }
    // cout << "i:" << i+1 << " pos:" << pos+1 << " 残り回数:" << cnt << " 現pt"
    // << nans << endl;
    nans += f(pos, cnt, pn, pt);
    // cout << "-答え" << nans << endl;
    ans = max(ans, nans);
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1001001001001001001
typedef long long ll;
using namespace std;
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 ans = -INF;
  for (int st = 0; st < n; st++) {
    ll score = 0, sum = 0;
    int pos = st;
    vector<int> s;
    while (1) {
      pos = p[pos];
      s.push_back(c[pos]);
      sum += c[pos];
      if (pos == st) {
        break;
      }
    }
    int sz = s.size();
    if (sz >= k) {
      for (int i = 0; i < k; i++) {
        score += s[i];
        ans = max(ans, score);
      }
    } else {
      for (int i = 0; i < sz; i++) {
        score += s[i];
        ll now = score;
        if (sum > 0) {
          int cnt = (k - i - 1) / sz;
          now += cnt * sum;
        }
        ans = max(ans, now);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1001001001001001001
typedef long long ll;
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  ll ans = -INF;
  for (int st = 0; st < n; st++) {
    ll score = 0;
    vector<ll> sc;
    int pos = st;
    for (int i = 0; i < k; i++) {
      pos = p[pos] - 1;
      score += c[pos];
      sc.push_back(score);
      if (pos == st && k <= 100) {
        ans = max(ans, score);
        if (score > 0) {
          int sz = sc.size();
          ll cnt = k / sz;
          score = cnt * score;
          for (int j = 0; j < (k % sz); j++) {
            pos = p[pos] - 1;
            score += c[pos];
            ans = max(ans, score);
          }
          ans = max(ans, score);
        }
        break;
      }
      ans = max(ans, score);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main() {
  ll n, k, ans = -10000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) cin >> c[i];
  vector<ll> dp(n);
  for (ll i = 0; i < n; i++) {
    ll now = i, start = i, count = 0;
    rep(j, k) {
      now = p[now];
      count++;
      dp[i] += c[now];
      ans = max(ans, dp[i]);
      if (now == start) {
        j = k;
        ll loop = (k - 1) / count;
        dp[i] = dp[i] * loop;
        ll kp = k - (count * loop);
        rep(r, kp) {
          now = p[now];
          dp[i] += c[now];
          ans = max(ans, dp[i]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main() {
  ll n, k, ans = -10000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) cin >> c[i];
  if (k) {
    vector<ll> dp(n);
    for (ll i = 0; i < n; i++) {
      ll now = i;
      rep(j, k) {
        now = p[now];
        dp[i] += c[now];
        ans = max(ans, dp[i]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
void solve() {
  ll n, k = 0, val = 0, m = INT64_MIN, i = 0, j;
  ll ans = 0;
  ll perm[5005]{}, c[5005]{};
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> perm[i];
  for (i = 1; i <= n; i++)
    cin >> c[i];

  for (i = 1; i <= n; i++) {
    ll score = 0;
    int vis[5005] = {};
    ll turns = 0;
    j = i;
    while (turns < k) {
      // if(i==3)cout<<score<<" ";
      j = perm[j];
      if (vis[j]) {
        if (score < 0)
          break;
        score *= (k - turns) / turns;
        for (int p = 0; p < k % turns + turns; p++) {
          score += c[j];
          j = perm[j];
          m = max(m, score);
        }
        break;
      }
      score += c[j];
      turns++;
      vis[j] = 1;
      m = max(m, score);
    }
    // if(score==16)cout<<i;
  }
  cout << m;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
void solve() {
  ll n, k = 0, val = 0, m = INT64_MIN, i = 0, j = 0, l = -1, x, y, z;
  ll ans = 0;
  ll perm[2005]{}, c[2005]{};
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> perm[i];
  for (i = 1; i <= n; i++)
    cin >> c[i];

  for (i = 1; i <= n; i++) {
    ll score = 0;
    int vis[2005] = {};
    ll turns = 0;
    j = i;
    while (turns < k) {
      // if(i==3)cout<<score<<" ";
      j = perm[j];
      if (vis[j]) {
        if (score < 0)
          break;
        score *= k / turns;
        for (int p = 0; p < k % turns; p++) {
          score += c[j];
          j = perm[j];
        }
        m = max(m, score);
        break;
      }
      score += c[j];
      turns++;
      vis[j] = 1;
      m = max(m, score);
    }
    // if(score==16)cout<<i;
  }
  cout << m;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

int main() {
  // std::cout << std::fixed << std::setprecision(10);
  ll ans = -INF;

  ll N, K;
  scanf("%lld %lld", &N, &K);

  vector<ll> jump_list;
  for (ll i = 0; i < N; i++) {
    ll v;
    scanf("%lld", &v);
    jump_list.emplace_back(v - 1);
  }

  vector<ll> point_list;
  for (ll i = 0; i < N; i++) {
    ll v;
    scanf("%lld", &v);
    point_list.emplace_back(v);
  }

  for (ll start = 0; start < N; start++) {
    ll total_point = 0;
    ll current_position = start;
    vll round;
    for (;;) {
      current_position = jump_list[current_position];
      total_point += point_list[current_position];
      round.emplace_back(point_list[current_position]);
      if (current_position == start) {
        break;
      }
    }

    ll current_point = 0;
    for (ll i = 0; i < round.size(); i++) {
      if (i >= K) {
        break;
      }
      current_point += round[i];

      ll calculated = current_point;

      if (total_point > 0) {
        calculated += total_point * ((K - i - 1) / round.size());
      }

      ans = max(ans, calculated);
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll N, K;
vector<ll> jump_list;
vector<ll> point_list;

ll calc(ll start, ll max_steps) {
  ll max_point = -INF;
  ll loop_steps = 0;
  ll loop_point = 0;

  ll current = start;
  bool can_loop = false;
  for (;;) {
    current = jump_list[current];
    loop_steps++;
    loop_point += point_list[current];

    max_point = max(max_point, loop_point);
    if (current == start) {
      can_loop = true;
      break;
    }
  }

  if (!can_loop) {
    return max_point;
  }
  if (can_loop && loop_point <= 0) {
    return max_point;
  }

  ll loop_num = max_steps / loop_steps;
  ll rest = max_steps % loop_steps;

  ll max_point2 = 0;
  ll current2 = start;
  ll current_point = 0;
  for (ll i = 0; i < rest; i++) {
    current2 = jump_list[current2];
    current_point += point_list[current2];
    max_point2 = max(max_point2, current_point);
  }

  return loop_point * loop_num + max_point2;
}

int main() {
  scanf("%lld %lld", &N, &K);

  for (ll i = 0; i < N; i++) {
    ll v;
    scanf("%lld", &v);
    jump_list.emplace_back(v - 1);
  }

  for (ll i = 0; i < N; i++) {
    ll v;
    scanf("%lld", &v);
    point_list.emplace_back(v);
  }

  ll ans = -INF;
  for (ll start = 0; start < N; start++) {
    for (ll k = 1; k <= K; k++) {
      ll tmp = calc(start, k);
      ans = max(ans, tmp);
    }
  }

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


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

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

  vector<ll> g(n);
  vector<ll> c(n);
  vector<ll> u(n, -1);
  ll Ans = -INF;

  rep(i, n) {
    ll p;
    cin >> p;
    p--;
    g[i] = p;
  }
  rep(i, n) cin >> c[i];

  rep(i, n) {
    // cout<<i<<endl;
    ll ans = -INF;
    //  if(u[i]==1)continue;

    vector<ll> v;
    ll now = i;
    ll cnt = 0;
    ll s = 0;
    u[now] = 1;

    while (1) {
      s += c[now];
      now = g[now];
      v.push_back(c[now]);
      // cout<<"c"<<c[now]<<endl;
      cnt += 1;
      u[now] = 1;
      if (now == i)
        break;
    }

    vector<ll> sv(10005, 0);
    rep(j, v.size()) { sv[j + 1] = sv[j] + v[j]; }
    rep(j, v.size()) { sv[v.size() + j + 1] = sv[v.size() + j] + v[j]; }
    ll t = k / cnt;
    t--;
    ll q = k - (t * cnt);
    //  cout<<"s"<<s<<endl;
    // cout<<"cnt"<<cnt<<endl;

    if (s > 0) {
      //  cout<<"q"<<q<<endl;

      rep(j, q) {
        ans = max(ans, sv[j + 1]);
        //    cout<<"sv"<<sv[j+1]<<endl;
      }
      ans += t * s;
    }

    else {
      //  ll q=k-(t*cnt);
      rep(j, q) {
        ans = max(ans, sv[j + 1]);
        //  cout<<"sv"<<sv[j+1]<<endl;
      }
    }
    Ans = max(Ans, ans);
  }

  cout << Ans;
}


Error: WA

Fixed submission:


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

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

  vector<ll> g(n);
  vector<ll> c(n);
  vector<ll> u(n, -1);
  ll Ans = -INF;

  rep(i, n) {
    ll p;
    cin >> p;
    p--;
    g[i] = p;
  }
  rep(i, n) cin >> c[i];

  rep(i, n) {
    // cout<<i<<endl;
    ll ans = -INF;
    //  if(u[i]==1)continue;

    vector<ll> v;
    int now = i;
    int cnt = 0;
    ll s = 0;
    u[now] = 1;

    while (1) {
      s += c[now];
      now = g[now];
      v.push_back(c[now]);
      // cout<<"c"<<c[now]<<endl;
      cnt += 1;
      u[now] = 1;
      if (now == i)
        break;
    }

    vector<ll> sv(5005, 0);
    rep(j, v.size()) { sv[j + 1] = sv[j] + v[j]; }
    rep(j, v.size()) { sv[v.size() + j + 1] = sv[v.size() + j] + v[j]; }
    ll t = k / cnt;
    t--;
    ll q = k - (t * cnt);
    //  cout<<"s"<<s<<endl;
    // cout<<"cnt"<<cnt<<endl;

    if (s > 0) {
      //  cout<<"q"<<q<<endl;

      rep(j, q) {
        ans = max(ans, sv[j + 1]);
        //    cout<<"sv"<<sv[j+1]<<endl;
      }
      ans += t * s;
    }

    else {
      //  ll q=k-(t*cnt);
      rep(j, q) {
        ans = max(ans, sv[j + 1]);
        //  cout<<"sv"<<sv[j+1]<<endl;
      }
    }
    Ans = max(Ans, ans);
  }

  cout << Ans;
}