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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int n, k;
int p[5000];
int c[5000];

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

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

  long long ans = *max_element(c, c + n);

  rep(i, n) {
    unordered_map<int, pair<int, long long>> vis;
    vis.reserve(n);
    int cur = i;
    long long s = 0;

    rep(j, min(2 * n, k)) {
      s += c[cur];
      cur = p[cur];

      ans = max(ans, s);

      auto it = vis.find(cur);
      if (it != vis.end()) {
        int cyc = j - it->second.first;
        long long cycs = s - it->second.second;

        ans = max(ans, s + (k - j - 1) / cyc * cycs);
      }
      vis[cur] = make_pair(j, s);
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int n, k;
int p[5000];
int c[5000];

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

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

  long long ans = *max_element(c, c + n);

  rep(i, n) {
    unordered_map<int, pair<int, long long>> vis;
    vis.reserve(n);
    int cur = i;
    long long s = 0;

    rep(j, k) {
      s += c[cur];
      cur = p[cur];

      ans = max(ans, s);

      auto it = vis.find(cur);
      if (it != vis.end()) {
        int cyc = j - it->second.first;
        long long cycs = s - it->second.second;

        ans = max(ans, s + (k - j - 1) / cyc * cycs);
      }
      vis[cur] = make_pair(j, s);
    }
  }

  cout << ans << endl;

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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
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;
}

vector<ll> P, C;
// 始点sから1~k回まで回した時の最大得点を返す
ll func(int s, ll k) {
  int index = s;
  ll ret = -LINF, score = 0;
  rep(i, k) {
    int nx = P[index];
    score += C[nx];
    chmax(ret, score);
    index = nx;
  }
  return ret;
}

int main() {
  ll n, k;
  cin >> n >> k;
  P.resize(n), C.resize(n);
  rep(i, n) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, n) cin >> C[i];

  ll res = -LINF;
  rep(i, n) {
    int index = i, cnt = 0;
    ll score = 0;
    while (1) {
      int nx = P[index];
      score += C[nx];
      index = nx;
      cnt++;
      if (nx == i)
        break; // 一周した
    }
    int r = cnt; // ループ長
    if (k < r) {
      chmax(res, func(i, k));
    } else {
      if (score >= 0) {
        if (k % r == 0) {
          ll tmp = score * (k / r - 1);
          chmax(res, tmp + func(i, r));
        } else {
          ll tmp = score * (k / r);
          chmax(res, tmp + func(i, k % r));
        }
      } else {
        chmax(res, func(i, r - 1));
      }
    }
  }
  cout << res << ln;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
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;
}

// 始点sから1~k回まで回した時の最大得点を返す
ll func(int s, ll k, vector<ll> P, vector<ll> C) {
  int index = s;
  ll ret = -LINF, score = 0;
  rep(i, k) {
    int nx = P[index];
    score += C[nx];
    chmax(ret, score);
    index = nx;
  }
  return ret;
}

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

  ll res = -LINF;
  rep(i, n) chmax(res, func(i, k, P, C));
  cout << res << ln;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> pes, color, ces, sz;
vector<ll> points;

void dfs(int u, int z) {
  color[u] = z;
  sz[color[u]]++;
  points[color[u]] += ces[u];
  int v = pes[u];
  if (color[v] == -1)
    dfs(v, z);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  ll k;
  cin >> n >> k;
  pes = ces = sz = vector<int>(n + 1);
  color.resize(n + 1, -1);
  points.resize(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> pes[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> ces[i];
  }
  int z = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == -1) {
      dfs(i, ++z);
    }
  }
  ll ans = -1e10;
  for (int i = 1; i <= n; i++) {
    int u = i;
    u = pes[u];
    ll tmp = 0;
    for (int len = 1; len <= sz[color[i]]; len++) {
      if (len > k)
        break;

      u = pes[u];
      tmp += ces[u];
      ans = max(ans, tmp);
      ll foo = (k - len) / sz[color[i]];
      ans = max(ans, tmp + foo * points[color[i]]);
    }
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> pes, color, ces, sz;
vector<ll> points;

void dfs(int u, int z) {
  color[u] = z;
  sz[color[u]]++;
  points[color[u]] += ces[u];
  int v = pes[u];
  if (color[v] == -1)
    dfs(v, z);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  ll k;
  cin >> n >> k;
  pes = ces = sz = vector<int>(n + 1);
  color.resize(n + 1, -1);
  points.resize(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> pes[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> ces[i];
  }
  int z = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == -1) {
      dfs(i, ++z);
    }
  }
  ll ans = -1e10;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (color[i] != color[j])
        continue;
      int len = 0;
      ll tmp = 0;
      int u = i;
      while (pes[u] != j) {
        u = pes[u];
        len++;
        tmp += ces[u];
      }
      tmp += ces[j];
      len++;
      if (len > k)
        continue;
      ans = max(ans, tmp);
      ll foo = (k - len) / sz[color[i]];
      ans = max(ans, tmp + foo * points[color[i]]);
    }
  }
  cout << ans;

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

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

