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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define forn(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define fore(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define rep(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
ll quickpower(ll n, ll k) {
  ll ans = 1;
  while (k) {
    if (k % 2) {
      ans *= n;
      ans %= mod;
    }
    n *= n;
    n %= mod;
    k /= 2;
  }
  return ans;
}
string int_to_string(int n) {
  string s = "";
  while (n) {
    int now = n % 10;
    s += now + '0';
    n /= 10;
  }
  reverse(s.begin(), s.end());
  return s;
}
int string_to_int(string s) {
  int n = 0;
  rep(i, s.size()) {
    n *= 10;
    n += s[i] - '0';
  }
  return n;
}
int n;
ll k;
int p[5555];
ll c[5555];
ll cnt[5555];
bool used[5555];
vi v[5555];
ll ans;
int main() {
  ios::sync_with_stdio(0);
  // think twice,code once
  cin >> n >> k;
  ans = -1000000000000009;
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];
  rep(i, n) {
    memset(used, 0, sizeof(used));
    memset(cnt, 0, sizeof(cnt));
    ll gg = k;
    ll res = 0;
    int num = 0;
    int now = i;
    ll mx = -1000000000000009;
    cnt[now] = 0;
    while (p[now] != i) {

      cnt[p[now]] = cnt[now] + c[p[now]];
      now = p[now];
      v[i].pb(now);
      num++;
    }
    num++;
    v[i].pb(i);
    cnt[i] = cnt[now] + c[i];
    //	rep(j,num)cout<<v[i][j]<<" ";
    //	cout<<endl;
    //	rep(j,num)cout<<cnt[v[i][j]]<<" ";
    //	cout<<endl;
    if (gg <= num) {
      rep(j, gg) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
      ans = max(ans, mx);
      continue;
    }
    if (cnt[i] < 0) {
      rep(j, num) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
      ans = max(ans, mx);
      continue;
    }
    ll g = gg;
    ll q = gg / num;
    //	cout<<q<<endl;
    res += q * cnt[i];
    gg %= num;
    rep(j, gg) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
    res += mx;
    ans = max(ans, res);
    ll res1 = 0;
    mx = -100000000000009;
    rep(j, num) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
    res1 += (q - 1) * cnt[i];
    res1 += mx;
    ans = max(ans, res1);
    //	cout<<ans<<endl;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define forn(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define fore(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define rep(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
ll quickpower(ll n, ll k) {
  ll ans = 1;
  while (k) {
    if (k % 2) {
      ans *= n;
      ans %= mod;
    }
    n *= n;
    n %= mod;
    k /= 2;
  }
  return ans;
}
string int_to_string(int n) {
  string s = "";
  while (n) {
    int now = n % 10;
    s += now + '0';
    n /= 10;
  }
  reverse(s.begin(), s.end());
  return s;
}
int string_to_int(string s) {
  int n = 0;
  rep(i, s.size()) {
    n *= 10;
    n += s[i] - '0';
  }
  return n;
}
int n;
ll k;
int p[5555];
ll c[5555];
ll cnt[5555];
bool used[5555];
vi v[5555];
ll ans;
int main() {
  ios::sync_with_stdio(0);
  // think twice,code once
  cin >> n >> k;
  ans = -1000000000000009;
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];
  rep(i, n) {
    memset(used, 0, sizeof(used));
    memset(cnt, 0, sizeof(cnt));
    ll gg = k;
    ll res = 0;
    int num = 0;
    int now = i;
    ll mx = -1000000000000009;
    cnt[now] = 0;
    while (p[now] != i) {

      cnt[p[now]] = cnt[now] + c[p[now]];
      now = p[now];
      v[i].pb(now);
      num++;
    }
    num++;
    v[i].pb(i);
    cnt[i] = cnt[now] + c[i];
    //	rep(j,num)cout<<v[i][j]<<" ";
    //	cout<<endl;
    //	rep(j,num)cout<<cnt[v[i][j]]<<" ";
    //	cout<<endl;
    if (gg <= num) {
      rep(j, gg) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
      ans = max(ans, mx);
      continue;
    }
    if (cnt[i] < 0) {
      rep(j, num) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
      ans = max(ans, mx);
      continue;
    }
    ll q = gg / num - 1;
    //	cout<<q<<endl;
    res += q * cnt[i];
    gg %= num;
    gg += num;
    forn(j, num, gg - 1) cnt[j] = cnt[j - 1] + v[i][j - now];
    rep(j, gg) if (cnt[v[i][j]] > mx) mx = cnt[v[i][j]];
    res += mx;
    ans = max(ans, res);
    //	cout<<ans<<endl;
  }
  cout << ans << endl;
  return 0;
}