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

#include <bits/stdc++.h>
#include <cstring>
// #define Mfc_Tanzim
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) (v.begin(), v.end())
#define pi acos(-1.0)
#define FastRead                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define bug(a) cerr << #a << " : " << a << endl
#define pq dp
using namespace std;
const ll mx = 4444444444;

ll posx[] = {1, -1, 0, 0};
ll posy[] = {0, 0, 1, -1};
vector<ll> v, v2;
ll ret = INT_MIN;

int main() {
  FastRead

#ifdef Mfc_Tanzim
      freopen("input.txt", "r", stdin);
  // freopen("output.txt","w", stdout);
#endif /// Mfc_Tanzim

  int t = 1, n, k, m, a, b, c, d;
  // cin >> t;

  while (t--) {
    cin >> n >> k;
    v.resize(n);
    v2.resize(n);
    for (auto &i : v) {
      cin >> i;
    }
    for (auto &i : v2) {
      cin >> i;
    }
    bool vis[n + 1];
    ll ans = INT_MIN, nn = n;
    for (int j = 0; j < nn; j++) {
      n = j + 1;
      // check(i+1, k, vis);
      vector<ll> res;
      ll r = 0;
      memset(vis, false, sizeof(vis));
      while (!vis[n - 1]) {
        r += v2[n - 1];
        vis[n - 1] = true;
        res.pb(v2[n - 1]);
        n = v[n - 1];
      }
      for (int i = 0; i < res.size(); i++) {
        if (i) {
          res[i] += res[i - 1];
        }
        if (i + 1 <= k) {
          ans = max(ans, res[i]);
        } else {
          break;
        }
        if (r) {
          ll kk = res[i] + ((k - i - 1) / res.size()) * r;
          ans = max(ans, kk);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cstring>
// #define Mfc_Tanzim
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) (v.begin(), v.end())
#define pi acos(-1.0)
#define FastRead                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define bug(a) cerr << #a << " : " << a << endl
#define pq dp
using namespace std;
const ll mx = 4444444444;

ll posx[] = {1, -1, 0, 0};
ll posy[] = {0, 0, 1, -1};
vector<ll> v, v2;
ll ret = INT_MIN;
map<ll, bool> vis;

void Dfs(ll n, ll k) {
  vector<ll> res;
  vis.clear();
  ll r = 0;
  while (!vis[n - 1]) {
    r += v2[n - 1];
    vis[n - 1] = true;
    res.pb(v2[n - 1]);
    n = v[n - 1];
  }
  ll ans = INT_MIN;
  for (ll i = 0; i < res.size(); i++) {
    if (i) {
      res[i] += res[i - 1];
    }
    if (i + 1 <= k) {
      ans = max(ans, res[i]);
    } else {
      break;
    }
    if (r) {
      ll kk = res[i] + (k - i - 1) / res.size() * r;
      ans = max(ans, kk);
    }
  }
  ret = max(ret, ans);
}

int main() {
  FastRead

#ifdef Mfc_Tanzim
      freopen("input.txt", "r", stdin);
  // freopen("output.txt","w", stdout);
#endif /// Mfc_Tanzim

  ll t = 1, n, k, m, a, b, c, d;
  // cin >> t;

  while (t--) {
    cin >> n >> k;
    v.resize(n);
    v2.resize(n);
    for (auto &i : v) {
      cin >> i;
    }
    for (auto &i : v2) {
      cin >> i;
    }
    for (ll i = 0; i < n; i++) {
      Dfs(v[i], k);
    }
    cout << ret << endl;
  }
  return 0;
}