Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    Author: Alam Khan
    AUST CSE 40th Batch

*/

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define pb push_back
#define inf 2e18
#define low -2e18
#define PI acos(-1.0)
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
const int sz = 1e5 + 9;
ll a[sz], p[sz];
int main() {
  ll i, n, t, k, j, x = 0, y = 0, m;
  FAST cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = low;
  ll z = k;
  unordered_map<ll, ll> ma;
  ma.reserve(8196);
  ma.max_load_factor(1.0);
  for (i = 0; i < n; i++) {

    ll now = 0;
    x = i;
    y = 0;
    k = z;
    while (k > 0) {
      if (ma.find(x) != ma.end()) {
        if (k / y != 0) {
          now += ((k / y) - 1) * now;
          k -= ((k / y) - 1) * y;
        }
        ma.clear();
        y = 0;
      }

      now += a[p[x]];
      // v.pb(now);

      ma[x] = 1;
      x = p[x];
      k--;
      y++;
      // cout<<x<<" "<<now<<" "<<k<<endl;
      ans = max(ans, now);
    }
    // cout<<i<<" "<<now<<endl;
    ans = max(ans, now);
    ma.clear();
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*
    Author: Alam Khan
    AUST CSE 40th Batch

*/

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define pb push_back
#define inf 2e18
#define low -2e18
#define PI acos(-1.0)
#define endl "\\n"
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
const int sz = 1e5 + 9;
ll a[sz], p[sz];
int main() {
  ll i, n, t, k, j, x = 0, y = 0, m;
  FAST cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = low;
  ll z = k;
  for (i = 0; i < n; i++) {
    map<ll, ll> ma;
    vector<ll> v;
    ll now = 0;
    x = i;
    y = 0;
    k = z;
    while (k > 0) {
      if (ma.find(x) != ma.end()) {
        if (k / y != 0) {
          now += ((k / y) - 1) * now;
          k -= ((k / y) - 1) * y;
        }
        ma.clear();
        y = 0;
      }

      now += a[p[x]];
      // v.pb(now);

      ma[x] = 1;
      x = p[x];
      k--;
      y++;
      // cout<<x<<" "<<now<<" "<<k<<endl;
      ans = max(ans, now);
    }
    // cout<<i<<" "<<now<<endl;
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}