Problem p02585 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
// namespace mp = boost::multiprecision;

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

  int n;
  ll k;
  cin >> n >> k;
  int *p = new int[n];
  rep(i, n) cin >> p[i];
  ll *c = new ll[n];
  rep(i, n) cin >> c[i];

  ll ans = INF * (-1);
  ll sum;
  ll *sum_max = new ll[n + 1];
  rep(i, n) {
    sum = 0;
    sum_max[0] = 0;

    ll counter = 0;
    int j = i;

    do {
      ++counter;

      j = p[j] - 1;
      sum += c[j];
      sum_max[counter] = max(sum, sum_max[counter - 1]);
    } while (i != j);

    if (k <= counter || sum < 0) {
      if (sum_max[k] == 0) {
        sum = 0;
        sum_max[0] = c[p[i] - 1];
        counter = 0;
        j = i;

        do {
          ++counter;
          j = p[j] - 1;
          sum += c[j];
          sum_max[counter] = max(sum, sum_max[counter - 1]);
        } while (i != j);
      }
      ans = max(ans, sum_max[min(k, counter)]);
    } else {
      if (sum + sum_max[k % counter] >= sum_max[counter]) {
        ans = max(ans, sum * (k / counter) + sum_max[k % counter]);
      } else {
        ans = max(ans, sum * max((ll)0, k / counter - 1) + sum_max[counter]);
      }
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
// namespace mp = boost::multiprecision;

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

  int n;
  ll k;
  cin >> n >> k;
  int *p = new int[n];
  rep(i, n) cin >> p[i];
  ll *c = new ll[n];
  rep(i, n) cin >> c[i];

  ll ans = INF * (-1);
  ll sum;
  ll *sum_max = new ll[n + 1];
  rep(i, n) {
    sum = 0;
    sum_max[0] = 0;

    ll counter = 0;
    int j = i;

    do {
      ++counter;

      j = p[j] - 1;
      sum += c[j];
      sum_max[counter] = max(sum, sum_max[counter - 1]);
    } while (i != j);

    if (k <= counter || sum < 0) {
      if (sum_max[min(k, counter)] == 0) {
        sum = 0;
        sum_max[0] = c[p[i] - 1];
        counter = 0;
        j = i;

        do {
          ++counter;
          j = p[j] - 1;
          sum += c[j];
          sum_max[counter] = max(sum, sum_max[counter - 1]);
        } while (i != j);
      }
      ans = max(ans, sum_max[min(k, counter)]);
    } else {
      if (sum + sum_max[k % counter] >= sum_max[counter]) {
        ans = max(ans, sum * (k / counter) + sum_max[k % counter]);
      } else {
        ans = max(ans, sum * max((ll)0, k / counter - 1) + sum_max[counter]);
      }
    }
  }

  cout << ans << "\n";

  return 0;
}