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

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<ll> c(n);
  for (auto &e : p) {
    cin >> e;
    e--;
  }
  for (auto &e : c)
    cin >> e;

  vector<ll> s1(n), s2(n), d1(n), d2(n);
  for (int i = 0; i < n; i++) {
    if (d1[i])
      continue;
    d1[i] = 1;
    for (int j = i; !d2[p[j]]; j = p[j]) {
      if (!d1[p[j]]) {
        s1[p[j]] = s1[j] + c[p[j]];
        d1[p[j]] = d1[j] + 1;
      } else {
        s2[p[j]] = (d2[j] ? s2[j] : s1[j]) + c[p[j]];
        d2[p[j]] = (d2[j] ? d2[j] : d1[j]) + 1;
      }
    }
  }

  ll r = -1e18;
  for (int i = 0; i < n; i++) {
    ll ri = 0;
    for (int j = i, ki = k; ki;) {
      if (d2[j] && 2 * (d2[j] - d1[j]) <= ki) {
        ll t = ki / (d2[j] - d1[j]) - 1;
        ri += t * (s2[j] - s1[j]);
        ki -= t * (d2[j] - d1[j]);
      } else {
        ri += c[p[j]];
        ki--;
        j = p[j];
      }
      r = max(r, ri);
      ri = max(ri, 0ll);
    }
  }
  cout << r << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<ll> c(n);
  for (auto &e : p) {
    cin >> e;
    e--;
  }
  for (auto &e : c)
    cin >> e;

  vector<ll> s1(n), s2(n), d1(n), d2(n);
  for (int i = 0; i < n; i++) {
    if (d1[i])
      continue;
    d1[i] = 1;
    for (int j = i; !d2[p[j]]; j = p[j]) {
      if (!d1[p[j]]) {
        s1[p[j]] = s1[j] + c[p[j]];
        d1[p[j]] = d1[j] + 1;
      } else {
        s2[p[j]] = (d2[j] ? s2[j] : s1[j]) + c[p[j]];
        d2[p[j]] = (d2[j] ? d2[j] : d1[j]) + 1;
      }
    }
  }

  ll r = -1e18;
  for (int i = 0; i < n; i++) {
    ll ri = 0;
    for (int j = i, ki = k; ki;) {
      if (d2[j] && 2 * (d2[j] - d1[j]) <= ki) {
        ri += s2[j] - s1[j];
        ki -= d2[j] - d1[j];
      } else {
        ri += c[p[j]];
        ki--;
        j = p[j];
      }
      r = max(r, ri);
      ri = max(ri, 0ll);
    }
  }
  cout << r << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
using namespace std;

ll n, k;
int main() {
  // freopen("input.txt", "r", stdin) ;
  // freopen("output.txt", "w", stdout) ;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k;
  vector<ll> 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];
  ll maxv = -10000000000000000;
  for (int i = 0; i < n; i++) {
    int iter = i;
    ll seq_sum = 0;
    ll seq_cnt = 0;
    while (true) {
      ++seq_cnt;
      seq_sum += c[iter];
      iter = p[iter];
      if (iter == i)
        break;
    }
    ll one = 0;
    ll cnt = 0;
    while (true) {
      cnt++;
      one += c[iter];
      if (cnt > k)
        break;
      ll num = (k - cnt) / seq_cnt;
      ll score = one + max(0ll, seq_sum) * num;
      maxv = max(maxv, score);
      iter = p[iter];
      if (iter == i)
        break;
    }
  }
  cout << maxv;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
using namespace std;

