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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vi p(N);
  vi c(N);
  rep(i, N) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, N) { cin >> c[i]; }
  ll ans = -LINF;
  rep(s, N) {
    ll res = -LINF;
    ll now = 0;
    vl sc(N, 0);
    int pos = s;
    vi visited(N, 0);
    int len = -1;
    rep(i, K) {
      visited[pos]++;
      pos = p[pos];
      now += c[pos];
      sc[i] = now;
      chmax(res, now);
      if (visited[pos]) {
        len = i + 1;
        break;
      }
    }
    chmax(ans, res);
    if (len < 0) {
      continue;
    }
    ll r = sc[len - 1] * ((K / len) - 1);
    if (((K / len) - 1) > 0) {
      chmax(ans, r);
    }
    rep(i, len) { chmax(ans, r + sc[i]); }
    r += sc[len - 1];
    chmax(ans, r);
    rep(i, K % len) { chmax(ans, r + sc[i]); }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vi p(N);
  vi c(N);
  rep(i, N) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, N) { cin >> c[i]; }
  ll ans = -LINF;
  rep(s, N) {
    ll res = -LINF;
    ll now = 0;
    int pos = s;
    rep(i, K) {
      pos = p[pos];
      now += c[pos];
      chmax(res, now);
    }
    chmax(ans, res);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 5e3 + 20;

int n, k, p[N], c[N];
bool vis[N];

void reset() {
  for (int i = 1; i <= n; i++)
    vis[i] = false;
}

long long go(int node) {
  node = p[node];
  int moves = k;

  reset();
  long long cycle_sum = 0;
  vector<long long> v;
  while (not vis[node]) {
    vis[node] = true;
    cycle_sum += c[node];
    v.push_back(cycle_sum);
    node = p[node];
  }

  long long ans = LLONG_MIN;
  for (int i = 0; i < min(moves, (int)v.size()); i++)
    ans = max(ans, v[i]);
  if (moves > v.size()) {
    long long sum = ((moves - 1) / v.size()) * cycle_sum;
    ans = max(ans, sum);
    for (int i = 0; i <= (moves - 1) % v.size(); i++)
      ans = max(ans, sum + v[i]);
  }
  return ans;
}

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]);

  long long ans = LLONG_MIN;
  for (int i = 1; i <= n; i++)
    ans = max(ans, go(i));
  printf("%lld", ans);
}

Error: TLE

Fixed submission:

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

const int N = 5e3 + 20;

int n, moves, p[N], c[N];
bool vis[N];

void reset() {
  for (int i = 1; i <= n; i++)
    vis[i] = false;
}

long long go(int node) {
  set<int> s;

  node = p[node];
  long long ans = 0, final_ans = LLONG_MIN;
  int k = moves;
  reset();
  while (k > -1) {
    if (k == 0)
      return final_ans;
    ans += c[node];
    final_ans = max(final_ans, ans);
    vis[node] = true;
    node = p[node];
    k--;
  }

  k++;
  reset();
  vector<long long> v;
  long long cycle_sum = 0;
  while (not vis[node]) {
    if (k == 0)
      return final_ans;
    cycle_sum += c[node];
    final_ans = max(final_ans, ans + cycle_sum);
    v.push_back(cycle_sum);
    vis[node] = true;
    node = p[node];
    k--;
  }

  ans += cycle_sum * (k / v.size());
  k %= v.size();
  ans += v[k];

  final_ans = max(final_ans, ans);
  return final_ans;
}

