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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int b[222222];
int vis[6222];
int a[6666];

vector<ll> S;

void dfs(int u, ll s) {
  if (vis[u])
    return;
  vis[u] = 1;
  s += b[u];
  S.pb(s);
  dfs(a[u], s);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  ll ans = -ll(1e18);
  for (int i = 0; i < n; i++) {
    memset(vis, 0, sizeof(vis));
    dfs(i, 0);
    if (S.back() > 0 && k > int(S.size())) {
      ll q = k / int(S.size());
      if (q >= 1) {
        ll mx = -ll(1e18);
        if (q - 1 > 0)
          mx = 0;
        for (int i = 0; i < int(S.size()); i++) {
          mx = max(mx, S[i]);
        }
        ans = max(ans, (q - 1) * S.back() + mx);
      }

      {
        int r = k % int(S.size());
        ll mx = 0;
        for (int i = 0; i < r; i++) {
          mx = max(mx, S[i]);
        }
        ans = max(ans, mx + q * S.back());
      }
    } else {
      for (int j = 1; j <= k; j++) {
        if (j - 1 < S.size())
          ans = max(ans, S[j - 1]);
        else
          break;
      }
    }
    S.clear();
  }
  cout << ans << '\\n';
}

Error: -6
a5fa2556-7fe3-41a9-8566-8db23942cd94.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02585/C++/s296014823.cpp:30: int main(int, char**): Assertion `false' failed.


Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

ll N, K;

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> K;
  vector<ll> P(N);
  REP(i, N) cin >> P[i], --P[i];
  vector<ll> C(N);
  REP(i, N) cin >> C[i];
  assert(false);

  ll res{-INF};
  REP(init, N) {
    ll co = init;
    ll cnt{0};
    vector<ll> sum;
    ll t{0};
    while (true) {
      co = P[co];
      t += C[co];
      sum.push_back(t);
      ++cnt;
      if (init == co)
        break;
    }
    assert(cnt >= 0);

    ll tmp{-INF};
    if (cnt >= K) {
      tmp = *max_element(sum.begin(), sum.begin() + K);
    } else // cnt < K
    {
      if (sum.back() >= 0) {
        tmp = max((K / cnt) * sum.back() +
                      *max_element(sum.begin(), sum.begin() + K % cnt),
                  ((K / cnt) - 1) * sum.back() + *max_element(ALL(sum)));
      } else {
        tmp = *max_element(ALL(sum));
      }
    }

    res = max(res, tmp);
  }

  std::cout << res << std::endl;

  return 0;
}