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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << endl
#define sz(a) int((a).size())
#define endl '\\n' // remove for interactive

const int INF = 1e9;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

void solve() {
  ll mx = -1e9 - 150;
  // watch(mx);
  int n, k;
  cin >> n >> k;
  vector<ll> v(n + 5), c(n + 5);
  REP(i, 1, n) {
    int x;
    cin >> x;
    v[i] = x;
  }
  REP(i, 1, n) {
    int x;
    cin >> x;
    c[i] = x;
  }
  REP(i, 1, n) {
    vi test;
    int length = 0;
    int currind = v[i];
    ll score = c[currind];
    test.pb(c[currind]);
    length++;
    while (currind != i) {
      currind = v[currind];
      test.pb(c[currind]);
      score += c[currind];
      length++;
    }
    // if(i == 2) {watch(length); watch(score); }
    ll sm = 0;
    ll localmx = test[0];
    REP(j, 0, min(length, k) - 1) {
      sm += test[j];
      localmx = max(localmx, sm);
    }
    mx = max(mx, localmx);
    if (length < k) {
      ll initsm = 1LL * score * (k / length);
      int left = k % length;
      ll localmx = test[0];
      ll localsm = 0;
      REP(j, 0, left - 1) {
        localsm += test[j];
        localmx = max(localmx, localsm);
      }
      if (left == 0)
        localmx = 0;
      mx = max(mx, initsm + localmx);

      initsm = 1LL * score * (k / length - 1);
      left = length;
      localmx = test[0];
      localsm = 0;
      REP(j, 0, left - 1) {
        localsm += test[j];
        localmx = max(localmx, localsm);
      }
      mx = max(mx, initsm + localmx);
    }
  }
  cout << mx;
}

int main() {
  // clock_t start, end;
  // start = clock();
  ios_base::sync_with_stdio(0); // cant use scanf, printf
  cin.tie(0);
  cout.tie(
      0); // no longer auto flush cout before each cin, remove for interactive
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(); }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // watch(time_taken);
  ll x = INF;
  x++;
  return 0;
}

/*before sub
TEST: 0, 1, equal, -ve, bounds(random and ez)
ERR/WA: loop vars, out of bounds, endl, finish taking inputs, large doubles,
larger INF, corr mod, t RE: double dec, integer overflow(ll or 1LL mult)
*/


Error: WA

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << endl
#define sz(a) int((a).size())
#define endl '\\n' // remove for interactive

const int INF = 1e9;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

void solve() {
  ll mx = -1e9 - 150;
  int n, k;
  cin >> n >> k;
  vector<ll> v(n + 5), c(n + 5);
  REP(i, 1, n) {
    int x;
    cin >> x;
    v[i] = x;
  }
  REP(i, 1, n) {
    int x;
    cin >> x;
    c[i] = x;
  }
  REP(i, 1, n) {
    vi test;
    int length = 0;

    int currind = v[i];
    ll score = c[currind];
    test.pb(c[currind]);
    length++;
    while (currind != i) {
      currind = v[currind];
      test.pb(c[currind]);
      score += c[currind];
      length++;
    }
    // if(i == 2) {watch(length); watch(score); }
    ll sm = 0;
    ll localmx = test[0];
    REP(j, 0, k - 1) {
      sm += test[j];
      localmx = max(localmx, sm);
    }
    mx = max(mx, localmx);
    if (length < k) {
      ll initsm = 1LL * score * (k / length);
      int left = k % length;
      ll localmx = test[0];
      ll localsm = 0;
      REP(j, 0, left - 1) {
        localsm += test[j];
        localmx = max(localmx, localsm);
      }
      mx = max(mx, initsm + localmx);
    }
  }
  cout << mx;
}

int main() {
  // clock_t start, end;
  // start = clock();
  ios_base::sync_with_stdio(0); // cant use scanf, printf
  cin.tie(0);
  cout.tie(
      0); // no longer auto flush cout before each cin, remove for interactive
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(); }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // watch(time_taken);
  ll x = INF;
  x++;
  return 0;
}

/*before sub
TEST: 0, 1, equal, -ve, bounds(random and ez)
ERR/WA: loop vars, out of bounds, endl, finish taking inputs, large doubles,
larger INF, corr mod, t RE: double dec, integer overflow(ll or 1LL mult)
*/