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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372085477807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

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

  int N, K;
  cin >> N >> K;
  VI P(N), C(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }
  REP(i, N) cin >> C[i];

  VI cnt(N, -1), po(N);
  REP(i, N) {
    if (cnt[i] != -1)
      continue;
    int now = i, to;
    int res = 0;
    VI tmp;
    tmp.push_back(i);
    // res += C[i];
    while (to != i) {
      // cout << now << " " << to << endl;

      to = P[now];
      res += C[to];
      now = to;
      tmp.push_back(to);
    }
    fore(j, tmp) {
      cnt[j] = tmp.size() - 1;
      po[j] = res;
    }
  }

  int ans = -LLINF;
  REP(i, N) {
    int tmp = 0;
    int k;
    if (po[i] > 0) {
      if (K % cnt[i]) {
        tmp += (K / cnt[i]) * po[i];
        k = K % cnt[i];
      } else {
        tmp += ((K / cnt[i]) - 1) * po[i];
        k = K % cnt[i] + K;
      }
    } else
      k = K;

    int ttmp = -LLINF;
    int now = 0;
    int to = i;
    if (k == 0)
      ttmp = 0;
    while (k--) {
      // cout << to << endl;
      now += C[to];
      to = P[to];

      chmax(ttmp, now);
      if (to == i)
        break;
    }

    chmax(ans, tmp + ttmp);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 92233723685477807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

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

  int N, K;
  cin >> N >> K;
  VI P(N), C(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
  }
  REP(i, N) cin >> C[i];

  VI cnt(N, -1), po(N);
  REP(i, N) {
    if (cnt[i] != -1)
      continue;
    int now = i, to;
    int res = 0;
    VI tmp;
    tmp.push_back(i);
    while (to != i) {

      to = P[now];
      res += C[to];
      now = to;
    }
    fore(j, tmp) {
      cnt[j] = tmp.size() - 1;
      po[j] = res;
    }
  }

  int ans = -LLINF;
  REP(i, N) {
    int tmp = 0;
    int k;
    if (po[i] > 0) {
      tmp += (K / cnt[i]) * po[i];
      k = K % cnt[i];
    } else
      k = K;

    int ttmp = -LLINF;
    int now = 0;
    int to = P[i];
    if (k == 0)
      ttmp = 0;
    while (k--) {

      now += C[to];
      to = P[to];

      chmax(ttmp, now);
      if (to == i)
        break;
    }

    chmax(ans, tmp + ttmp);
  }

  cout << ans << endl;

  return 0;
}