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

/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
                  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

////多倍長整数, cpp_intで宣言
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

// #pragma gcc target ("avx2")
// #pragma gcc optimization ("o3")
// #pragma gcc optimization ("unroll-loops")
#define repeat(i, n, m) for (int i = n; i < (m); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\\n" : "no\\n")
#define printyn(a) printf(a ? "Yes\\n" : "No\\n")
#define printYN(a) printf(a ? "YES\\n" : "NO\\n")
#define printim(a) printf(a ? "possible\\n" : "imposible\\n")
#define printdb(a) printf("%.50lf\\n", a)    // 少数出力
#define printLdb(a) printf("%.50Lf\\n", a)   // 少数出力
#define printdbd(a) printf("%.16lf\\n", a)   // 少数出力(桁少なめ)
#define prints(s) printf("%s\\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg) / 360.0L) * 2.0L * PI)
#define rad_to_deg(rad) (((rad) / 2.0L / PI) * 360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d)                                             \\
  (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string &str) {
  char c;
  str = "";
  scanf("%c", &c);
  if (c == '\\n')
    scanf("%c", &c);
  while (c != '\\n' && c != -1 && c != ' ') {
    str += c;
    scanf("%c", &c);
  }
}

void scanc(char &str) {
  char c;
  scanf("%c", &c);
  if (c == -1)
    return;
  while (c == '\\n') {
    scanf("%c", &c);
  }
  str = c;
}

double acot(double x) { return PI / 2 - atan(x); }

ll LSB(ll n) { return (n & (-n)); }

template <typename T> T chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a;
}

template <typename T> T chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a;
}

/*-----------------------------------------ここからコード-----------------------------------------*/

int main() {

  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  vector<ll> p(n), c(n);
  vector<pair<ll, ll>> score(n);
  rep(i, n) {
    scanf("%lld", &p[i]);
    --p[i];
  }
  rep(i, n) scanf("%lld", &c[i]);
  rep(i, n) {
    ll cur = p[i], cnt = 1;
    ll sum = 0;
    while (cur != i) {
      sum += c[cur];
      cur = p[cur];
      ++cnt;
    }
    sum += c[cur];
    score[i] = {sum, cnt};
  }
  ll ans = *max_element(all(c)), tmp = 0;
  ll ktmp = k, now = 0, cur = 0;
  for (const auto &[sum, cnt] : score) {
    cur = now;
    ++now;
    k = ktmp;
    tmp = 0;
    vector<ll> add, del;
    if (sum >= 0) {
      tmp += sum * (k / cnt);
      k -= (k / cnt) * cnt;
      chmax(ans, tmp);
      add.push_back(0);
    }
    chmin(k, cnt);
    for (int i = 0; k--; ++i) {
      cur = p[cur];
      if (i)
        add.push_back(add.back() + c[cur]);
      else
        add.push_back(c[cur]);
    }
    ll ad = *max_element(all(add));
    if (sum >= 0) {
      add = {0};
      cur = p[now - 1];
      while (cur != now - 1) {
        add.push_back(add.back() + c[cur]);
        cur = p[cur];
      }
      add.push_back(add.back() + c[cur]);
      chmax(ad, *max_element(all(add)) - sum);
    }
    tmp += ad;
    chmax(ans, tmp);
  }
  printf("%lld\\n", ans);

  Please AC;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
                  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

////多倍長整数, cpp_intで宣言
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

// #pragma gcc target ("avx2")
// #pragma gcc optimization ("o3")
// #pragma gcc optimization ("unroll-loops")
#define repeat(i, n, m) for (int i = n; i < (m); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\\n" : "no\\n")
#define printyn(a) printf(a ? "Yes\\n" : "No\\n")
#define printYN(a) printf(a ? "YES\\n" : "NO\\n")
#define printim(a) printf(a ? "possible\\n" : "imposible\\n")
#define printdb(a) printf("%.50lf\\n", a)    // 少数出力
#define printLdb(a) printf("%.50Lf\\n", a)   // 少数出力
#define printdbd(a) printf("%.16lf\\n", a)   // 少数出力(桁少なめ)
#define prints(s) printf("%s\\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg) / 360.0L) * 2.0L * PI)
#define rad_to_deg(rad) (((rad) / 2.0L / PI) * 360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d)                                             \\
  (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string &str) {
  char c;
  str = "";
  scanf("%c", &c);
  if (c == '\\n')
    scanf("%c", &c);
  while (c != '\\n' && c != -1 && c != ' ') {
    str += c;
    scanf("%c", &c);
  }
}

void scanc(char &str) {
  char c;
  scanf("%c", &c);
  if (c == -1)
    return;
  while (c == '\\n') {
    scanf("%c", &c);
  }
  str = c;
}

double acot(double x) { return PI / 2 - atan(x); }

ll LSB(ll n) { return (n & (-n)); }

template <typename T> T chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a;
}

template <typename T> T chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a;
}

/*-----------------------------------------ここからコード-----------------------------------------*/

int main() {

#define int ll
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  vector<ll> p(n), c(n);
  vector<pair<ll, ll>> score(n);
  rep(i, n) {
    scanf("%lld", &p[i]);
    --p[i];
  }
  rep(i, n) scanf("%lld", &c[i]);
  rep(i, n) {
    ll cur = p[i], cnt = 1;
    ll sum = 0;
    while (cur != i) {
      sum += c[cur];
      cur = p[cur];
      ++cnt;
    }
    sum += c[cur];
    score[i] = {sum, cnt};
  }
  ll ans = *max_element(all(c)), tmp = 0;
  ll ktmp = k, now = 0, cur = 0;
  for (const auto &[sum, cnt] : score) {
    cur = now;
    ++now;
    k = ktmp;
    tmp = 0;
    vector<ll> add;
    if (sum >= 0) {
      tmp += sum * (k / cnt);
      k -= (k / cnt) * cnt;
      chmax(ans, tmp);
      add.push_back(0);
    }
    chmin(k, cnt);
    for (int i = 0; k--; ++i) {
      cur = p[cur];
      if (i)
        add.push_back(add.back() + c[cur]);
      else
        add.push_back(c[cur]);
    }
    tmp += *max_element(all(add));
    chmax(ans, tmp);
  }
  printf("%lld\\n", ans);

  Please AC;
}