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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

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

  vector<int> P(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }

  vector<int> C(N);
  REP(i, N) cin >> C[i];

  REP(i, N) {
    if (C[i] > 0)
      break;
    if (i == N - 1) {
      sort(ALL(C));
      cout << C.back() << "\\n";
      return 0;
    }
  }

  vector<vector<int>> visited(N, vector<int>(N, 0));
  vector<vector<int>> score(N, vector<int>(N + 1, 0));

  REP(i, N) {
    int now = i;
    int cnt = 1;
    while (1) {
      int next = P[now];
      visited[i][next] = visited[i][now] + 1;

      if (cnt != 0)
        score[i][cnt] = score[i][cnt - 1];
      score[i][cnt] += C[next];

      if (next == i)
        break;

      now = next;
      cnt++;
    }
  }

  int ans = -INF;
  REP(i, N) {
    int cycle = visited[i][i];
    int loop = K / cycle;
    int amari = K % cycle;
    int ret = -INF;

    if (loop >= 1) {
      REP(j, cycle + 1) { chmax(ret, score[i][j]); }
      if (score[i][cycle] >= 0)
        ret += (loop - 1) * score[i][cycle];
      chmax(ans, ret);
    }
    {
      ret = -INF;
      REP(j, amari + 1) { chmax(ret, score[i][j]); }
      if (score[i][cycle] >= 0)
        ret += loop * score[i][cycle];
      chmax(ans, ret);
    }
  }

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

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

  vector<int> P(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }

  vector<int> C(N);
  REP(i, N) cin >> C[i];

  REP(i, N) {
    if (C[i] > 0)
      break;
    if (i == N - 1) {
      sort(ALL(C));
      cout << C.back() << "\\n";
      return 0;
    }
  }

  vector<vector<int>> visited(N, vector<int>(N, 0));
  vector<vector<int>> score(N, vector<int>(N + 1, 0));

  REP(i, N) {
    int now = i;
    int cnt = 1;
    while (1) {
      int next = P[now];
      visited[i][next] = visited[i][now] + 1;

      if (cnt != 0)
        score[i][cnt] = score[i][cnt - 1];
      score[i][cnt] += C[next];

      if (next == i)
        break;

      now = next;
      cnt++;
    }
  }

  int ans = -INF;
  REP(i, N) {
    int loop = K / visited[i][i];
    int amari = K % visited[i][i];
    int ret = -INF;

    if (score[i][visited[i][i]] > 0) {
      REP(j, amari + 1) { chmax(ret, score[i][j]); }
    } else {
      REP(j, min(K, loop) + 1) { chmax(ret, score[i][j]); }
    }

    if (score[i][visited[i][i]] >= 0) {
      ret += loop * score[i][visited[i][i]];
    }
    chmax(ans, ret);
  }

  cout << ans << "\\n";

  return 0;
}