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

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n + 1);
  rep(i, n) cin >> p[i + 1];
  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  ll res = -1e18;
  for (int i = 1; i <= n; ++i) {
    int now = i;
    ll tmp = 0;
    ll mx = -1e18;
    for (int j = 1; j <= k; ++j) {
      now = p[now];
      tmp += c[now];
      chmax(mx, tmp);
      if (now == i) {
        if (tmp < 0)
          break;
        chmax(mx, mx + ((k / j - 1) * tmp));
        tmp *= k / j;
        j = j * (k / j);
      }
    }
    chmax(res, mx);
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n + 1);
  rep(i, n) cin >> p[i + 1];
  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  ll res = -1e18;
  if (k <= 1e4) {
    for (int i = 1; i <= n; ++i) {
      ll tmp = 0;
      ll mx = -1e18;
      int now = i;
      int next = -1;
      rep(j, k) {
        next = p[now];
        tmp += c[next];
        chmax(mx, tmp);
        now = next;
      }
      chmax(res, mx);
    }
    cout << res << endl;
  } else {
    for (int i = 1; i <= n; ++i) {
      ll tmp = 0;
      ll mx = -1e18;
      int now = i;
      int next = -1;
      rep(j, min(k, 10000)) {
        next = p[now];
        tmp += c[next];
        chmax(mx, tmp);
        now = next;
      }
      chmax(res, mx);
      vector<int> passed(n + 1, -1);
      now = i;
      next = -1;
      int loop_start = -1;
      int loop_step = -1;
      passed[now] = 0;
      rep(j, k) {
        next = p[now];
        if (passed[next] != -1) {
          loop_start = next;
          loop_step = passed[now] + 1 - passed[loop_start];
          break;
        }
        now = next;
      }
      ll ini_score = 0;
      int ini_step = 0;
      now = i;
      next = -1;
      rep(j, k) {
        if (now == loop_start)
          break;
        ++ini_step;
        next = p[now];
        ini_score += c[next];
        now = next;
      }
      ll loop_score = 0;
      now = loop_start;
      rep(j, 10000) {
        next = p[now];
        loop_score += c[next];
        if (next == loop_start)
          break;
        now = next;
      }
      k -= ini_step;
      tmp = ini_score;
      mx = -1e18;
      tmp += (k / loop_step) * loop_score;
      k %= loop_step;
      now = loop_start;
      chmax(mx, tmp);
      rep(j, k) {
        next = p[now];
        tmp += c[next];
        chmax(mx, tmp);
        now = next;
      }
      chmax(res, mx);
    }
    cout << res << endl;
  }
}