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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 n, k;
  cin >> n >> k;

  vs64 p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) { cin >> c[i]; }

  vector<vs64> dp(n, vs64(n + 1));
  vector<vs32> dst(n, vs32(n + 1));
  rep(i, n) dp[i][0] = 0;
  rep(i, n) dst[i][0] = i;

  rep(j, n) {
    rep(i, n) { dst[i][j + 1] = p[dst[i][j]]; }
  }

  rep(j, n) {
    rep(i, n) { dp[i][j + 1] = dp[i][j] + c[dst[i][j + 1]]; }
  }

  s64 ans = -1e18;
  rep(i, n) {
    s64 loop;
    for (loop = 1; loop <= n; ++loop) {
      if (dst[i][loop] == i) {
        break;
      }
    }

    s64 sum;
    if (k > n && loop <= k && dp[i][loop] >= 0) {
      s64 tk = k % loop;
      sum = dp[i][loop] * (k / loop);
      s64 temp = 0;
      rep(j, tk + 1) { temp = max(temp, dp[i][j]); }
      rep(j, loop + 1) { temp = max(temp, dp[i][j] - dp[i][loop]); }
      sum += temp;
    } else {
      sum = -1e18;
      rep(j, MIN(n, k)) { sum = max(sum, dp[i][j + 1]); }
    }
    ans = max(ans, sum);
  }

  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 n, k;
  cin >> n >> k;

  vs64 p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) { cin >> c[i]; }

  s64 dp[n + 100][n + 100];
  int dst[n + 100][n + 100];
  rep(i, n) dp[i][0] = 0;
  rep(i, n) dst[i][0] = i;

  rep(j, n) {
    rep(i, n) { dst[i][j + 1] = p[dst[i][j]]; }
  }

  rep(j, n) {
    rep(i, n) { dp[i][j + 1] = dp[i][j] + c[dst[i][j + 1]]; }
  }

  s64 ans = -1e18;
  rep(i, n) {
    s64 loop;
    for (loop = 1; loop <= n; ++loop) {
      if (dst[i][loop] == i) {
        break;
      }
    }

    s64 sum;
    // if (dp[i][loop] > 0)
    // {
    //     s64 tk = k - k / loop * loop;
    //     sum = dp[i][loop] * (k / loop);
    //     s64 temp = 0;
    //     rep (j, MIN(n + 1, tk + 1))
    //     {
    //         if (!IN(0, n, j)) break;
    //         if (temp < dp[i][j])
    //         {
    //             temp = dp[i][j];
    //         }
    //     }
    //     sum += temp;
    // }
    // else
    {
      sum = -1e18;
      rep(j, n) { sum = max(sum, dp[i][j + 1]); }
    }
    ans = max(ans, sum);
  }

  cout << ans << "\\n";
  return 0;
}