ll n, k;
int main() {
  // freopen("input.txt", "r", stdin) ;
  // freopen("output.txt", "w", stdout) ;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k;
  vector<ll> p(n);
  vector<ll> c(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  ll maxv = -10000000000000000;
  for (int i = 0; i < n; i++) {
    ll score = 0;
    ll cor = i;
    for (int j = 0; j < k; j++) {
      cor = p[cor] - 1;
      score += c[cor];
      if (maxv < score)
        maxv = score;
    }
  }
  cout << maxv;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// D - Moving Piece
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n), vi(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];

  long long ans = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    vector<int> vi(n);
    vector<long long> a;
    long long t = LLONG_MIN;
    for (int j = 0, x = i; j < min(n, k) && !vi[x]; j++, x = p[x] - 1) {
      vi[x] = 1;
      a.push_back(c[p[x] - 1] + (a.empty() ? 0 : a.back()));
      t = max(t, a.back());
    }
    long long z = k / a.size();
    if (z > 0 && a.back() > 0) {
      t += (z - 1) * a.back();
      t = max(t, z * a.back());
      for (int i = 0; i < k % a.size(); i++)
        t = max(t, z * a.back() + a[i]);
    }
    ans = max(ans, t);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// D - Moving Piece
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n), vi(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];

  long long ans = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    if (vi[i])
      continue;
    vector<long long> a(k);
    int x = i;
    for (int j = 0; j < k; j++) {
      a[j] = c[p[x] - 1] + (j > 0 ? a[j - 1] : 0);
      ans = max(ans, a[j]);
      x = p[x] - 1;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int n, k;
  cin >> n >> k;
  int p[n], c[n];
  for (int &i : p) {
    cin >> i;
    i--;
  }
  for (int &i : c)
    cin >> i;

  ll ans = -1e9 - 1;
  for (int i = 0; i < n; i++) {
    int square = i, cycleSize = 0;
    ll cycleSum = 0;
    do {
      cycleSize++;
      cycleSum += c[square];
      square = p[square];
    } while (square != i);

    int moves = 0;
    ll path = 0;
    square = i;
    do {
      if (moves >= k)
        break;
      moves++;
      square = p[square];
      path += c[square];
      int multiplier = (k - moves) / cycleSize;
      ll score = path + max(0ll, cycleSum) * multiplier;
      ans = max(ans, score);
    } while (square != i);
  }

  cout << ans << nl;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

ll dfs(int i, int k, vector<vector<ll>> &dp, int *c, int *p) {
  if (!k)
    return dp[i][k] = c[i];
  if (dp[i][k] != INT_MIN)
    return dp[i][k];

  dp[i][k] = max((ll)c[i], c[i] + dfs(p[i], k - 1, dp, c, p));
  return dp[i][k];
}

void solve() {
  int n, k;
  cin >> n >> k;
  int p[n], c[n];
  for (int &i : p) {
    cin >> i;
    i--;
  }
  for (int &i : c)
    cin >> i;

  vector<vector<ll>> dp(n, vector<ll>(k + 1, INT_MIN));
  ll score = INT_MIN;

  for (int i = 0; i < n; i++) {
    score = max(score, dfs(p[i], k - 1, dp, c, p));
  }

  cout << score;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // }

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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 5000 + 5;
const int MAX_M = 100000 + 5;
const int INF = 0x7fffffff;
const int inf = 1000000000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
int n, k;
int p[MAX_N];
int c[MAX_N];
int main() {
  cin >> n >> k;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  ll ans = -INF;
  for (i = 1; i <= n; i++) {
    ll sum = 0;
    int j;
    vector<ll> v;
    for (j = p[i]; j != i; j = p[j]) {
      sum += (ll)c[j];
      v.pb(sum);
    }
    sum += (ll)c[i];
    v.pb(sum);
    int len = v.size();
    for (j = 0; j < len; j++) {
      ll rest = k - j - 1;
      if (rest < 0)
        break;
      ans = max(ans, v[j] + (rest / len) * max(sum, (ll)0));
    }
  }
  cout << ans << endl;
  // system("pause");
}

Error: 0
sh: 1: pause: not found


Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 3000 + 5;
const int MAX_M = 100000 + 5;
const int INF = 0x7fffffff;
const int inf = 1000000000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
int n, k;
int p[MAX_N];
int c[MAX_N];
int main() {
  cin >> n >> k;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  ll ans = -INF;
  for (i = 1; i <= n; i++) {
    ll sum = 0;
    int j;
    vector<ll> v;
    for (j = p[i]; j != i; j = p[j]) {
      sum += (ll)c[j];
      v.pb(sum);
    }
    sum += (ll)c[i];
    v.pb(sum);
    int len = v.size();
    for (j = 0; j < len; j++) {
      ll rest = k - j - 1;
      if (rest < 0)
        break;
      ans = max(ans, v[j] + (rest / len) * max(sum, (ll)0));
    }
  }
  cout << ans << endl;
  system("pause");
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1000000007;
const int INF = 1001001001;
int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> 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];
  }

  vector<int64_t> mx(N + 1);
  int64_t ans = -INF;
  for (int i = 1; i <= N; i++) {
    int64_t r = i, MAX = -INF, cost = 0;
    vector<int64_t> d(N + 1);
    vector<bool> seen(N + 1, false);
    for (int64_t j = 1; j <= K; j++) {
      if (seen[r]) {
        if (cost >= 0) {
          int64_t x = cost * (K / (j - 1)) + d[K % (j - 1)];
          int64_t y = cost * (K / (j - 1) - 1) + MAX;
          mx[i] = max(x, y);
        }
        break;
      } else {
        cost += C[P[r]];
        chmax(MAX, cost);
        seen[r] = true;
        r = P[r];
        mx[i] = MAX;
        d[j] = MAX;
      }
    }
    chmax(ans, mx[i]);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1000000007;
const int INF = 1001001001;
int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> 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];
  }

  vector<int64_t> mx(N + 1);
  int64_t ans = -INF;
  for (int i = 1; i <= N; i++) {
    int64_t r = i, MAX = -INF, cost = 0;
    vector<int64_t> d(K + 1);
    vector<bool> seen(N + 1, false);

    for (int64_t j = 1; j <= K; j++) {
      if (seen[r]) {
        mx[i] = cost * (K / (j - 1)) + d[K % (j - 1)];
        break;
      } else {
        cost += C[P[r]];
        chmax(MAX, cost);
        seen[r] = true;
        r = P[r];
        mx[i] = MAX;
      }
      d[j] = MAX;
    }
    chmax(ans, mx[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 10000000000;
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];
  if (k <= n) {
    ll ans = -INF;
    for (int i = 0; i < n; i++) {
      ll P = i, s = 0;
      for (int j = 0; j < k; j++) {
        P = p[P];
        s += c[P];
        ans = max(ans, s);
      }
    }
    cout << ans << endl;
  } else {
    vector<vector<int>> next(31, vector<int>(n));
    for (int i = 0; i < n; i++)
      next[0][i] = p[i];
    for (int i = 1; i < 31; i++)
      for (int j = 0; j < n; j++)
        next[i][j] = next[i - 1][next[i - 1][j]];
    vector<vector<ll>> sum(31, vector<ll>(n));
    for (int i = 0; i < n; i++)
      sum[0][i] = c[p[i]];
    for (int i = 1; i < 31; i++)
      for (int j = 0; j < n; j++)
        sum[i][j] = sum[i - 1][j] + sum[i - 1][next[i - 1][j]];
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ll P = i;
      ll s = 0;
      for (int j = 0; j < n; j++) {
        P = p[P];
        s += c[P];
        ans = max(ans, s);
      }
      P = i;
      s = 0;
      for (int j = 0; j < 31; j++) {
        if ((k - n) >> j & 1) {
          s += sum[j][P];
          P = next[j][P];
        }
      }
      ans = max(ans, s);
      for (int j = 0; j < n; j++) {
        P = p[P];
        s += c[P];
        ans = max(ans, s);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e3 + 10;
const ll INF = -1e13;
ll p[N], c[N];
ll n, k;
int main() {
  cin >> n >> k;
  vector<ll> p(N);
  for (int i = 0; i < n; i++)
    cin >> p[i], p[i]--;
  vector<ll> c(N);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<int> visit(N);
  vector<vector<int>> vv;
  for (int i = 0; i < n; i++)
    if (!visit[i]) {
      vector<int> v;
      v.push_back(i);
      int pt = p[i];
      while (pt != i) {
        v.push_back(pt);
        pt = p[pt];
      }
      vv.push_back(v);
    }
  ll ans = -1e18;
  for (int i = 0; i < vv.size(); i++) {
    int sz = vv[i].size();
    for (int r = 0; r < sz; r++) {
      vector<ll> s(sz + 1);
      for (int j = 0; j < sz; j++)
        s[j + 1] = s[j] + c[vv[i][j]];
      for (int t = 0; t < sz; t++) {
        ll lmn = (t ? 0 : 1);
        ll lmx = (k + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        ll l = (s[sz] > 0 ? lmx : lmn);
        ll res = l * s[sz] + s[t];
        ans = max(ans, res);
      }
      rotate(vv[i].begin(), vv[i].begin() + 1, vv[i].end());
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (auto &i : p)
    cin >> i;
  for (auto &i : p)
    i--;
  vector<ll> c(n);
  for (auto &i : c)
    cin >> i;

  vector<vector<int>> st(n); // iから始めた時の順列
  for (int i = 0; i < n; i++) {
    int now = i;
    for (int j = 0; j < n; j++) {
      now = p[now];
      st[i].push_back(c[now]);
      if (now == i)
        break;
    }
  }
  ll ans = INT_MIN;

  for (int i = 0; i < n; i++) {
    ll nowsum = 0;
    ll turnsum = 0;
    for (int j = 0; j < st[i].size(); j++)
      turnsum += st[i][j];

    for (int j = 0; j < st[i].size(); j++) {
      nowsum += st[i][j];
      ll nowk = k - j - 1;
      ll nowt = nowk / (ll)st[i].size();
      ll nowans = 0;
      if (turnsum > 0)
        nowans += nowt * turnsum;
      if (nowk < 0)
        continue;
      // cout<<i<<' ' <<j<<';'<<nowsum+nowans<<endl;
      ans = max(ans, nowsum + nowans);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (auto &i : p)
    cin >> i;
  for (auto &i : p)
    i--;
  vector<ll> c(n);
  for (auto &i : c)
    cin >> i;

  vector<vector<int>> st(n); // iから始めた時の順列
  for (int i = 0; i < n; i++) {
    int now = i;
    for (int j = 0; j < k; j++) {
      now = p[now];
      st[i].push_back(c[now]);
    }
  }
  ll ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    ll nowsum = 0;
    for (int j = 0; j < k; j++) {

      nowsum += st[i][j];
      // cout<<nowsum<<endl;
      ans = max(ans, nowsum);
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll INF = 1LL << 60;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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 i = 0; i < N; i++) {
    int start = i;
    int now = P[start];
    ll lans = 0;
    int cnt = 1;
    for (; cnt <= K; cnt++) {
      lans += C[now];
      chmax(ans, lans);
      if (now == start)
        break;
      now = P[now];
    }
    chmax(ans, lans);
    if (K % cnt != 0) {
      lans *= K / cnt;
      cnt = K - (K % cnt) + 1;
    } else {
      lans *= K / cnt - 1;
      cnt = K - (K % cnt) - cnt + 1;
    }

    for (; cnt <= K; cnt++) {
      now = P[now];
      lans += C[now];
      chmax(ans, lans);
    }
    chmax(ans, lans);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

const ll INF = 1LL << 60;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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 i = 0; i < N; i++) {
    int start = i;
    int now = P[start];
    ll lans = 0;
    int cnt = 0;
    for (; cnt < K; cnt++) {
      lans += C[now];
      chmax(ans, lans);
      now = P[now];
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define LL long long
using namespace std;
const int N = 5010;
LL sum[N], a[N];
int p[N];
int main() {
  int n;
  LL k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  LL ans = -1e18 - 1;
  for (int i = 1; i <= n; i++) {
    int x = p[i];
    LL cnt = 1;
    int flag = 0;
    while (x != i) {
      sum[cnt] = sum[cnt - 1] + a[x];
      x = p[x];
      cnt++;
    }
    sum[cnt] = sum[cnt - 1] + a[i];
    // for(int j = 1; j <= cnt; j++)
    //	cout<< sum[j] << " ";
    // cout<<endl;
    for (int j = 1; j <= min(k, cnt); j++) {
      LL nk = k - j;
      if (sum[cnt] >= 0)
        ans = max(ans, sum[cnt] * (nk / cnt) + sum[j]);
      else
        ans = max(ans, sum[j]);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define LL long long
using namespace std;
const int N = 5010;
LL sum[N], a[N];
int p[N];
int main() {
  int n;
  LL k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  LL ans = -1e18 - 1;
  for (int i = 1; i <= n; i++) {
    int x = p[i];
    int cnt = 1;
    int flag = 0;
    while (x != i) {
      sum[cnt] = sum[cnt - 1] + a[x];
      x = p[x];
      cnt++;
    }
    sum[cnt] = sum[cnt - 1] + a[i];
    // for(int j = 1; j <= cnt; j++)
    //	cout<< sum[j] << " ";
    // cout<<endl;
    LL now = 0;
    if (sum[cnt] >= 0) {
      now = sum[cnt] * (k / cnt);
      flag = 1;
    }
    int nk;
    if (flag)
      nk = k % cnt;
    else
      nk = max((int)k, cnt);
    // printf("%d\\n",nk);
    if (flag)
      ans = max(ans, now);
    for (int j = 1; j <= nk; j++)
      ans = max(ans, now + sum[j]);
  }
  cout << ans << endl;
  return 0;
}