long long ara[5005], tra[5005], mp[5005];

int main() {

  long long n, m, a, b, c, d, e, f, g, h, p, q, r, fr, sc, tr, sz, tz, i, j, k,
      mx = LLONG_MIN, mn = LLONG_MAX;
  long long x, y = 0, cnt = 0, res = 0, ttl = 0, ans, sum = 0;
  long long flg = 0, flag = 1, na = 0, as = 1;
  vector<long long> u, v, w;
  vector<pair<long long, long long>> vct;
  vector<string> vst;
  set<long long> st, nt, tt;
  map<long long, long long> nq, qr;
  string str, ttr, ntr;

  scanf("%lld %lld", &n, &k);

  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    tra[i] = a;
  }

  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    ara[i] = a;
  }

  ans = mx;

  for (i = 1; i <= n; i++) {

    cnt = 0;
    g = mx;
    sum = 0;
    fr = mx;
    x = i;

    while (1) {
      h = tra[x];
      sum = sum + ara[h];

      if (sum > g)
        g = sum;

      cnt++;
      mp[cnt] = g;

      if (cnt == k || h == i)
        break;

      x = h;
    }

    if (cnt == k) {
      if (g > ans)
        ans = g;
    } else {
      for (j = 1; j <= cnt; j++) {
        c = k - j;
        d = c / cnt;
        if (d == 0)
          break;

        e = sum * d;
        e = max(0LL, e);
        f = e + mp[j];
        if (f > g)
          g = f;
      }

      if (g > ans)
        ans = g;
    }
  }

  printf("%lld\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

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

long long ara[5005], tra[5005];

int main() {

  long long n, m, a, b, c, d, e, f, g, h, p, q, r, fr, sc, tr, sz, tz, i, j, k,
      mx = LLONG_MIN, mn = LLONG_MAX;
  long long x, y = 0, cnt = 0, res = 0, ttl = 0, ans, sum = 0;
  long long flg = 0, flag = 1, na = 0, as = 1;
  vector<long long> u, v, w;
  vector<pair<long long, long long>> vct;
  vector<string> vst;
  set<long long> st, nt, tt;
  map<long long, long long> mp, nq, qr;
  string str, ttr, ntr;

  scanf("%lld %lld", &n, &k);

  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    tra[i] = a;
  }

  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    ara[i] = a;
  }

  ans = mx;

  for (i = 1; i <= n; i++) {

    cnt = 0;
    g = mx;
    sum = 0;
    fr = mx;
    x = i;

    while (1) {
      h = tra[x];
      sum = sum + ara[h];

      if (sum > g)
        g = sum;

      cnt++;
      mp[cnt] = g;

      if (cnt == k || h == i)
        break;

      x = h;
    }

    if (cnt == k) {
      if (g > ans)
        ans = g;
    } else {
      for (j = 1; j <= cnt; j++) {
        c = k - j;
        d = c / cnt;
        if (d == 0)
          break;

        e = sum * d;
        e = max(0LL, e);
        f = e + mp[j];
        if (f > g)
          g = f;
      }

      if (g > ans)
        ans = g;
    }

    mp.clear();
  }

  printf("%lld\\n", ans);

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {

  long long int N, K, res = -1000000009;
  vector<long long int> P, C, score;
  cin >> N >> K;

  P.resize(N + 1);
  C.resize(N + 1);
  score.resize(N + 1);

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

  for (int i = 1; i < N + 1; i++) {
    int tmpIdx = i, loopL = 0;
    long long int loopS = 0, score = 0;

    do {
      loopL++;
      tmpIdx = P[tmpIdx];
      loopS += C[tmpIdx];
      score += C[tmpIdx];

      if (loopL <= K) {
        res = max(res, score);
      }
    } while (tmpIdx != i);
    // loop部分を使わない場合はこのループで網羅

    // tmpIdx は i
    score = 0;
    if (K >= loopL && loopS > 0) {
      for (int j = 1; j < loopL + 1; j++) {
        tmpIdx = P[tmpIdx];
        score += C[tmpIdx];
        res = max(res, score + loopS * ((K - j) / loopL));
      }
    }
  }

  cout << res;

  return 0;
}

Error: WA

