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

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

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << '\\n';
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

int main() {
  Int n, k;
  cin >> n >> k;
  vector<Int> p(n), c(n);
  for (Int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  Int sum = 0;
  for (Int i = 0; i < n; i++) {
    cin >> c[i];
    sum += c[i];
  }
  Int res = -INF;
  for (Int i = 0; i < n; i++) {
    Int score = -INF;
    if (k <= n) {
      Int ma = 0;
      Int cur = i;
      for (Int j = 0; j < k; j++) {
        ma += c[p[cur]];
        cur = p[cur];
        score = max(score, ma);
      }
    } else {
      Int s = c[i];
      Int cur = i;
      Int cycle = 1;
      while (p[cur] != i) {
        s += c[p[cur]];
        cur = p[cur];
        cycle++;
      }
      if (s > 0) {
        Int ma = s * (k / cycle - 1);
        cur = i;
        for (Int j = 0; j < k % cycle + cycle; j++) {
          ma += c[p[cur]];
          cur = p[cur];
          score = max(score, ma);
        }
      } else {
        Int ma = 0;
        cur = i;
        for (Int j = 0; j < cycle; j++) {
          ma += c[p[cur]];
          cur = p[cur];
          score = max(score, ma);
        }
      }
    }
    res = max(res, score);
  }
  print(res);
  return 0;
}


Error: 0

Fixed submission:

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

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << '\\n';
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

int main() {
  Int n, k;
  cin >> n >> k;
  vector<Int> p(n), c(n);
  for (Int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  Int sum = 0;
  for (Int i = 0; i < n; i++) {
    cin >> c[i];
    sum += c[i];
  }
  Int res = -INF;
  for (Int i = 0; i < n; i++) {
    Int score = -INF;
    if (k <= n) {
      Int ma = 0;
      Int cur = i;
      for (Int i = 0; i < k; i++) {
        ma += c[p[cur]];
        cur = p[cur];
        score = max(score, ma);
      }
    } else {
      Int s = c[i];
      Int cur = i;
      Int cycle = 1;
      while (p[cur] != i) {
        s += c[p[cur]];
        cur = p[cur];
        cycle++;
      }
      if (s > 0) {
        Int ma = s * (k / cycle - 1);
        cur = i;
        for (Int j = 0; j < k % cycle + cycle; j++) {
          ma += c[p[cur]];
          cur = p[cur];
          score = max(score, ma);
        }
      } else {
        assert(false);
        Int ma = 0;
        cur = i;
        for (Int i = 0; i < cycle; i++) {
          ma = c[p[cur]];
          cur = p[cur];
          score = max(score, ma);
        }
      }
    }
    res = max(res, score);
  }
  print(res);
  return 0;
}