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

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

using ll = long long;

struct Doubling {
  const long long LOG;
  const int N;
  vector<vector<int>> nexted;

  Doubling(int N, long long log = 60) : N(N), LOG(log) {
    nexted.assign(LOG + 1, vector<int>(N + 1, -1));
  }

  inline void set_init_next(const int v, const int x) { nexted[0][v] = x; }

  void build() {
    for (int k = 0; k < LOG; k++) {
      for (int v = 0; v < N; v++) {
        if (nexted[k][v] == -1)
          nexted[k + 1][v] = -1;
        else
          nexted[k + 1][v] = nexted[k][nexted[k][v]];
      }
    }
  }

  inline int query(int v, long long p) {
    for (long long i = LOG; i >= 0; i--) {
      if (v < 0)
        return -1;
      if (p & (1LL << i)) {
        v = nexted[i][v];
      }
    }
    return v;
  }
};

void solve(long long N, long long K, std::vector<long long> P,
           std::vector<long long> C) {
  auto f = [&](int k) {
    int _k = k;
    ll _K = K;
    vector<int> flag(N);
    ll ans = 0;
    vector<ll> v;
    for (int i = 0;; i++) {
      if (flag[k] == 1) {
        break;
      }
      flag[k] = 1;
      ans += C[P[k]];
      v.push_back(C[P[k]]);
      k = P[k];
    }
    // cout<<ans<<endl;
    if (K <= 5000 * 4) {
      ans = -1e18;
      ll curent = 0;
      for (int i = 0; i < K; i++) {
        curent += v[i % (v.size())];
        ans = max(ans, curent);
      }
      return ans;
    }
    ll _N = v.size();

    if (ans >= 0) {
      // cout<<ans<<endl;
      ans = (K / _N - 1) * ans;
      ll ma = -1e18;
      ll curent = 0;
      for (int i = 0; i < K % _N + _N; i++) {
        curent += v[i % _N];
        ma = max(curent, ma);
      }
      ans += ma;
    } else {
      ll ma = -1e18;
      ll curent = 0;
      for (int i = 0; i < _N; i++) {
        curent += v[i];
        // cout<<curent<<endl;
        ma = max(curent, ma);
      }
      ans = ma;
    }
    return ans;
  };
  ll ans = -1e18;
  for (int i = 0; i < N; i++) {
    ans = max(ans, f(i));
  }
  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &P[i]);
    P[i]--;
  }
  std::vector<long long> C(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &C[i]);
  }
  solve(N, K, std::move(P), std::move(C));
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;

void solve(long long N, long long K, std::vector<long long> P,
           std::vector<long long> C) {
  auto f = [&](int k) {
    int _k = k;
    ll _K = K;
    set<int> se;
    ll ans = 0;
    vector<int> v;
    for (; _K >= 0; _K--) {
      if (se.find(k) != se.end()) {
        break;
      }
      ans += C[P[k]];
      se.insert(k);
      v.push_back(C[P[k]]);
      k = P[k];
    }
    // cout<<ans<<endl;
    if (ans >= 0) {
      ans += _K / v.size() * ans;
      ll ma = 0;
      ll curent = 0;
      for (int i = 0; i < _K % v.size(); i++) {
        curent += v[i];
        ma = max(curent, ma);
      }
      ans += ma;
    } else {
      ll ma = -1e18;
      ll curent = 0;
      for (int i = 0; i < min(N, K); i++) {
        curent += v[i];
        // cout<<curent<<endl;
        ma = max(curent, ma);
      }
      ans = ma;
    }
    return ans;
  };
  ll ans = -1e18;
  for (int i = 0; i < N; i++) {
    ans = max(ans, f(i));
  }
  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &P[i]);
    P[i]--;
  }
  std::vector<long long> C(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &C[i]);
  }
  solve(N, K, std::move(P), std::move(C));
  return 0;
}