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


#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  int P[N];
  ll C[N];
  rep(i, N) {
    cin >> P[i];
    --P[i];
  }
  // rep(i,N){cout<<P[i]<<" ";}
  cout << endl;
  rep(i, N) { cin >> C[i]; }
  // rep(i,N){cout<<C[i]<<" ";}
  cout << endl;

  ll ans = -1e18;
  rep(sz, N) {
    int np = P[sz];
    ll tot = C[np];
    vector<int> s;
    s.push_back(np);
    while (1) {
      np = P[np];
      tot += C[np];
      s.push_back(np);
      if (np == sz) {
        break;
      }
    }

    size_t n = s.size();
    ll t = 0;
    rep(i, n) {

      t += C[s[i]];

      if (i + 1 > K)
        break;
      ll now = t;
      // cout<<"tot:"<<tot<<endl;
      if (tot > 0) {
        ll e = (K - (i + 1)) / n;
        now += e * tot;
      }
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:



#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_set>
#include <utility>

#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;

using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N + 1, 0);
  vector<ll> C(N + 1, 0);

  for (ll i = 1; i <= N; ++i) {
    cin >> P[i];
  }

  for (ll i = 1; i <= N; ++i) {
    cin >> C[i];
  }
  vector<vector<ll>> CP(K + 1, vector<ll>(N + 1, 0));

  for (int i = 0; i <= N; ++i) {
    CP[0][i] = i;
  }

  ll k = 0;
  ll a = 0;
  bool flag = true;

  // cout<<"----"<<endl;
  for (k = 1; k < K; ++k) {

    for (int i = 1; i <= N; ++i) {

      CP[k][P[i]] = CP[k - 1][i];
    }

    for (int a = 0; a < k; ++a) {
      if (CP[k] == CP[a]) {
        flag = false;
        break;
      }
    }

    if (flag == false)
      break;
  }

  // cout<<"a:"<<a<<endl;
  // cout<<"k:"<<k<<endl;

  vector<vector<ll>> PC(k + 1, vector<ll>(N + 1, 0));
  ll mx = -100000000000000000;
  /*
   for(ll i=1;i<=N;++i){
   PC[0][i]=C[i];
   }
   */

  for (ll l = 1; l <= k; ++l) {
    for (ll i = 1; i <= N; ++i) {
      PC[l][P[i]] = C[P[i]] + PC[l - 1][i];
      mx = max(PC[l][P[i]], mx);
    }
  }
  /*
  cout<<"=============="<<endl;


       rep(i,k+1){
           rep(j,N+1){
               cout<<PC[i][j]<<" ";
           }
           cout<<endl;
       }

  cout<<"=============="<<endl;
  */
  // cout<<"=============="<<endl;

  ll ans = 0;
  ll b = k - a;
  ll p = (K - a) / b;
  ll q = (K - a) % b;
  ll mx_tmp = 0;

  // vector<vector<ll> > vec(k+1,vector<ll>(N+1,0));

  if (mx < 0 || k >= K) {
    ans = mx;
  } else {

    for (ll i = 0; i <= k - a; ++i) {
      for (ll j = 0; j <= N; ++j) {

        ll tmp = PC[a + i][j] + (p - 1) * (PC[k][j] - PC[a][j]);
        mx_tmp = max(tmp, mx_tmp);

        // cout<<tmp<<" ";
      }
      // cout<<endl;
    }

    /*
    rep(i,k+1){
        rep(j,N+1){
            cout<<PC[i][j]<<" ";
        }
        cout<<endl;
    }

       cout<<"=============="<<endl;
    */
    for (ll i = 0; i <= q; ++i) {
      for (ll j = 0; j <= N; ++j) {
        ll tmp = PC[a + i][j] + (p) * (PC[k][j] - PC[a][j]);
        mx_tmp = max(tmp, mx_tmp);

        // cout<<tmp<<" ";
      }
      // cout<<endl;
    }
    /*
     cout<<"=============="<<endl;

      rep(i,k+1){
          rep(j,N+1){
              cout<<vec[i][j]<<" ";
          }
          cout<<endl;
      }
         cout<<"=============="<<endl;
     */
    ans = mx_tmp;
  }

  //  cout<<"=============="<<endl;

  //  cout<<"p:"<<p<<endl;
  // cout<<"q:"<<q<<endl;

  /*
  rep(i,K+1){
      rep(j,N+1){
          cout<<CP[i][j]<<" ";
      }
      cout<<endl;
  }
   */
  cout << ans << endl;
}