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

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  ll ans = -1e18;
  for (int i = 0; i < n; ++i) {
    int pos = i, cyc = 0;
    fill(all(w), 0);
    for (int j = 1; j <= n; ++j) {
      pos = p[pos];
      w[j] = w[j - 1] + c[pos];
      cyc++;
      if (pos == i)
        break;
    }
    ll max_all = *max_element(&w[1], &w[cyc + 1]);
    ll max_mod = *max_element(&w[1], &w[k % cyc + 1]);
    ll t = max_mod;
    if (k >= cyc) {
      chmax(t, max_all);
      chmax(t, (k / cyc) * w[cyc] + (k % cyc == 0 ? 0LL : max(0LL, max_mod)));
      chmax(t, ((k / cyc) - 1) * w[cyc] + max_all);
    }
    chmax(ans, t);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int n;
ll k;
vector<ll> p, c;

ll solve1() { // high speed
  vector<ll> w(n + 1);
  ll ans = -1e18;
  for (int i = 0; i < n; ++i) {
    int pos = i, cyc = 0;
    fill(all(w), 0);
    for (int j = 1; j <= n; ++j) {
      pos = p[pos];
      w[j] = w[j - 1] + c[pos];
      cyc++;
      if (pos == i)
        break;
    }
    ll max_all = *max_element(&w[1], &w[cyc + 1]);
    ll max_mod = *max_element(&w[1], &w[k % cyc + 1]);
    ll t = max_mod;
    if (k >= cyc) {
      chmax(t, (k / cyc) * w[cyc] + (k % cyc == 0 ? 0LL : max(0LL, max_mod)));
      chmax(t, ((k / cyc) - 1) * w[cyc] + max_all);
    }
    chmax(ans, t);
  }
  return ans;
}

ll solve2() { // slow speed
  ll ans = -1e18;
  for (int i = 0; i < n; ++i) {
    int pos = i;
    ll t = 0;
    for (int j = 0; j < k; ++j) {
      pos = p[pos];
      t += c[pos];
      chmax(ans, t);
    }
  }
  return ans;
}

void input() {
  cin >> n >> k;
  p.resize(n);
  c.resize(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }
}

int main() {
  input();

  // cout << solve1() << endl;
  cout << solve2() << endl;

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

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

typedef long long ll;
const ll INF = 1e18;

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

  vector<ll> P(N + 1), 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 = -INF;
  for (int s = 1; s <= N; ++s) {
    // find loop
    ll tot = 0, len = 0;
    int x = s;
    do {
      tot += C[x];
      len++;
    } while ((x = P[x]) != s);

    // search max
    ll now = 0;
    for (int i = 1; i <= len; ++i) {
      x = P[x];
      now += C[x];
      if (i > K)
        break;
      ll sc = now;
      if (tot > 0)
        sc += ((K - i) / len) * tot;
      ans = max(ans, sc);
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
const ll INF = 1000000000000000;

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

  vector<ll> P(N + 1), 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 = -INF;
  for (int s = 1; s <= N; ++s) {
    ll score = 0;
    ll k = 0;
    int x = s;

    vector<bool> visited(N + 1, false);
    while (k < K && !visited[x]) {
      ++k;
      x = P[x];
      score += C[x];
      ans = max(ans, score);
    }

    if (k == K)
      continue;

    ll lp_len = k;
    ll lp_score = score;

    k = 0;
    score = 0;
    if (lp_score > 0) {
      score = lp_score * (K / lp_len);
      k = lp_len * (K / lp_len);
    }
    for (int i = 0; i < lp_len; ++i) {
      ++k;
      x = P[x];
      score += C[x];
      ans = max(ans, score);
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
ll c[maxn], p[maxn], b[maxn], n, k, ans = -999999999999, t, cnt, vis[maxn],
                                    inf = 0;
int main() {

  // freopen("input.in", "r", stdin);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll temp;
  for (int i = 1; i <= n; i++) {
    memset(b, 0, sizeof(b));
    memset(vis, 0, sizeof(vis));
    cnt = 0, temp = 0, t = i, inf = 0;
    while (vis[p[t]] == 0)
      b[cnt + 1] = c[p[t]], inf += c[p[t]], cnt++, vis[p[t]] = 1, t = p[t];
    if (k <= cnt) {
      ll ab = 0;
      for (int j = 1; j <= k; j++) {
        ab += b[j];
        ans = max(ans, ab);
      }
      continue;
    }
    if (inf <= 0) {
      ll ab = 0;
      for (int j = 1; j <= cnt; j++) {
        ab += b[j];
        ans = max(ans, ab);
      }
    }
    {
      int kk = k / cnt;
      kk--;
      temp = (kk * inf);
      ans = max(temp, ans);
      for (int j = 1; j <= cnt; j++) {
        temp = temp + b[j];
        ans = max(temp, ans);
      }
      kk++;
      for (int j = 1; j <= k - kk * cnt; j++) {
        temp = temp + b[j];
        ans = max(temp, ans);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
ll c[maxn], p[maxn], b[maxn], n, k, ans = -999999999999, t, cnt, vis[maxn],
                                    inf = 0;
int main() {

  // freopen("input.in", "r", stdin);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll temp;
  for (int i = 1; i <= n; i++) {
    memset(b, 0, sizeof(b));
    memset(vis, 0, sizeof(vis));
    cnt = 0, temp = 0, t = i, inf = 0;
    while (vis[p[t]] == 0)
      b[cnt + 1] = c[p[t]], inf += c[p[t]], cnt++, vis[p[t]] = 1, t = p[t];
    if (k <= cnt || inf <= 0) {
      ll ab = 0;
      for (int j = 1; j <= k; j++) {
        ab += b[j];
        ans = max(ans, ab);
      }
    } else {
      int kk = k / cnt;
      kk--;
      temp = (kk * inf);
      ans = max(temp, ans);
      for (int j = 1; j <= cnt; j++) {
        temp = temp + b[j];
        ans = max(temp, ans);
      }
      kk++;
      for (int j = 1; j <= k - kk * cnt; j++) {
        temp = temp + b[j];
        ans = max(temp, ans);
      }
    }
  }
  cout << ans << endl;
  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 < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define _GLIBCXX_DEBUG

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

typedef long long ll;
const int max_n = 5010;
const ll INF = 1e15;
ll n, ans = -INF;
int k;
int p[max_n];
ll c[max_n];

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

  rep(i, n) {
    vector<int> v;
    int now = i;
    while (true) {
      now = p[now];
      v.push_back(c[now]);
      if (now == i)
        break;
    }
    int sz = v.size();
    ll sum = accumulate(v.begin(), v.end(), 0LL);
    ll cur = 0;
    rep(j, min(sz, k)) {
      cur += v[j];
      ans = max(ans, cur + (k - j - 1) / sz * max(sum, 0LL));
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

typedef long long ll;
const int max_n = 5010;
const ll INF = 1e15;
ll n, k, ans = -INF;
int p[max_n];
ll c[max_n];

void dfs(int n, int m, int sum) {
  if (ans < sum)
    ans = sum;
  if (m == 0)
    return;
  dfs(p[n] - 1, m - 1, sum + c[p[n] - 1]);
  return;
}

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

  rep(i, n) {
    vector<int> v;
    int now = i;
    while (true) {
      now = p[now];
      v.push_back(c[i]);
      if (now == i)
        break;
    }
    int sz = v.size();
    auto sum = accumulate(v.begin(), v.end(), 0);
    ll cur = 0;
    for (int j = 1; j <= k; j++) {
      if (j > k)
        break;
      cur += v[j - 1];
      ans = max(ans, cur + (k - j) / sz * max(sum, 0));
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
int main() {
  ll ans = -99999999999;
  ll n, k;
  std::cin >> n >> k;
  std::vector<ll> P;
  std::vector<ll> P_inv(n, 0);
  std::vector<ll> C;
  std::vector<ll> S(n, 0);
  std::vector<ll> S_temp(n, 0);
  std::vector<std::array<ll, 2>> T(n, {0, 0});
  for (ll i = 0; i < n; ++i) {
    ll temp = 0;
    std::cin >> temp;
    P.push_back(temp - 1);
    // P_inv[temp - 1] = i;
  }
  /*for (auto v : P) {
      std::cout << v << std::endl;
  }*/
  for (ll i = 0; i < n; ++i) {
    ll temp = 0;
    std::cin >> temp;
    C.push_back(temp);
  }

  for (ll i = 0; i < n; ++i) {
    T[i][1] = i;
  }
  for (ll i = 0; i < n; ++i) {
    ll count = 0;
    do {
      ++count;
      // std::cout << T[i][1] << P[(T[i][1])] << std::endl;
      T[i][1] = P[(T[i][1])];
      // std::cout << T[i][1] << std::endl;
      T[i][0] += C[(T[i][1])];
      // std::cout << "a" << count << " " << i << " " << T[i][0] << " " <<
      // T[i][1] << std::endl;
      ans = std::max(ans, T[i][0]);
    } while (!(T[i][1] == i || count == k));
    if (k > count) {
      ll rev = (k / count) - 1;

      count = count * rev;
      T[i][0] *= rev;
      // std::cout << "b" << count << " " << i << " " << T[i][0] << " " <<
      // T[i][1] << std::endl;
      ans = std::max(ans, T[i][0]);
    }
    while (count != k) {
      ++count;
      T[i][1] = P[(T[i][1])];
      T[i][0] += C[(T[i][1])];
      // std::cout << "c" << count << " " << i << " " << T[i][0] << " " <<
      // T[i][1] << std::endl;
      ans = std::max(ans, T[i][0]);
    }
  }

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

/*
for (ll j = 0; j < k; ++j) {
    for (ll i = 0; i < n; ++i) {
        S_temp[i] = S[P_inv[i]] + C[i];
    }
    for (ll i = 0; i < n; ++i) {
        S[i] = S_temp[i];
    }
    for (ll i = 0; i < n; ++i) {


}


*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
int main() {
  ll n, k;
  std::cin >> n >> k;
  std::vector<ll> P;
  std::vector<ll> P_inv(n, 0);
  std::vector<ll> C;
  std::vector<ll> S(n, 0);
  std::vector<ll> S_temp(n, 0);

  for (ll i = 0; i < n; ++i) {
    ll temp = 0;
    std::cin >> temp;
    P.push_back(temp - 1);
    P_inv[temp - 1] = i;
  }
  for (ll i = 0; i < n; ++i) {
    ll temp = 0;
    std::cin >> temp;
    C.push_back(temp);
  }
  ll ans = -99999999999;
  for (ll j = 0; j < k; ++j) {
    for (ll i = 0; i < n; ++i) {
      S_temp[i] = S[P_inv[i]] + C[i];
    }
    for (ll i = 0; i < n; ++i) {
      S[i] = S_temp[i];
    }
    for (ll i = 0; i < n; ++i) {

      ans = std::max(ans, S.at(i));
    }
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double

int main(void) {
  ll n, k, max = -10000000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n), sum(n), num(n, 0);
  vector<vector<ll>> memo(n, vector<ll>(0));
  for (ll i = 0; i < n; i++) {
    cin >> p.at(i);
    p.at(i)--;
  }
  for (ll i = 0; i < n; i++) {
    cin >> c.at(i);
  }

  for (ll i = 0; i < n; i++) {
    ll wa = 0, roop = i;
    vector<bool> tootta(n, false);
    while (1) {
      wa += c.at(roop);
      memo.at(i).push_back(wa);
      tootta.at(roop) = true;
      if (tootta.at(p.at(roop))) {
        break;
      }
      roop = p.at(roop);
    }

    sum.at(i) = wa;

    if (sum.at(i) <= 0) {
      for (ll j = 0; j < min((ll)memo.at(i).size(), k); j++) {
        if (max < memo.at(i).at(j)) {
          max = memo.at(i).at(j);
        }
      }
    } else {
      for (ll j = 0; j < memo.at(i).size(); j++) {
        ll ij = sum.at(i) * (k / memo.at(i).size() - 1) + memo.at(i).at(j);
        if (max < ij) {
          max = ij;
        }
      }
      for (ll j = 0; j < (k % memo.at(i).size()); j++) {
        ll ij = sum.at(i) * (k / memo.at(i).size()) + memo.at(i).at(j);
        if (max < ij) {
          max = ij;
        }
      }
    }
  }

  cout << max;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector<bool>::_M_range_check: __n (which is 5) >= this->size() (which is 5)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long

int main(void) {
  ll n, k, max = -1000000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  for (ll i = 0; i < n; i++) {
    cin >> p.at(i);
  }
  for (ll i = 0; i < n; i++) {
    cin >> c.at(i);
  }

  vector<vector<ll>> memo(n, vector<ll>(0));

  for (ll i = 0; i < n; i++) {
    vector<bool> tootta(n, true);
    vector<ll> summ(n, 0);
    ll sum = 0, start = i, roop = 0;
    while (1) {
      tootta.at(start) = false;
      if (tootta.at(p.at(start)) != 1)
        break;
      sum += c.at(p.at(start));
      if (memo.at(i).size() == 0)
        memo.at(i).push_back(c.at(p.at(start)));
      else {
        memo.at(i).push_back(memo.at(i).at(roop) + c.at(p.at(start)));
        roop++;
      }
      start = p.at(start);
    }

    if (sum <= 0)
      for (ll j = 0; j < k % memo.at(i).size(); j++) {
        if (memo.at(i).at(j) > max) {
          max = memo.at(i).at(j);
        }
      }

    else {
      for (ll j = 0; j < k % memo.at(i).size(); j++) {
        if (memo.at(i).at(j) > max) {
          if (sum * (k / memo.at(i).size()) + memo.at(i).at(j) > max)
            max = sum * (k / memo.at(i).size()) + memo.at(i).at(j);
        }
      }
    }
  }
  cout << max;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

ll N, K;

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> K;
  vector<ll> P(N);
  REP(i, N) cin >> P[i], --P[i];
  vector<ll> C(N);
  REP(i, N) cin >> C[i];

  ll res{-INF};
  REP(init, N) {
    ll co = init;
    vector<ll> sum;
    ll t{0};
    while (true) {
      co = P[co];
      t += C[co];
      sum.push_back(t);
      if (init == co)
        break;
    }

    ll M = sum.size();

    if (M >= K) {
      res = max(res, *max_element(sum.begin(), sum.begin() + K));
    } else // M < K
    {
      ll p = K / M;
      if (sum.back() > 0) {
        ll b = sum.back();
        if (p > 1)
          res = max(res, (p - 1) * b + *max_element(ALL(sum)));

        if (K % M)
          res =
              max(res, p * b + *max_element(sum.begin(), sum.begin() + K % M));
        else
          res = max(res, p * b);
      } else {
        res = max(res, *max_element(ALL(sum)));
      }
    }
  }

  std::cout << res << std::endl;

  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

ll N, K;

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> K;
  vector<ll> P(N);
  REP(i, N) cin >> P[i], --P[i];
  vector<ll> C(N);
  REP(i, N) cin >> C[i];

  ll res{-INF};
  REP(init, N) {
    ll co = init;
    vector<ll> sum;
    ll t{0};
    while (true) {
      t += C[co];
      sum.push_back(t);
      co = P[co];
      if (init == co)
        break;
    }

    ll M = sum.size();

    if (sum.back() > 0 && K >= M) {
      ll q = K / M;
      if (q >= 1) {
        ll mx = -INF;
        if (q > 1)
          mx = 0;
        mx = max(mx, *max_element(ALL(sum)));
        res = max(res, (q - 1) * sum.back() + mx);
      }

      {
        ll r = K % M;
        ll mx = 0;
        mx = *max_element(sum.begin(), sum.begin() + r);
        res = max(res, q * sum.back() + mx);
      }
    } else {
      res = max(res, *max_element(sum.begin(), sum.begin() + K));
    }
  }

  std::cout << res << std::endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n, k;
  cin >> n >> k;
  int p[n];
  int 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 maxval = -1e18;
  for (int i = 0; i < n; i++) {
    ll sum = c[i], period = 1;
    int j = p[i];
    vector<ll> c2;
    c2.push_back(c[i]);

    while (j != i) {

      sum += c[j];
      c2.push_back(sum);
      j = p[j];
      period++;
    }
    for (int l = 0; l < c2.size(); l++) {
      if (k - l - 1 < 0)
        break;
      int rest = k - l - 1;
      if (c2[l] + max(sum, 0LL) * (rest / period) > maxval)
        maxval = c2[l] + max(sum, 0LL) * (rest / period);
    }
  }

  cout << maxval << "\\n";
}

Error: TLE

Fixed submission:

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

  ll sums[n];
  int times[n];
  ll cyclel[n];
  ll cyclev[n];
  for (int s = 0; s < n; s++) {
    sums[s] = -1000000009;
    cyclel[s] = n + 1;
    for (int count = 1; count <= min(n, k); count++) {
      int x = count, val = s, preval = -1;
      ll tsum = 0;
      while (x > 0 && preval != val) {
        preval = s;
        val = p[val] - 1;
        tsum += c[val];
        x--;
        if (val == preval)
          cyclel[s] = count - x;
        cyclev[s] = tsum;
      }
      if (sums[s] < tsum) {
        sums[s] = tsum;
        times[s] = count;
      }
    }
  }
  ll maxval = -1000000009;
  for (int i = 0; i < n; i++) {
    ll cyclesum = 0;
    if (cyclel[i] != n + 1 && k > cyclel[i]) {
      ll partsum = 0, str = i;
      for (int x = 0; x < k % cyclel[i]; x++) {
        str = p[str] - 1;
        partsum += c[str];
      }
      cyclesum = cyclev[i] * (k / cyclel[i]) + partsum;
    }
    if (cyclesum == 0) {
      if (maxval < sums[i])
        maxval = sums[i];
    } else if (max(sums[i], cyclesum) > maxval)
      maxval = max(sums[i], cyclesum);
  }
  cout << maxval << "\\n";
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong N, K;
  vector<llong> P, C;

  cin >> N >> K;
  P.resize(N + 1);
  C.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> P[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
  }

  llong ans = LLONG_MIN;
  for (int i = 1; i <= N; i++) {
    vector<llong> used(N + 1, 0);
    llong now = i;
    llong res = 0;
    llong cnt = 0;
    while (cnt < K && !used[now]) {
      used[now]++;
      cnt++;
      res += C[P[now]];
      now = P[now];
      ans = max(ans, res);
    }
    if (cnt < K && res > 0) {
      if (K % cnt == 0) {
        res += res * (K / cnt - 2);
        cnt += cnt * (K / cnt - 2);
        // cerr << res << " " << cnt << endl;
      } else {
        res *= K / cnt;
        cnt *= K / cnt;
      }
      ans = max(ans, res);
      while (cnt < K) {
        cnt++;
        res += C[P[now]];
        now = P[now];
        ans = max(ans, res);
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong N, K;
  vector<llong> P, C;

  cin >> N >> K;
  P.resize(N + 1);
  C.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> P[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
  }

  llong ans = LLONG_MIN;
  for (int i = 1; i <= N; i++) {
    vector<llong> used(N + 1, 0);
    llong now = i;
    llong res = 0;
    llong cnt = 0;
    while (cnt < K && !used[now]) {
      used[now]++;
      cnt++;
      res += C[P[now]];
      now = P[now];
      ans = max(ans, res);
    }
    if (cnt < K && res > 0) {
      return 1;
      res *= K / cnt;
      ans = max(ans, res);
      cnt *= K / cnt;
      while (cnt < K) {
        cnt++;
        res += C[P[now]];
        now = P[now];
        ans = max(ans, res);
      }
    }
  }

  cout << ans << endl;

  return 0;
}