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

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
#define i_7 (long long)(1e9 + 7)
// #define i_7 998'244'353
long mod(long a) {
  long long c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
long long po(long a, long b) {
  if (b == 0) {
    return 1;
  }

  long long z = po(a, b / 2);
  z = mod(z * z);
  if (b % 2 != 0) {
    z = mod(a * z);
  }
  return z;
}

bool prime_(int n) {
  if (n == 1) {
    return false;
  } else if (n == 2) {
    return true;
  } else {
    for (int i = 2; i <= std::sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b) {
  if (a < b) {
    std::swap(a, b);
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd_(b, a % b);
  }
}

long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }

// using namespace std;
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;

int main() {
  using namespace std;
  int n;
  long long k;
  cin >> n >> k;
  int p[n];
  REP(i, n) {
    cin >> p[i];
    p[i]--;
  }
  long long c[n];
  REP(i, n) cin >> c[i];

  vector<long long> temp(n);
  REP(i, n) {
    long long loop = 1;
    long long lscore = c[i];
    long long score = 0;
    int now = i;
    vector<long long> scores;
    while (p[now] != i) {
      loop++;
      lscore += c[p[now]];
      score += c[p[now]];
      scores.push_back(score);
      now = p[now];
    }
    scores.push_back(lscore);
    if (lscore <= 0) {
      vector<long long> tscores;
      if (k >= loop) {
        sort(ALL(scores), greater<long long>());
        temp[i] = scores[0];
        continue;
      }
      REP(j, k) tscores.push_back(scores[j]);
      sort(ALL(tscores), greater<long long>());
      temp[i] = tscores[0];
    } else {
      long long q = k / loop;
      long long r = k % loop;
      if (r == 0) {
        q--;
        r = loop;
      }
      long long a = q * lscore;
      vector<long long> tscores;
      if (q > 0)
        tscores.push_back(0);
      REP(j, r) tscores.push_back(scores[j]);
      sort(ALL(tscores), greater<long long>());
      temp[i] = tscores[0] + a;
    }
  }
  sort(ALL(temp), greater<long long>());
  long long ans = temp[0];
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
#define i_7 (long long)(1e9 + 7)
// #define i_7 998'244'353
long mod(long a) {
  long long c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
long long po(long a, long b) {
  if (b == 0) {
    return 1;
  }

  long long z = po(a, b / 2);
  z = mod(z * z);
  if (b % 2 != 0) {
    z = mod(a * z);
  }
  return z;
}

bool prime_(int n) {
  if (n == 1) {
    return false;
  } else if (n == 2) {
    return true;
  } else {
    for (int i = 2; i <= std::sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b) {
  if (a < b) {
    std::swap(a, b);
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd_(b, a % b);
  }
}

long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }

// using namespace std;
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;

int main() {
  using namespace std;
  int n;
  long long k;
  cin >> n >> k;
  int p[n];
  REP(i, n) cin >> p[i];
  long long c[n];
  REP(i, n) cin >> c[i];

  vector<long long> temp(n);
  REP(i, n) {
    long long loop = 1;
    long long lscore = c[i];
    long long score = 0;
    int now = i;
    vector<long long> scores;
    while (p[now] != i) {
      loop++;
      lscore += c[now];
      score += c[now];
      scores.push_back(score);
      now = p[now];
    }
    scores.push_back(lscore);
    if (lscore <= 0) {
      vector<long long> tscores;
      if (k >= loop) {
        sort(ALL(scores), greater<long long>());
        temp[i] = scores[0];
        continue;
      }
      REP(j, k) tscores.push_back(scores[j]);
      sort(ALL(tscores), greater<long long>());
      temp[i] = tscores[0];
    } else {
      long long q = k / loop;
      long long r = k % loop;
      long long a = q * lscore;
      vector<long long> tscores;
      REP(j, r) tscores.push_back(scores[j]);
      sort(ALL(tscores), greater<long long>());
      temp[i] = tscores[0] + a;
    }
  }
  sort(ALL(temp), greater<long long>());
  long long ans = temp[0];
  cout << ans << endl;
  return 0;
}