int main() {
  scanf("%d%d", &n, &moves);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);

  long long ans = LLONG_MIN;
  for (int i = 1; i <= n; i++)
    ans = max(ans, go(i));
  printf("%lld", ans);
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
ll p[5005], c[5005];
ll chain[5005][5005];
int vis[5005];
ll len[5005];
int main() {
  IOS;
  ll n, k;
  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++) {
    int pp = i;
    int s = 1;
    do {
      chain[i][s] = chain[i][s - 1] + c[p[pp]];
      pp = p[pp];
      s++;
    } while (pp != i);
    len[i] = s - 1;
  }
  ll ans = -LINF;
  // for (int i = 1; i <= n; i++) {
  //   for (int j = 1; j <= len[i]; j++) {
  //     cout << chain[i][j] << ' ';
  //   }
  //   cout << endl;
  // }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= min(k, len[i]); j++) {
      ll val = chain[i][j];
      ans = max(ans, val);
      if (k >= len[i]) {
        val = max(val, val + ll(max(0LL, k - j) / len[i]) * chain[i][len[i]]);
      }
      ans = max(ans, val);
    }
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
ll p[5005], c[5005];
ll dp[5005][5005];
int main() {
  IOS;
  int n, k;
  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++) {
    for (int j = 1; j <= k; j++) {
      dp[i][j] = -LINF;
    }
  }
  for (int i = 1; i <= n; i++)
    dp[p[i]][1] = c[p[i]];
  for (int j = 2; j <= k; j++) {
    for (int i = 1; i <= n; i++) {
      dp[p[i]][j] = max(dp[p[i]][j], dp[i][j - 1] + c[p[i]]);
    }
  }
  ll ans = -LINF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      // cout << dp[i][j] << ' ';
      ans = max(ans, dp[i][j]);
    }
    // cout << endl;
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// Author: HarshKumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e6 + 6, M = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  ll n, k, ans;
  cin >> n >> k;
  vector<ll> p(n + 1), c(n + 1), vis(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> p[i];
  for (ll i = 1; i <= n; i++)
    cin >> c[i];
  ans = *max_element(c.begin() + 1, c.end());
  for (ll i = 1; i <= n; i++)
    if (!vis[i]) {
      ll j = i, ts = 0, mx = -M * M;
      vector<ll> cyc;
      while (!vis[j])
        cyc.push_back(c[j]), vis[j] = 1, j = p[j];
      ll cs = cyc.size();
      for (ll e : cyc)
        ts += e;
      ll kk = k;
      for (ll i = 0; i < cs; i++) {
        ll s = 0;
        for (ll j = 0; j < min(cs, kk); j++)
          s += cyc[(i + j) % cs], mx = max(mx, s);
      }
      if (ts > 0 && kk > cs) {
        ll q = kk / cs;
        kk -= q * cs;
        mx = (q - 1) * ts + mx;
        ll tmmx = -M * M;
        for (ll i = 0; i < cs; i++) {
          ll s = 0;
          for (ll j = 0; j < min(cs, kk); j++)
            s += cyc[(i + j) % cs], tmmx = max(tmmx, s);
        }
        mx = max(mx, q * ts + tmmx);
      }
      ans = max(ans, mx);
    }
  cout << ans;
}

Error: -11

Fixed submission:

