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

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

void chmax(int64_t &a, int64_t b) { a = max(a, b); }

int nth_bit(int64_t num, int n) { return (num >> n) & 1; }

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  static int64_t dub[30][5000], nxt[30][5000];
  for (int i = 0; i < N; i++) {
    dub[0][i] = C[P[i]];
    nxt[0][i] = P[i];
  }
  for (int k = 1; k < 30; k++) {
    for (int i = 0; i < N; i++) {
      dub[k][i] = dub[k - 1][i] + dub[k - 1][nxt[k - 1][i]];
      nxt[k][i] = nxt[k - 1][nxt[k - 1][i]];
    }
  }

  // k桁目まで、頂点iにいる、K未満確定、正
  static int64_t dp[31][5000][2][2];
  for (int k = 30; k >= 0; k--)
    for (int i = 0; i < N; i++)
      for (int l = 0; l < 2; l++)
        for (int p = 0; p < 2; p++)
          dp[k][i][l][p] = -5e18;
  for (int i = 0; i < N; i++)
    dp[30][i][0][0] = 0;

  for (int k = 29; k >= 0; k--)
    for (int i = 0; i < N; i++)
      for (int l = 0; l < 2; l++)
        for (int p = 0; p < 2; p++) {
          int64_t v = dp[k + 1][i][l][p];
          for (int u = 0; u < 2; u++) {
            if (!l && !nth_bit(K, k) && u)
              continue;
            int64_t v2 = v + (u ? dub[k][i] : 0);
            int i2 = (u ? nxt[k][i] : i);
            int l2 = l | (nth_bit(K, k) && !u);
            int p2 = p | u;
            chmax(dp[k][i2][l2][p2], v2);
          }
        }

  int64_t ans = -5e18;
  for (int i = 0; i < N; i++)
    for (int l = 0; l < 2; l++)
      chmax(ans, dp[0][i][l][1]);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

void chmax(int64_t &a, int64_t b) { a = max(a, b); }

int nth_bit(int64_t num, int n) { return (num >> n) & 1; }

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  static int64_t dub[30][3000], nxt[30][3000];
  for (int i = 0; i < N; i++) {
    dub[0][i] = C[P[i]];
    nxt[0][i] = P[i];
  }
  for (int k = 1; k < 30; k++) {
    for (int i = 0; i < N; i++) {
      dub[k][i] = dub[k - 1][i] + dub[k - 1][nxt[k - 1][i]];
      nxt[k][i] = nxt[k - 1][nxt[k - 1][i]];
    }
  }

  // k桁目まで、頂点iにいる、K未満確定、正
  static int64_t dp[31][5000][2][2];
  for (int k = 30; k >= 0; k--)
    for (int i = 0; i < N; i++)
      for (int l = 0; l < 2; l++)
        for (int p = 0; p < 2; p++)
          dp[k][i][l][p] = -1e18;
  for (int i = 0; i < N; i++)
    dp[30][i][0][0] = 0;

  for (int k = 29; k >= 0; k--)
    for (int i = 0; i < N; i++)
      for (int l = 0; l < 2; l++)
        for (int p = 0; p < 2; p++) {
          int64_t v = dp[k + 1][i][l][p];
          for (int u = 0; u < 2; u++) {
            if (!l && !nth_bit(K, k) && u)
              continue;
            int64_t v2 = v + (u ? dub[k][i] : 0);
            int i2 = (v ? nxt[k][i] : i);
            int l2 = l | (nth_bit(K, k) && !u);
            int p2 = p | u;
            chmax(dp[k][i2][l2][p2], v2);
          }
        }

  int64_t ans = -1e18;
  for (int i = 0; i < N; i++)
    for (int l = 0; l < 2; l++)
      chmax(ans, dp[0][i][l][1]);
  cout << ans << endl;
  return 0;
}