Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// I SELL YOU...!
#include <algorithm>
#include <cassert>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
signed main() {
  init_io();
  ll n, k, ans = -1e18, tmp = 0, idx = 0, cnt = 0;
  ll tk;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  tk = k;
  for (int s = 0; s < n; s++) {
    k = tk;
    tmp = 0;
    idx = s;
    if (k * n <= 1e7) {
      while (k > 0) {
        k--;
        idx = p[idx];
        tmp += c[idx];
        ans = max(tmp, ans);
      }
      continue;
    }
    vector<ll> pre(n, -1);
    vector<ll> score(n, 0);
    pre[s] = 0;
    cnt = 0;
    while (k > 0) {
      k--;
      idx = p[idx];
      tmp += c[idx];
      cnt++;
      ans = max(tmp, ans);
      if (pre[idx] != -1) {
        break;
      }
      pre[idx] = cnt;
      score[idx] = tmp;
    }
    if (k != 0) {
      ll roop_sc = tmp - score[idx];
      ll roop_need = cnt - pre[idx];
      ll roop_can = k / roop_need;
      roop_can = max(0ll, roop_can - 7);
      if (roop_sc >= 0) {
        tmp += roop_sc * roop_can;
        k -= roop_can * roop_need;
        ans = max(tmp, ans);
      } else {
        k = min(k, 7 * n);
      }
      while (k > 0) {
        k--;
        idx = p[idx];
        tmp += c[idx];
        ans = max(tmp, ans);
      }
    }
  }
  cout << ans << endl;
}


Error: -6
8e4a5f01-ad73-488d-90b5-6c2e5072851f.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02585/C++/s997402301.cpp:40: int main(): Assertion `1==2' failed.


Fixed submission:

// I SELL YOU...!
#include <algorithm>
#include <cassert>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
signed main() {
  init_io();
  ll n, k, ans = -1e18, tmp = 0, idx = 0, cnt = 0;
  ll tk;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  tk = k;
  for (int s = 0; s < n; s++) {
    k = tk;
    tmp = 0;
    idx = s;
    if (k * n <= 1e7) {
      assert(1 == 2);
      while (k > 0) {
        k--;
        idx = p[idx];
        tmp += c[idx];
        ans = max(tmp, ans);
      }
      continue;
    }
    vector<ll> pre(n, -1);
    vector<ll> score(n, 0);
    pre[s] = 0;
    cnt = 0;
    while (k > 0) {
      k--;
      idx = p[idx];
      tmp += c[idx];
      cnt++;
      if (pre[idx] != -1) {
        break;
      }
      pre[idx] = cnt;
      score[idx] = tmp;
      ans = max(tmp, ans);
    }
    if (k != 0) {
      ll roop_sc = tmp - score[idx];
      ll roop_need = cnt - pre[idx];
      ll roop_can = k / roop_need;
      roop_can = max(0ll, roop_can - 7);
      if (roop_sc >= 0) {
        tmp += roop_sc * roop_can;
        k -= roop_can * roop_need;
        ans = max(tmp, ans);
      } else {
        k = min(k, 7 * n);
      }
      while (k > 0) {
        k--;
        idx = p[idx];
        tmp += c[idx];
        ans = max(tmp, ans);
      }
    }
  }
  cout << ans << endl;
}