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

#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#pragma GCC optimize(2)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 5010;
ll dist[N][N], circle[N], mark[N];
bool st[N];
int pos[N], n;
ll k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> pos[i];
  for (int i = 1; i <= n; i++)
    cin >> mark[i];
  for (int i = 1; i <= n; i++) {
    memset(st, 0, sizeof st);
    int j = pos[i], idx = 0;
    while (!st[j]) {
      st[j] = 1;
      idx++;
      dist[i][idx] = dist[i][idx - 1] + mark[j];
      j = pos[j];
    }
    circle[i] = idx;
    // dist[i][1]=mark[i];
    // int idx=1;
    // for(int j=pos[i];j!=i;j=pos[j])
    // {
    //     ++idx;
    //     dist[i][idx]=dist[i][idx-1]+mark[j];
    // }
    // circle[i]=idx;
  }
  // for(int i=1;i<=n;i++)
  // {
  //     for(int j=1;j<=circle[i];j++) cout<<dist[i][j]<<' ';
  //     cout<<endl;
  // }
  ll res = -1e18;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= circle[i]; j++) {
      ll x = dist[i][j];
      if (k < j)
        break;
      if (dist[i][circle[i]] >= 0)
        x += (k - j) / circle[i] * dist[i][circle[i]];
      res = max(res, x);
    }

  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#pragma GCC optimize(2)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 5010;
ll dist[N][N];
ll circle[N];
bool st[N];
ll mark[N];
int pos[N];
int n, k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> pos[i];
  for (int i = 1; i <= n; i++)
    cin >> mark[i];
  for (int i = 1; i <= n; i++) {
    memset(st, 0, sizeof st);
    int j = pos[i], idx = 0;
    while (!st[j]) {
      st[j] = 1;
      idx++;
      dist[i][idx] = dist[i][idx - 1] + mark[j];
      j = pos[j];
    }
    circle[i] = idx;
  }
  ll res = -1e18;

  for (int i = 1; i <= n; i++) {

    if (circle[i] <= k) {
      if (dist[i][circle[i]] > 0) {
        ll s = k / circle[i] * dist[i][circle[i]];
        int p = k % circle[i];
        ll x = -1e18;
        for (int j = 0; j <= p; j++)
          x = max(x, dist[i][j]);
        res = max(res, s + x);
      } else {
        int p = k;
        ll x = -1e18;
        for (int j = 1; j <= p; j++)
          x = max(x, dist[i][j]);
        res = max(res, x);
      }
    } else {
      int p = k;
      ll x = -1e18;
      for (int j = 1; j <= p; j++)
        x = max(x, dist[i][j]);
      res = max(res, x);
    }
  }
  cout << res << endl;
  return 0;
}