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

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

typedef long long ll;

int n, k;
int p[5005], c[5005], ir[5005], rc, rl[5005], vis[5005];
stack<int> st;
ll rs[5005], ans = -1e9;

void dfs(int x) {
  vis[x] = 1;
  st.push(x);
  if (!vis[p[x]]) {
    dfs(p[x]);
  } else {
    rc++;
    while (st.top() != p[x]) {
      ir[st.top()] = rc;
      rl[rc]++;
      rs[rc] += c[st.top()];
      st.pop();
    }
    ir[p[x]] = rc;
    rl[rc]++;
    rs[rc] += c[p[x]];
    st.pop();
  }
}

bool u[5005][10005];
ll dp[5005][10005];
int dial;

ll calc(int x, int y) {
  dial++;
  if (y == 0)
    return 0;
  if (ir[x]) {
    ll pre = 0;
    if (y > rl[ir[x]]) {
      pre += max(0ll, (y - rl[ir[x]]) / rl[ir[x]] * rs[ir[x]]);
      y -= (y - rl[ir[x]]) / rl[ir[x]] * rl[ir[x]];
    }
    if (u[x][y])
      return dp[x][y];
    u[x][y] = 1;
    return dp[x][y] = max(0ll, calc(p[x], y - 1)) + c[x] + pre;
  } else {
    return max(0ll, calc(p[x], y - 1)) + c[x];
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  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++)
    if (!vis[i])
      dfs(i);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, calc(i, k));
  }
  cout << ans << endl;
  cerr << "TOT: " << dial << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int n, k;
int p[5005], c[5005], ir[5005], rc, rl[5005], vis[5005];
stack<int> st;
ll rs[5005], ans = -1e9;

void dfs(int x) {
  vis[x] = 1;
  st.push(x);
  if (!vis[p[x]]) {
    dfs(p[x]);
  } else {
    rc++;
    while (st.top() != p[x]) {
      ir[st.top()] = rc;
      rl[rc]++;
      rs[rc] += c[st.top()];
      st.pop();
    }
    ir[p[x]] = rc;
    rl[rc]++;
    rs[rc] += c[p[x]];
    st.pop();
  }
}

map<int, bool> u[5005];
map<int, ll> dp[5005];

ll calc(int x, int y) {
  if (y == 0)
    return 0;
  if (ir[x]) {
    ll pre = 0;
    y--;
    if (y > rl[ir[x]]) {
      pre += max(0ll, (y - rl[ir[x]]) / rl[ir[x]] * rs[ir[x]]);
      y -= (y - rl[ir[x]]) / rl[ir[x]] * rl[ir[x]];
    }
    if (u[x][y])
      return dp[x][y];
    u[x][y] = 1;
    ll &res = dp[x][y] = 0;
    for (int i = 0; i < rl[ir[x]] && i <= y; i++) {
      res = max(res, max(0ll, calc(p[x], (y - i) % rl[ir[x]])) +
                         max(0ll, (y - i) / rl[ir[x]] * rs[ir[x]]));
    }
    return res += c[x] + pre;
  } else {
    return max(0ll, calc(p[x], y - 1)) + c[x];
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  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++)
    if (!vis[i])
      dfs(i);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, calc(i, k));
  }
  cout << ans << endl;

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

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

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