// Author: HarshKumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 3e3 + 6, M = 1e9 + 7;
ll a[N][N], dp[N][N][4][2];
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  ll n, m, k;
  cin >> n >> m >> k;
  while (k--) {
    ll r, c, v;
    cin >> r >> c >> v;
    a[r][c] = v;
  }
  for (ll i = 1; i <= n; i++)
    for (ll j = 1; j <= m; j++) {
      ll mx = 0;
      for (ll k = 0; k < 4; k++)
        for (ll l = 0; l < 2; l++)
          mx = max(mx, dp[i - 1][j][k][l]);
      dp[i][j][0][0] = max(mx, dp[i][j - 1][0][0]);
      for (ll k = 1; k < 4; k++)
        dp[i][j][k][0] = max(dp[i][j - 1][k][0], dp[i][j - 1][k][1]);
      dp[i][j][1][1] = mx + a[i][j];
      for (ll k = 1; k < 4; k++)
        dp[i][j][k][1] = max({dp[i][j][k][1], dp[i - 1][j][k - 1][1] + a[i][j],
                              dp[i][j][k - 1][0] + a[i][j]});
    }
  ll mx = 0;
  for (ll k = 0; k < 4; k++)
    for (ll l = 0; l < 2; l++)
      mx = max(mx, dp[n][m][k][l]);
  cout << mx;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  vector<int> visited(N);
  vector<vector<int>> VV;
  for (int i = 0; i < N; i++)
    if (!visited[i]) {
      vector<int> v = {i};
      int pt = P[i];
      while (pt != i) {
        v.push_back(pt);
        pt = P[pt];
      }
      for (int a : v)
        visited[a] = 1;
      VV.push_back(v);
    }

  int64_t ans = -1e18;
  for (auto &v : VV) {
    int sz = v.size();
    for (int r = 0; r < sz; r++) {
      vector<int64_t> S(sz + 1);
      for (int i = 0; i < sz; i++)
        S[i + 1] = S[i] + C[v[i]];
      for (int t = 0; t < sz; t++) {
        int64_t lmn = (t ? 0 : 1);
        int64_t lmx = (K + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        int64_t l = (S[sz] > 0 ? lmx : lmn);
        int64_t res = l * S[sz] + S[t];
        ans = max(ans, res);
      }
      rotate(v.begin(), v.begin() + 1, v.end());
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  vector<int> visited(N);
  vector<vector<int>> VV;
  for (int i = 0; i < N; i++)
    if (!visited[i]) {
      vector<int> v = {i};
      int pt = P[i];
      while (pt != i) {
        v.push_back(pt);
        pt = P[pt];
      }
      VV.push_back(v);
    }

  int64_t ans = -1e18;
  for (auto &v : VV) {
    int sz = v.size();
    for (int r = 0; r < sz; r++) {
      vector<int64_t> S(sz + 1);
      for (int i = 0; i < sz; i++)
        S[i + 1] = S[i] + C[v[i]];
      for (int t = 0; t < sz; t++) {
        int64_t lmn = (t ? 0 : 1);
        int64_t lmx = (K + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        int64_t l = (S[sz] > 0 ? lmx : lmn);
        int64_t res = l * S[sz] + S[t];
        ans = max(ans, res);
      }
      rotate(v.begin(), v.begin() + 1, v.end());
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5005;
int n, k, p[N], c[N], cir[N];
int cal(int st) {
  int now = p[st], j = 0;
  while (now != st) {
    cir[++j] = c[now];
    now = p[now];
  }
  cir[++j] = c[now];
  for (int i = 1; i <= j; i++)
    cir[i] += cir[i - 1];
  if (k <= j) {
    int ans = cir[1];
    for (int i = 1; i <= k; i++)
      ans = max(ans, cir[i]);
    return ans;
  } else {
    int ans = cir[1];
    for (int i = 1; i <= j; i++)
      ans = max(ans, cir[i]);
    int macir = ans, qu = k % j, maqu = 0;
    for (int i = 1; i <= qu; i++)
      maqu = max(maqu, cir[i]);
    ans = max(ans, k / j * cir[j] + maqu);
    if (k / j > 0)
      ans = max(ans, (k / j - 1) * cir[j] + macir);
    return ans;
  }
}
signed 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]);
  int ans = -1e17;
  for (int i = 1; i <= n; i++)
    ans = max(cal(i), ans);
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int s = -1073741825, h, a[1005], b[1005], m;
void bfs(int i, int k) {
  if (k >= h)
    return;
  m += b[a[i]];
  s = max(s, m);
  // cout<<m<<' ';
  bfs(a[i], k + 1);
  return;
}
int main() {
  int n;
  cin >> n >> h;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[i] = x - 1;
    a[x] = i - 1;
  }
  for (int i = 0; i < n; i++) {
    m = 0;
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    m = 0;
    bfs(i, 0);
    // cout<<'\\n';
  }
  cout << s << '\\n';
}
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 P = pair<int, int>;
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]--; // 0始まりに合わせる

  ll ans = -1e18; // 負にしておく
  rep(si, N) {    // 開始位置の変更
    int x = si;
    vector<int> s;
    ll tot = 0;
    // 周期のカウント
    while (1) {
      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) { // 1周期分の試行
      t += s[i];
      if (i + 1 > K)
        break;
      ll now = t;
      if (tot > 0) { // totが負になる場合周期を繰り返さない方がいい
        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;

int64_t walk(int64_t zahyo, int64_t D) {
  if (zahyo >= 0)
    return zahyo - D;
  else
    return zahyo + D;
}

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

  // vector<int> P(N);
  map<int, int> P, C;
  // vector<tuple<int,int, int>> P(N); // tuple(スコア, 移動先,
  // 対応するコマ番号)
  for (int i = 0; i < N; i++)
    cin >> P[i + 1];
  for (int i = 0; i < N; i++)
    cin >> C[i + 1];

  int64_t max = C[1], score, now;
  for (int i = 1; i < N + 1; i++) {
    score = 0;
    now = i;
    for (int j = 0; j < K; j++) {
      now = P[now]; // 移動後の位置
      score += C[now];
      if (max < score)
        max = score; // 更新
    }
  }

  cout << max << endl;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, k, p[5005] = {}, c[5005] = {}, ans = -1e10, score[5005][5005] = {};
bool visited[5005][5005] = {};

int main() {
  cin >> n >> k;
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n) {
    ll cur = i, maxscore[5005] = {};
    for (int j = 1; j <= k && !visited[i][cur]; j++) {
      visited[i][cur] = true;
      score[i][j] = score[i][j - 1] + c[p[cur] - 1];
      maxscore[j] = max(maxscore[j - 1], score[i][j]);
      ans = max(ans, score[i][j]);
      cur = p[cur] - 1;
      if (visited[i][cur] && score[i][j] > 0)
        ans = max(ans, max(score[i][j] * (k / j) + maxscore[k % j],
                           score[i][j] * (k / j - 1) + maxscore[j]));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, k, p[5005] = {}, c[5005] = {}, ans = -1e10;
bool visited[5005] = {};

int main() {
  cin >> n >> k;
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n) {
    ll cur = i, score[5005] = {};
    for (int j = 1; j <= k && !visited[cur]; j++) {
      score[j] = score[j - 1] + c[cur];
      ans = max(ans, score[j]);
      cur = p[cur] - 1;
      if (visited[cur] && score[j] > 0)
        ans = max(ans, score[j] * (k / j) + score[k % j]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long a[5010], c[5010];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m, ans = -1e18;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    long long s = i, sum = 0, k = 0;
    for (int j = 1; j <= m; j++) {
      s = a[s];
      sum += c[s];
      ans = max(ans, sum);
      if (s == i) {
        k = j;
        break;
      }
    }
    if (m == k || k == 0)
      continue;
    sum = sum * ((m - 1) / k);
    ans = max(ans, sum);
    long long mm = (m - 1) % k + 1;
    s = i;
    for (int j = 1; j <= mm; j++) {
      s = a[s];
      sum += c[s];
      ans = max(ans, sum);
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[5010], c[5010];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m, ans = -1e18;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    long long s = i, sum = 0;
    for (int j = 1; j <= m; j++) {
      s = a[s];
      sum += c[s];
      ans = max(ans, sum);
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

// typedef pair<int, pair<int, int> > p;

#define MOD 1000000007

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

  ll N, K;
  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];
  }

  vector<ll> l(N);
  vector<ll> c(N);
  vector<vector<ll>> m(N);

  ll ans = -pow(10, 18);

  for (int i = 0; i < N; i++) {
    int p = P[i];
    ll count = 0;
    m[i].push_back(0);
    ll ma = -pow(10, 18);
    while (p != i) {
      count += C[p];
      ma = max(ma, count);
      m[i].push_back(ma);
      p = P[p];
    }
    count += C[p];
    c[i] = m[i].size();
    l[i] = count;
  }

  for (int i = 0; i < N; i++) {

    ll count = (K / c[i]) * l[i];
    count += m[i][K % c[i]];
    ans = max(ans, count);

    count = K / c[i];
    if (count >= 1) {
      count--;
      count *= l[i];
      count += m[i][c[i] - 1];
      ans = max(ans, count);
      ans = max(ans, m[i][c[i] - 1]);
    } else {
      ans = max(ans, m[i][K]);
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

// typedef pair<int, pair<int, int> > p;

#define MOD 1000000007

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

  ll N, K;
  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];
  }

  vector<ll> l(N);
  vector<ll> c(N);
  vector<vector<ll>> m(N);

  ll ans = -pow(10, 18);

  for (int i = 0; i < N; i++) {
    int p = P[i];
    ll count = 0;
    m[i].push_back(0);
    ll ma = -pow(10, 18);
    while (p != i) {
      count += C[p];
      ma = max(ma, count);
      m[i].push_back(ma);
      p = P[p];
    }
    count += C[p];
    c[i] = m[i].size();
    l[i] = count;
  }

  for (int i = 0; i < N; i++) {
    if (l[i] > K) {
      ans = max(ans, m[i][K]);
    }

    ll count = (K / c[i]) * l[i];
    count += m[i][K % c[i]];
    ans = max(ans, count);

    count = K / c[i];
    if (count >= 1) {
      count--;
      count *= l[i];
      count += m[i][c[i] - 1];
      ans = max(ans, count);
    }
  }

  cout << ans << endl;
}