Fixed submission:

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {

  long long int N, K, res = -1000000009;
  vector<long long int> P, C;
  vector<vector<long long int>> loopLS;
  vector<vector<long long int>> DP;
  cin >> N >> K;

  P.resize(N + 1);
  C.resize(N + 1);
  loopLS.resize(N + 1);

  for (int i = 1; i < N + 1; i++) {
    long long int tmp;
    cin >> tmp;
    P[i] = tmp;
  }
  for (int i = 1; i < N + 1; i++) {
    long long int tmp;
    cin >> tmp;
    C[i] = tmp;
  }

  for (int i = 1; i < N + 1; i++) {
    loopLS[i].resize(2);
    int tmpIdx = i;
    long long int tmpSum = 0;
    for (int j = 1;; j++) {
      tmpIdx = P[tmpIdx];
      tmpSum += C[tmpIdx];
      if (tmpIdx == i) {
        loopLS[i][0] = j;
        loopLS[i][1] = tmpSum;
        break;
      }
    }
  }

  DP.resize(N + 1);

  int maxLoopLength = 0;
  for (int i = 1; i < N + 1; i++) {
    if (loopLS[i][0] > maxLoopLength) {
      maxLoopLength = loopLS[i][0];
    }
  }

  for (int i = 1; i < N + 1; i++) {
    DP[i].resize(maxLoopLength + 1);
    DP[i][0] = 0;
    DP[i][1] = C[P[i]];
  }

  for (int j = 2; j < maxLoopLength + 1; j++) {
    for (int i = 1; i < N + 1; i++) {
      DP[i][j] = DP[P[i]][j - 1] + C[P[i]];
    }
  }

  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < loopLS[i][1] + 1; j++) {
      if (j > K) {
        break;
      }
      res = max(
          res, max(DP[i][j], DP[i][j] + loopLS[i][1] * (K - j) / loopLS[i][0]));
    }
  }

  cout << res;

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

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

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

  ll ans = -1e9 - 1;
  rep(i, n) {
    ll mx = -1e9 - 1;
    ll sum = 0;
    int position;
    int a = p[i] - 1;
    sum += c[a];
    mx = max(mx, sum);
    int times = 1;
    while (a != i && times < k) {
      a = p[a] - 1;
      sum += c[a];
      times++;
      if (mx < sum) {
        mx = sum;
      }
    }
    int loop = k / times;
    int left = k % times;
    if (sum <= 0)
      ans = max(mx, ans);
    else {
      sum *= (loop - 1);
      a = p[i] - 1;
      rep(j, left + times) {
        sum += c[a];
        a = p[a] - 1;
        mx = max(mx, sum);
      }
      ans = max(mx, ans);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  // start point
  ll mx = -1e9;
  rep(i, n) {
    ll sum = 0;
    int a = p[i] - 1;
    rep(j, k) {
      sum += c[a];
      a = p[a] - 1;
      mx = max(mx, sum);
    }
  }
  cout << mx << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1000000000000000
using namespace std;
template <typename T> T Max(T x, T y) { return x > y ? x : y; }
int n, k, c[5005], p[5005];
short g[5005][5005];
long long ans = -INF, f[5005][5005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);

  for (int i = 1; i <= n; i++) {
    g[i][0] = i;
    f[i][0] = 0;
    for (int j = 1; j <= min(n, k); j++) {
      g[i][j] = p[g[i][j - 1]];
      f[i][j] = f[i][j - 1] + (long long)c[g[i][j]];
      ans = Max(ans, f[i][j]);
    }
  }

  if (k <= n) {
    cout << ans << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int j = 1;
    while (g[i][j] != i)
      j++;
    for (int l = 0; l <= k % j; l++)
      ans = Max(ans, (k / j) * f[i][j] + f[i][l]);
    for (int l = 0; l < j; l++)
      ans = Max(ans, (k / j - 1) * f[i][j] + f[i][l]);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000000000
using namespace std;
template <typename T> T Max(T x, T y) { return x > y ? x : y; }
int n, k, c[5005], p[5005];
short g[5005][5005];
long long ans = -INF, f[5005][5005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);

  if (k <= n) {
    for (int i = 1; i <= n; i++) {
      g[i][0] = i;
      f[i][0] = 0;
      for (int j = 1; j <= k; j++) {
        g[i][j] = p[g[i][j - 1]];
        f[i][j] = f[i][j - 1] + (long long)c[g[i][j]];
        ans = Max(ans, f[i][j]);
      }
    }
    cout << ans << endl;
    return 0;
  }
  while (true)
    ;
  for (int i = 1; i <= n; i++) {
    g[i][0] = i;
    f[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      g[i][j] = p[g[i][j - 1]];
      f[i][j] = f[i][j - 1] + (long long)c[g[i][j]];
      ans = Max(ans, f[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    int j = 1;
    while (g[i][j] != i)
      j++;
    for (int l = 0; l <= k % j; l++)
      ans = Max(ans, (k / j) * f[i][j] + f[i][l]);
  }
  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;
typedef pair<int, int> P;
typedef long long ll;
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;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }
  ll ans = -1001001001;
  rep(i, n) {
    vector<int> s;
    int x = p[i];
    ll tot = 0;
    while (true) {
      s.push_back(c[x]);
      tot += c[x];
      if (x == i)
        break;
      x = p[x];
    }
    ll sum = 0;
    int l = s.size();
    rep(j, l) {
      if (j + 1 > k)
        break;
      sum += s[j];
      ll now = sum;
      if (tot > 0) {
        ll e = (k - j - 1) / l;
        now += tot * e; // 周期をとれるだけとって加算
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
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;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }
  ll score = -1001001001;
  rep(i, n) {
    int cur = i;
    ll tmp = 0;
    rep(j, k) {
      tmp += c[p[cur]];
      cur = p[cur];
      score = max(tmp, score);
    }
  }
  cout << score << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll arr[5001], score[5001], vis[5001];
ll n, k;
const ll OO = -1e18;
ll mx;
ll cycleLen(ll idx) {
  if (vis[idx])
    return 0;
  vis[idx]++;
  ll cnt = cycleLen(arr[idx]) + 1;
  return cnt;
}
ll cycleVal(ll idx, ll num, ll scsofar, ll MX) {
  if (vis[idx])
    return 0;
  vis[idx]++;
  scsofar += score[arr[idx]];
  num++;
  if (num <= MX)
    mx = max(mx, scsofar);
  ll ans = cycleVal(arr[idx], num, scsofar, MX) + score[arr[idx]];
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> score[i];
  }
  ll ans = OO, len, cyc;
  pair<int, int> p;
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    mx = OO;
    len = cycleLen(i);
    memset(vis, 0, sizeof vis);
    cyc = cycleVal(i, 0, 0, k % len);
    ll x = k / len;
    ans = max(ans, cyc * (x) + mx);
    memset(vis, 0, sizeof vis);
    mx = OO;
    cyc = cycleVal(i, 0, 0, min(k, len));
    ans = max(ans, cyc * max(x - 1, (long long)0) + mx);
    ans = max(ans, mx);
  }
  cout << ans;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll N = 5e3 + 3;
ll n, k;
ll arr[N];
ll c[N];
ll OO = -1e18;
ll solve(int num, int step) {
  if (step == k)
    return 0;
  ll ret = max(solve(arr[num], step + 1), (long long)0) + c[arr[num]];
  return ret;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    OO = max(OO, solve(i, 0));
  }
  cout << OO << "\\n";
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll NINF = -1e18L;

// INT VS LL

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  ll k;
  cin >> n >> k;
  vector<int> to(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    to[i] = x - 1;
  }
  vector<ll> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  ll sum = 0, ans = NINF;
  vector<bool> vis(n);
  vector<ll> sms;
  function<void(int, int)> dfs = [&](int u, int cnt) {
    if (cnt == k)
      return;
    int v = to[u];
    if (!vis[v]) {
      sum += c[v];
      ans = max(ans, sum);
      sms.push_back(sum);
      vis[v] = 1;
      dfs(v, cnt + 1);
    }
  };
  for (int u = 0; u < n; ++u) {
    dfs(u, 0);
    assert(!sms.empty());
    int len = (int)sms.size();
    int turns = k / len;
    if (turns) {
      ll cur = sms.back() * (turns - 1);
      for (int i = 0; i < (int)sms.size(); ++i)
        ans = max(ans, cur + sms[i]);
      cur += sms.back();
      for (int i = 0; i < k - (turns * len); ++i)
        ans = max(ans, cur + sms[i]);
      sum = 0;
      vis.assign(n, 0);
      sms.clear();
    }
  }
  assert(ans != NINF);
  cout << ans << '\\n';
  // cout << NINF << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll NINF = -1e18L;

// INT VS LL

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> to(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    to[i] = x - 1;
  }
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  ll sum = 0, ans = NINF;
  function<void(int, int)> dfs = [&](int u, int cnt) {
    if (cnt == k)
      return;
    sum += c[to[u]];
    ans = max(ans, sum);
    dfs(to[u], cnt + 1);
  };
  for (int i = 0; i < n; ++i) {
    dfs(i, 0);
    sum = 0;
  }
  cout << ans << '\\n';
  // cout << NINF << endl;
  return 0;
}