Fixed submission:

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

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 (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, k) {
      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;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  int64_t ans = -1e9, max_score = -1e9;
  bool flg = true;
  vector<int64_t> p(n), c(n);
  vector<bool> used(n, false);
  vector<vector<int64_t>> scores;
  for (auto i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (auto i = 0; i < n; i++) {
    cin >> c[i];
    max_score = max(c[i], max_score);
    if (c[i] >= 0)
      flg = false;
  }
  for (auto i = 0; i < n; i++) {
    if (used[i])
      continue;
    scores.emplace_back(0);
    int index = i;
    int64_t sum = 0;
    while (not used[index]) {
      used[index] = true;
      index = p[index];
      scores.back().emplace_back(c[index]);
      sum += c[index];
    }
    if (sum < 0)
      sum = 0;
    auto size = (int)scores.back().size();
    //        cout << sum << " " << size << endl;
    for (auto j = 0; j < size; j++) {
      int64_t res = scores.back()[j];
      //            cout << j << " " << res << endl;
      for (auto l = 0; l <= min(k - 1, size); l++) {
        //                cout << l << " " << res << " " << ans << endl;
        ans = max(ans, res + sum * ((k - l - 1) / size));
        res += scores.back()[(j + l + 1) % size];
      }
    }
  }
  if (flg)
    ans = max_score;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int64_t> p(n), c(n);
  int64_t ans = -1e9;
  for (auto i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (auto i = 0; i < n; i++)
    cin >> c[i];

  vector<vector<int64_t>> ret(n, vector<int64_t>(n + 1, 0));
  vector<map<int, pair<int, int>>> loop(n);
  pair<int, int> lp;
  for (auto i = 0; i < n; i++) {
    //        cout << i << endl;
    int index = i, cnt = 0, lc = -1;
    int64_t tmp = 0, prev = 0, ls = 0;
    for (auto j = 0; j < n; j++) {
      if (loop[i][index].first != 0) {
        lc = lp.second - loop[i][index].second + 1;
        ls = ret[i][lp.second + 1] - ret[i][loop[i][index].second];
        //                cout << "loop1: " << lc << " " << ls << endl;
        break;
      }
      loop[i][index].first++;
      loop[i][index].second = j;
      lp = loop[i][index];
      prev += c[p[index]];
      ret[i][j + 1] = prev;
      index = p[index];
    }
    if (lc == -1) {
      ls = ret[i].back();
      lc = (int)ret[i].size() - 1;
      //            cout << "loop2: " << lc << " " << ls << endl;
    }
    if (ls > 0) {
      tmp += ls * (k / lc);
      cnt += lc * (k / lc);
      //            cout << "score2: " << tmp << " " << cnt << " " << endl;
    }
    if (k > cnt) {
      prev = -1e9;
      for (auto j = 1; j <= min((k - cnt), lc); j++)
        prev = max(prev, ret[i][j]);
      if (prev > 0)
        tmp += prev;
      else {
        if (cnt == 0)
          tmp = ret[i][1];
      }
    }
    //        cout << "score3: " << tmp << " " << cnt << " " << endl;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

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

  vector<vector<int>> point(n, vector<int>());
  for (int i = 0; i < n; i++) {
    if (p[i] == i) {
      point[i].push_back(c[i]);
      continue;
    }
    point[i].push_back(c[p[i]]);
    int now_p = c[p[i]];
    int now = p[p[i]];
    while (i != now) {
      now_p += c[now];
      point[i].push_back(now_p);
      now = p[now];
    }
    point[i].push_back(now_p + c[now]);
  }

  int max_p = point[0][0];
  for (int i = 0; i < n; i++) {
    int loop_size = point[i].size();
    int pp = point[i][loop_size - 1];
    int p = (k - 1) / loop_size * pp;
    if (pp < 0)
      p = 0;
    int max_plus = point[i][0];
    for (int j = 0; j < k - (k - 1) / loop_size * loop_size; j++) {
      max_plus = max(max_plus, point[i][j]);
    }
    p += max_plus;
    // if(k - k/loop_size*loop_size != 0){
    //     p += point[i][k - k/loop_size*loop_size - 1];
    // }
    max_p = max(max_p, p);
  }

  for (int i = 0; i < n; i++) {
    int lim = ((k > point[i].size()) ? point[i].size() : k);
    for (int j = 0; j < lim; j++) {
      max_p = max(max_p, point[i][j]);
    }
  }
  cout << max_p << endl;

  return 0;
}

Error: 0

Fixed submission:

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

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

  vector<vector<int>> point(n, vector<int>());
  for (int i = 0; i < n; i++) {
    if (p[i] == i) {
      point[i].push_back(c[i]);
      continue;
    }
    point[i].push_back(c[p[i]]);
    int now_p = c[p[i]];
    int now = p[p[i]];
    while (i != now) {
      now_p += c[now];
      point[i].push_back(now_p);
      now = p[now];
    }
    point[i].push_back(now_p + c[now]);
  }

  int max_p = -(1LL << 50);
  for (int i = 0; i < n; i++) {
    int loop_size = point[i].size();
    int pp = point[i][loop_size - 1];
    int p = k / loop_size * pp;
    if (k - k / loop_size * loop_size != 0) {
      p += point[i][k - k / loop_size * loop_size - 1];
    }
    max_p = max(max_p, p);
  }

  int lim = min(k, n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < lim; j++) {
      max_p = max(max_p, point[i][j]);
    }
  }
  cout << max_p << endl;

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

#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;

int main(void) {

  int N;
  long long K;
  cin >> N >> K;

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

  long long max_point = LLONG_MIN;
  long long tmp_point = 0;
  int current_p = 0;

  max_point = LLONG_MIN;
  for (int m_first = 1; m_first < N + 1; m_first++) {
    tmp_point = 0;
    current_p = m_first;
    // cout << "m_first: " << m_first << ", current: " << current_p << endl;
    for (int total = 1; total <= K; total++) {
      // cout << "total: " << total << ", current: " << current_p << endl;
      // cout << "tmp_point: " << tmp_point << endl;
      tmp_point += C[P[current_p]];
      if (max_point <= tmp_point)
        max_point = tmp_point;
      current_p = P[current_p];

      if (m_first == current_p && tmp_point <= 0) {
        tmp_point += C[P[current_p]];
        if (max_point <= tmp_point)
          max_point = tmp_point;
        break;
      } else if (m_first == current_p && tmp_point > 0) {
        int cycle_num = K / total;
        int amari = K % total;
        // cout << "cycle_num: " << cycle_num << endl;
        // cout << "amari: " << amari << endl;
        // cout << "tmp_potnt: " << tmp_point << endl;
        tmp_point = tmp_point * (cycle_num - 1);
        if (max_point <= tmp_point)
          max_point = tmp_point;
        for (int l = 0; l < amari + total; l++) {
          // cout << "tmp_point:" << tmp_point << endl;
          tmp_point += C[P[current_p]];
          if (max_point <= tmp_point)
            max_point = tmp_point;
          current_p = P[current_p];
        }
        break;
      }
    }
    // cout << "" << endl;
  }

  cout << max_point << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;

int main(void) {

  int N;
  long long K;
  cin >> N >> K;

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

  long long max_point = LLONG_MIN;
  long long tmp_point = 0;
  int current_p = 0;

  max_point = LLONG_MIN;
  for (int m_first = 1; m_first < N + 1; m_first++) {
    tmp_point = 0;
    current_p = m_first;
    // cout << "m_first: " << m_first << ", current: " << current_p << endl;
    for (int total = 1; total <= K; total++) {
      // cout << "total: " << total << ", current: " << current_p << endl;
      // cout << "tmp_point: " << tmp_point << endl;
      tmp_point += C[P[current_p]];
      if (max_point <= tmp_point)
        max_point = tmp_point;
      current_p = P[current_p];

      if (m_first == current_p && tmp_point <= 0) {
        tmp_point += C[P[current_p]];
        if (max_point <= tmp_point)
          max_point = tmp_point;
        break;
      }
    }
    // cout << "" << endl;
  }

  cout << max_point << endl;

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

#define INF 1001001001

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

  vector<vector<int>> a;
  vector<bool> used(n);
  rep(i, n) {
    if (used[i])
      continue;
    vector<int> tmp;
    int j = i;
    while (!used[j]) {
      tmp.push_back(c[p[j]]);
      used[j] = true;
      j = p[j];
    }
    a.push_back(tmp);
  }

  ll ans = -INF;
  for (auto e : a) {
    int n = e.size();
    ll sum = 0;
    rep(i, n) sum += e[i];

    ll ta = -INF;
    ll sum2;
    rep(i, n) {
      if (sum <= 0) {
        sum2 = 0;
        int nk = min(n, k);
        rep(j, nk) {
          sum2 += e[(i + j) % n];
          ta = max(ta, sum2);
        }
      } else {
        int nk = k;
        sum2 = sum * (nk / n);
        nk %= n;
        rep(j, nk) sum2 += e[(i + j) % n];
        ta = max(ta, sum2);
        rep(j, n) {
          sum2 -= e[(i + (nk - 1) - j + n) % n];
          ta = max(ta, sum2);
        }
      }
    }
    ans = max(ans, ta);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

#define INF 1001001001

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

  vector<vector<int>> a;
  rep(i, n) {
    if (p[i] == INF)
      continue;
    vector<int> tmp;
    int j = i;
    while (p[j] != INF) {
      tmp.push_back(c[p[j]]);
      int bk = j;
      j = p[j];
      p[bk] = INF;
    }
    a.push_back(tmp);
  }

  ll ans = -INF;
  for (auto e : a) {
    ll ta = 0;
    int n = e.size();
    ll sum = 0;
    rep(i, n) sum += e[i];
    if (k >= n && sum > 0) {
      ta += sum * (k / n);
      k %= n;
    }
    //    cout << "sum:" << sum << endl;
    vector<ll> b(n * 2);
    rep(i, n * 2) {
      if (i)
        b[i] = e[i % n] + b[i - 1];
      else
        b[i] = e[i % n];
    }
    //    cout << "ta:" << ta << endl;
    //    for (auto bb : b) cout << bb << " "; cout << endl;
    if (k > 0) {
      ll ta2 = -INF;
      rep(i, n) {
        rep(j, k) {
          ta2 = max(ta2, b[i + j + 1] - b[i]);
          //        cout << "i:" << i << " ta2:" << ta2 << endl;
        }
      }
      ta += ta2;
    }
    ans = max(ans, ta);
  }

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

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 ans = -10e10;
  rep(i, n) {
    int v = i;
    ll cycle_sum = 0;
    int cycle_cnt = 0;
    while (true) {
      cycle_cnt++;
      cycle_sum += c[v];
      v = p[v];
      if (v == i)
        break;
    }
    ll path = 0;
    int cnt = 0;
    while (true) {
      cnt++;
      path += c[v];
      if (cnt > k)
        break;
      int num = (k - cnt) / cycle_cnt;
      ll score = path + max(0ll, cycle_sum) * num;
      if (ans < score)
        ans = score;
      v = p[v];
      if (i == v)
        break;
    }
  }
  cout << ans << endl;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
typedef long long ll;
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> v(n);
  rep(i, n) cin >> v[i].second;
  rep(i, n) cin >> v[i].first;
  ll m = -10e10, mi, x = 10e10;
  rep(i, n) {
    if (v[i].first > m) {
      m = v[i].first;
      mi = i;
    }
    if (v[i].first < x)
      x = v[i].first;
  }
  /*vector<int> num;
  num.push_back(mi);
  rep(i,n) {
      if(v[i].first==m) {
          num.push_back(i);
      }
  }
  cout << m << endl;
  for(int i=1; i<num.size(); i++) {
      cout << num[i] << endl;
  }*/
  ll sum = 0, ans = x, cnt = 0;
  rep(i, n) {
    if (mi == v[mi].second)
      break;
    sum += v[mi].first;
    mi = v[mi].second;
    ans = max(ans, sum);
    cnt++;
  }
  if (sum > 0 && (k - cnt) > 0) {
    if (k % cnt == 0)
      ans = max(ans, sum * (k / cnt));
    else {
      for (int i = 0; i < (k % cnt); ++i) {
        ans = max(ans, sum * (k / cnt) + v[mi].first);
        mi = v[mi].second;
      }
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const ll inf = 4e18 + 5;

int main() {
  int n, k;
  cin >> n >> k;
  ll ans = -inf;
  vector<int> p(n);
  vector<ll> c(n), q;
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n) p[i]--;
  rep(i, n) {
    q.clear();
    int now = i;
    do {
      now = p[now];
      q.push_back(c[now]);
      // cout<<c[now]<<endl;
    } while (p[now] != p[i]);
    ll karians = -inf, tot = 0, kari = 0;
    for (auto &v : q)
      tot += v;
    rep(j, q.size()) {
      if (j >= k)
        break;
      kari += q[j];
      if (tot > 0) {
        ll aa = (k - j - 1) / q.size();
        if (aa > 0) {
          kari += tot * aa;
          karians = max(karians, kari);
          kari -= tot * aa;
        }
      }
      karians = max(karians, kari);
    }
    ans = max(ans, karians);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const ll inf = 2e18;

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]--;
  vector<int> period(n);
  rep(i, n) {
    int ini = i, now = p[i], cnt = 1;
    while (ini != now) {
      cnt++;
      now = p[now];
    }
    period[i] = cnt;
  }
  ll ans = -inf;
  rep(i, n) {
    int kk = k;
    ll karians = 0;
    if (k > period[i]) {
      ll tot = 0;
      int now = i;
      rep(j, period[i]) {
        tot += c[now];
        now = p[now];
      }
      if (tot > 0) {
        karians += tot * (k / period[i]);
      }
      kk %= period[i];
    }
    vector<ll> sum;
    int now = i;
    rep(j, kk) {
      sum.push_back(c[now]);
      now = p[now];
    }
    rep(j, kk - 1) { sum[j + 1] += sum[j]; }
    sort(etob(sum));
    karians += sum[0];
    ans = max(ans, karians);
  }
  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;
using P = pair<int, int>;

ll ans = -10e10;
int n;
ll k;
vector<int> p;
vector<ll> c;

void simulate(int st) {
  int next = st;

  // get loop cnt
  ll loop_score = 0;
  ll loop_cnt = 0;
  while (true) {
    next = p[next];
    loop_score += c[next];
    loop_cnt++;
    if (next == st)
      break;
  }

  ll score = 0;

  // get rest cnt
  ll rest_cnt = k;
  if (loop_score > 0 && k / loop_cnt >= 2) {
    score += loop_score * (k / loop_cnt - 1);
    rest_cnt -= loop_cnt * (k / loop_cnt - 1);
    ans = max(ans, score);
  } else if (loop_score <= 0) {
    rest_cnt = loop_cnt;
  }

  // calc score
  while (rest_cnt > 0) {
    score += c[next];
    next = p[next];
    rest_cnt--;
    ans = max(ans, score);
  }
  return;
};

int main() {
  cin >> n >> k;

  p.resize(n + 1);
  rep(i, n) cin >> p[i + 1];

  c.resize(n + 1);
  rep(i, n) cin >> c[i + 1];

  rep(i, n) { simulate(i + 1); }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

ll ans = -10e10;
int n;
ll k;
vector<int> p;
vector<ll> c;

void simulate(int st) {
  int next = st;
  ll score = 0;

  // get loop cnt
  ll loop_cnt = 0;
  while (true) {
    next = p[next];
    loop_cnt++;
    if (next == st)
      break;
  }

  // get rest cnt
  ll rest_cnt = k;
  if (score > 0 && k / loop_cnt >= 2) {
    score = score * (k / loop_cnt - 1);
    rest_cnt -= loop_cnt * (k / loop_cnt - 1);
    ans = max(ans, score);
  } else if (score < 0) {
    rest_cnt = min(k, loop_cnt);
  }

  // calc score
  while (rest_cnt > 0) {
    score += c[next];
    next = p[next];
    rest_cnt--;
    ans = max(ans, score);
  }
  return;
};

int main() {
  cin >> n >> k;

  p.resize(n + 1);
  rep(i, n) cin >> p[i + 1];

  c.resize(n + 1);
  rep(i, n) cin >> c[i + 1];

  rep(i, n) { simulate(i + 1); }
  cout << ans << endl;
  return 0;
}