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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1};
V<int> dy = {0, 0, -1, 1, -1, 1, -1, 1};

int main() {
  int n, k;
  cin >> n >> k;
  V<ll> p(n), c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) p[i]--;
  rep(i, n) cin >> c[i];
  ll ans = -1e18;
  rep(si, n) {
    V<ll> loop; // siの入っているloopの各スコア
    int np = si;
    ll loop_score = 0;
    while (true) {
      np = p[np];
      loop.push_back(c[np]);
      loop_score += c[np];
      if (np == si)
        break;
    }

    ll t = 0;
    rep(i, loop.size()) { // 最後がloop[i] 最後の周はsiからi+1回移動
      if (i + 1 > k)
        break;
      t += loop[i];
      ll now = t;
      if (loop_score > 0) {
        ll cnt = (k - (i + 1)) / loop.size(); // cnt回周回
        now += cnt * loop_score;
      }
      chmax(ans, now);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1};
V<int> dy = {0, 0, -1, 1, -1, 1, -1, 1};

int main() {
  int n, k;
  cin >> n >> k;
  V<ll> p(n), c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) p[i]--;
  rep(i, n) cin >> c[i];
  ll ans = -1e18;
  rep(s, n) {
    ll loop_score = 0, loop_num = 0, now = s;
    while (true) {
      now = p[now];
      loop_score += c[now];
      loop_num++;
      if (now == s)
        break;
    }
    if (loop_score <= 0) {
      now = s;
      ll score = 0, num = 0;
      while (true) {
        now = p[now];
        score += c[now];
        num++;
        chmax(ans, score);
        if (num == k)
          break;
        if (num == loop_num)
          break;
      }
    } else {
      ll loop = k / loop_num; // - 1;
      ll score = loop_score * loop;
      ll num = loop_num * loop;
      now = s;
      while (true) {
        now = p[now];
        score += c[now];
        num++;
        chmax(ans, score);
        if (num == k)
          break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

void dfs(int start, vector<ll> &per, vector<ll> &score, vector<ll> &temp,
         vector<ll> &visited, ll pre) {
  if (visited[start] == 1)
    return;
  visited[start] = 1;
  start = per[start];
  pre += score[start];
  temp.push_back(pre);
  dfs(start, per, score, temp, visited, pre);
}

ll getMaxFrom(vector<ll> &data, int limit) {
  ll ans = data[0];
  for (int i = 0; i < limit; i++)
    ans = max(ans, data[i]);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  t = 1;

  while (t--) {
    int N, K;
    cin >> N >> K;

    vector<ll> per(N);
    for (int i = 0; i < N; i++) {
      cin >> per[i];
      per[i] -= 1;
    }

    vector<ll> score(N);
    for (int i = 0; i < N; i++)
      cin >> score[i];

    vector<vector<ll>> prefix;
    for (int i = 0; i < N; i++) {
      vector<ll> temp;
      vector<ll> visited(N, 0);
      dfs(i, per, score, temp, visited, 0);
      prefix.push_back(temp);
    }

    ll ans = INT_MIN;
    for (int i = 0; i < N; i++) {
      if (K > prefix[i].size()) {
        if (prefix[i][prefix[i].size() - 1] <= 0) {
          ans = max(ans, getMaxFrom(prefix[i], prefix[i].size()));
        } else {
          int len = prefix[i].size();
          ll div = K / len;
          ll rem = K % len;

          ll profit1 = prefix[i][len - 1] * (div - 1);

          profit1 += getMaxFrom(prefix[i], len);

          if (rem != 0) {
            ll profit2 = prefix[i][len - 1] * div;
            profit2 += getMaxFrom(prefix[i], rem);

            profit1 = max(profit1, profit2);
          }

          ans = max(ans, profit1);
        }
      } else {
        ans = max(ans, getMaxFrom(prefix[i], K));
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

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

  int t;
  t = 1;

  while (t--) {
    int N, K;
    cin >> N >> K;

    vector<ll> per(N);
    for (int i = 0; i < N; i++)
      cin >> per[i];

    vector<ll> score(N);
    for (int i = 0; i < N; i++)
      cin >> score[i];

    vector<vector<ll>> dp(N, vector<ll>(K + 1));

    for (int i = 0; i < N; i++)
      dp[i][1] = score[per[i] - 1];

    for (int i = 2; i <= K; i++) {
      for (int j = 0; j < N; j++) {
        dp[j][i] =
            max(score[per[j] - 1], score[per[j] - 1] + dp[per[j] - 1][i - 1]);
      }
    }

    ll ans = dp[1][K];
    for (int i = 1; i < N; i++)
      ans = max(ans, dp[i][K]);

    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 < (n); ++i)

using namespace std;
using ll = long long;

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 (true) {
      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: TLE

Fixed submission:

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

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<int64_t> P(N), C(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> P.at(i);
  }
  for (int64_t i = 0; i < N; i++) {
    cin >> C.at(i);
  }

  int64_t ans = -1000000000;

  for (int64_t i = 0; i < N; i++) {
    int64_t max_score = ans;
    int64_t tmp_score = 0;
    int64_t pos = i;
    for (int64_t j = 0; j < K; j++) {
      pos = P.at(pos) - 1;
      tmp_score += C.at(pos);
      max_score = max(max_score, tmp_score);
    }
    ans = max(ans, max_score);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n);
  vector<ll> c(n);
  for (ll i = 0; i < n; i++) {
    ll _p;
    cin >> _p;
    p[i] = _p - 1;
  }
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
  }

  ll best = -INFINITY;
  for (ll start = 0; start < n; start++) {
    unordered_set<ll> steped;
    ll score = 0;
    ll mode = 0;
    ll turnparloop = INFINITY;
    ll scoreparloop = 0;
    ll loopstart = -1;
    ll _turnls = 0;
    ll _scorels = 0;
    ll now = start;
    ll turn = 0;
    for (; turn < k; turn++) {
      if (mode == 0 && steped.count(now)) {
        mode = 1;
        _turnls = turn;
        _scorels = score;
        loopstart = now;
      } else if (mode == 1 && loopstart == now) {
        mode = 2;
        scoreparloop = score - _scorels;
        turnparloop = turn - _turnls;
        break;
      }
      if (mode == 0)
        steped.insert(now);
      now = p[now];
      score += c[now];
      best = max(best, score);
    }
    if (scoreparloop > 0) {
      ll loops = max(0ll, (k - turn - 1) / turnparloop - 1);
      score += scoreparloop * loops;
      turn += turnparloop * loops;
      best = max(best, score);
      for (; turn < k; turn++) {
        now = p[now];
        score += c[now];
        best = max(best, score);
      }
    }
  }
  cout << best;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n);
  vector<ll> c(n);
  for (ll i = 0; i < n; i++) {
    ll _p;
    cin >> _p;
    p[i] = _p - 1;
  }
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
  }

  ll best = -INFINITY;
  for (ll start = 0; start < n; start++) {
    set<ll> steped;
    ll score = 0;
    ll mode = 0;
    ll turnparloop = INFINITY;
    ll scoreparloop = 0;
    ll loopstart = -1;
    ll _turnls = 0;
    ll _scorels = 0;
    ll now = start;
    ll turn = 0;
    for (; turn < k; turn++) {
      if (mode == 0 && steped.count(now)) {
        mode = 1;
        _turnls = turn;
        _scorels = score;
        loopstart = now;
      } else if (mode == 1 && loopstart == now) {
        mode = 2;
        scoreparloop = score - _scorels;
        turnparloop = turn - _turnls;
        break;
      }
      if (mode == 0)
        steped.insert(now);
      now = p[now];
      score += c[now];
      best = max(best, score);
    }
    if (scoreparloop > 0) {
      ll loops = max(0ll, (k - turn - 1) / turnparloop - 1);
      score += scoreparloop * loops;
      turn += turnparloop * loops;
      best = max(best, score);
      for (; turn < k; turn++) {
        now = p[now];
        score += c[now];
        best = max(best, score);
      }
    }
  }
  cout << best;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

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

  ll ans = -0x3f3f3f3f3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    vector<bool> visited(n + 1, 0);
    int num = 0;
    int next = p[i];
    vector<ll> tmax(n + 1, -0x3f3f3f3f3f3f3f3f);
    ll score = 0;
    while (!visited[next]) {
      score += c[p[next]];
      visited[next] = 1;
      next = p[next];
      num++;
      tmax[num] = max(tmax[num - 1], score);
    }
    if (score < 0)
      score = tmax[min(k, num)];
    else {
      if (num >= k)
        score = tmax[k];
      else
        score = max((k / num) * score + max(0ll, tmax[k % num]),
                    (k / num - 1) * score + max(0ll, tmax[num]));
    }
    ans = max(ans, score);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

  ll ans = -0x3f3f3f3f3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    vector<bool> visited(n + 1, 0);
    int num = 0;
    int next = p[i];
    vector<ll> tmax(n + 1, -0x3f3f3f3f3f3f3f3f);
    ll score = 0;
    while (!visited[next]) {
      score += c[p[next]];
      visited[next] = 1;
      next = p[next];
      num++;
      tmax[num] = max(tmax[num - 1], score);
    }
    if (score < 0)
      score = tmax[max(k, num)];
    else {
      if (num >= k)
        score = tmax[k];
      else
        score = (k / num) * score + max(0ll, tmax[k % num]);
    }
    ans = max(ans, score);
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

#define INF 1e18

using ll = long long;

int p[5010], c[5010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int 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 >> c[i];

  ll ans = -INF;
  for (int i = 0; i < n; i++) {
    int pos = i;
    vector<ll> sum;
    sum.push_back(0);
    for (;;) {
      pos = p[pos];
      sum.push_back(sum[sum.size() - 1] + c[pos]);
      if (pos == i)
        break;
    }

    int cnt = sum.size() - 1;

    ll score = 0;
    int ik = min(k, cnt);
    if (sum[cnt] > 0) {
      score += ((k - 1) / cnt) * sum[cnt];
      ik = (k - 1) % cnt + 1;
    }

    ll smax = -INF;
    for (int j = 1; j <= ik; j++)
      smax = max(smax, sum[j]);
    score += smax;

    ans = max(ans, score);
  }

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
#define INF 1000000000

int p[5010];
int c[5010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, k;
  cin >> n >> k;

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

  ll ans = -INF;

  for (int i = 0; i < n; i++) {
    int num = i;
    vector<ll> s;
    s.push_back(0);
    for (;;) {
      num = p[num] - 1;
      s.push_back(c[num] + s[s.size() - 1]);
      if (num == i)
        break;
    }
    int sz = s.size();

    int cnt = k / (sz - 1);

    ll score = 0;

    int ik = max(k, sz - 1);
    if (s[sz - 1] > 0) {
      score += cnt * s[sz - 1];
      ik = k % (sz - 1);
    }

    ll m = -INF;
    for (int j = 1; j <= ik; j++)
      m = max(m, s[j]);
    score += m;
    ans = max(ans, score);
  }

  cout << ans << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
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);
    p.at(i)--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c.at(i);
  }
  vector<vector<int>> G(n, vector<int>());
  int64_t ans = -1e18;
  for (int i = 0; i < n; i++) {
    int x = i;
    int64_t sum = 0;
    int64_t tmp = 0;
    while (1) {
      G.at(i).push_back(c.at(x));
      sum += c.at(x);
      x = p.at(x);
      if (x == i)
        break;
    }
    for (int j = 0; j < k; j++) {
      int roop_size = G.at(i).size();
      if (j > roop_size - 1)
        break;
      tmp += G.at(i).at(j);
      if (sum > 0) {
        ans = max(ans, tmp + sum * ((k - j - 1) / roop_size));
      } else {
        ans = max(ans, tmp);
      }
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int64_t k, ans = 0;
  cin >> k;
  vector<int64_t> p(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
    p.at(i)--;
  }
  int64_t sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> c.at(i);
    sum += c.at(i);
  }
  vector<int64_t> a(3 * n + 1, 0);
  int tmp = 0;
  int tmp2 = 0;
  while (tmp != 3 * n + 1) {
    a.at(tmp) = c.at(tmp2);
    tmp2 = p.at(tmp2);
    tmp++;
  }
  vector<int64_t> amax(n, -9999999999999999);
  for (int i = 0; i < n; i++) {
    int64_t sumtmp = 0;
    for (int j = i; j - i < n; j++) {
      sumtmp += a.at(j);
      amax.at(j - i) = max(amax.at(j - i), sumtmp);
    }
  }
  if (k <= n)
    cout << amax.at(k - 1) << endl;
  else
    cout << sum * (k / n) + amax.at(k - k / n * n - 1) << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> p[i];
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll ans = INT_MIN;
  for (ll i = 0; i < n; i++) {
    ll csum = 0;
    ll cnt = 0, idx = i;
    do {
      if (cnt == k)
        break;
      idx = p[idx] - 1;
      csum += v[idx];
      ans = max(ans, csum);
      cnt++;
    } while (idx != i);
    // cout<<cnt<<endl;
    if (csum > 0 && cnt < k) {
      ll cyc = k / cnt;
      ll rem = k % cnt;
      // cout<<rem<<endl;
      ll sum1 = csum * cyc;
      ll sum2 = csum * (cyc - 1);
      do {
        idx = p[idx] - 1;
        sum2 += v[idx];
        ans = max(ans, sum2);
      } while (idx != i);
      do {
        if (rem == 0)
          break;
        idx = p[idx] - 1;
        sum1 += v[idx];
        ans = max(ans, sum1);
        rem--;
      } while (idx != i);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> p[i];
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll ans = INT_MIN;
  for (ll i = 0; i < n; i++) {
    ll csum = 0;
    ll cnt = 0, idx = i;
    while (cnt < k && idx != i) {
      idx = p[idx] - 1;
      csum += v[idx];
      ans = max(ans, csum);
      cnt++;
    }
    if (cnt < k) {
      ll cyc = k / cnt;
      ll rem = k % cnt;
      csum = (csum * cyc);
      ans = max(ans, csum);
      ll c = 0;
      while (c < rem) {
        idx = p[idx] - 1;
        csum += v[idx];
        ans = max(ans, csum);
        c++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 9;
const double ep = 1e-7;
const int mod = 998244353;
const double pi = acos(-1);
const int INF = -1e9;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
typedef long long ll;
ll n, k, p[maxn], c[maxn], ans = -1e9;
int main() {
  scanf("%lld%lld", &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++) {
    ll now = i, num = 0, sum = 0;
    do {
      now = p[now];
      sum += c[now];
      num++;
    } while (now != i);
    now = i;
    ll ss = 0;
    for (int j = 1; j <= min(k, num); j++) {
      now = p[now];
      ss += c[now];
      ans = max(ans, ss);
      ans = max(ans, ss + sum * ((k - j) / num));
    }
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e2 + 9;
const double ep = 1e-7;
const int mod = 998244353;
const double pi = acos(-1);
const int INF = -1e9;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
typedef long long ll;
ll n, k, p[maxn], c[maxn], ans = -1e9;
int main() {
  scanf("%lld%lld", &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++) {
    ll now = i, num = 0, sum = 0;
    do {
      now = p[now];
      sum += c[now];
      num++;
    } while (now != i);
    now = i;
    ll ss = 0;
    for (int j = 1; j <= min(k, num); j++) {
      now = p[now];
      ss += c[now];
      ans = max(ans, ss);
      ans = max(ans, ss + sum * ((k - j) / num));
    }
  }
  printf("%lld\\n", ans);
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

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

  ll sum = 0;
  rep(i, n) sum += c[i];

  vector<vector<ll>> sim(n, vector<ll>(n + 1));
  rep(i, n) rep(j, n + 1) sim[i][j] = 0;

  // 最初iマス目に置いた場合
  rep(i, n) {
    // j回操作を繰り返した場合
    ll pos = i;
    for (ll j = 0; j < n; j++) {
      pos = p[pos];
      sim[i][j + 1] = sim[i][j] + c[pos];
    }
  }
  /*
          rep(i,n){
          for(ll j=1;j<n+1;j++){
              cout << sim[i][j] << " ";
          }
        cout << endl;
      }
  */

  ll ans = -1e18;
  rep(i, n) {
    // 頂点iから始まるサイクルを1周分調べる
    int pos = i;
    vector<ll> s;
    ll sum = 0;
    while (true) {
      pos = p[pos];
      s.push_back(c[pos]);
      sum += c[pos];
      if (pos == i)
        break;
    }

    // サイクルをどこまで進めるべきかを探索
    int len = s.size();
    ll tmp = 0;
    rep(j, len) {
      tmp += s[j];
      // サイクルの個数 > kならループしない
      if (j + 1 > k)
        break;
      ll now = tmp;
      if (sum > 0) {
        ll times = (k - (j + 1)) / len;
        now += times * sum;
      }
      ans = max(ans, now);
    }
  }

  cout << ans << endl;

  return (0);
}

Error: 0

Fixed submission:

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

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

  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    sum += c[i];
  }
  // cout << sum << endl;

  vector<vector<ll>> sim(n, vector<ll>(n + 1));
  rep(i, n) rep(j, n) sim[i][j] = 0;
  // 最初iマス目に置いた場合
  rep(i, n) {
    // j回操作を繰り返した場合
    ll res = 0;
    ll pos = i;
    for (ll j = 0; j < n; j++) {
      pos = p[pos];
      sim[i][j + 1] = sim[i][j] + c[pos];
    }
  }

  ll ans = -1e18;
  if (k <= n || sum < 0) {
    rep(i, n) {
      for (ll j = 1; j <= k; j++) {
        ans = max(ans, sim[i][j]);
      }
    }
    cout << ans << endl;
  } else {
    ll ans1 = -1e18;
    /*
    rep(i,n){
      for(ll j=1;j<=n;j++){
        ans1 = max(ans1,sim[i][j]);
      }
    }
    ans1 += sum * (k/n-1);
    */

    ll rem = k % n;
    ll ans2 = -1e18;
    // cout << rem << endl;
    rep(i, n) {
      for (ll j = 1; j <= rem; j++) {
        ans2 = max(ans2, sim[i][j]);
      }
    }
    ans2 += sum * (k / n);

    // cout << ans1 << " " << ans2 << endl;
    /*
    if(ans1 > ans2){
      cout << ans1 << endl;
    }else{
      cout << ans2 << endl;
    }
    */
    cout << ans2 << endl;
  }

  /*
  rep(i,n){
    rep(j,n){
      cout << sim[i][j] << " ";
    }
    cout << endl;
  }
  */
  return (0);
}