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

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\\n"
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);
ll compute_neg(deque<ll> cost, ll k) {
  ll n = cost.size();
  ll res = INT_MIN;
  ll acc = 0;
  for (int i = 0; i < min(n, k); i++) {
    acc += cost[i % n];
    res = max(res, acc);
  }
  return res;
}
ll compute_pos(deque<ll> cost, ll k, ll sum) {
  ll n = cost.size();
  ll res = INT_MIN;
  ll times = k / n;
  ll acc = 0;
  if (times > 3) {
    ll zip = times - 3;
    acc += zip * sum;
    k -= zip * n;
  }
  res = acc;
  for (int i = 0; i < k; i++) {
    acc += cost[i % n];
    res = max(res, acc);
  }
  return res;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> perm(n, 0);
  vector<ll> cost(n, 0);
  for (auto &val : perm) {
    cin >> val;
    val -= 1;
  }
  for (auto &val : cost)
    cin >> val;
  vector<vector<int>> grp;
  vector<int> visit(n, 0);
  auto dfs = [&](auto &self, int src, int cur, vector<int> &st) -> void {
    if (cur == src) {
      grp.push_back(st);
      return;
    }
    st.push_back(cur);
    visit[cur] = 1;
    self(self, src, perm[cur], st);
  };
  for (int i = 0; i < n; i++) {
    if (visit[i] == 0) {
      vector<int> tmp = {i};
      visit[i] = 1;
      dfs(dfs, i, perm[i], tmp);
    }
  }
  ll ans = INT_MIN;
  for (auto vec : grp) {
    deque<ll> cost2;
    for (int idx : vec) {
      cost2.push_back(cost[idx]);
    }
    ll cycle = vec.size();
    ll acc = accumulate(cost2.begin(), cost2.end(), 0LL);
    for (int i = 0; i < cycle; i++) {
      if (acc < 0)
        ans = max(ans, compute_neg(cost2, k));
      else
        ans = max(ans, compute_pos(cost2, k, acc));
      ll head = cost2.front();
      cost2.push_back(head);
      cost2.pop_front();
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\\n"
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);
ll compute_neg(deque<ll> cost, ll k) {
  ll n = cost.size();
  ll res = INT_MIN;
  ll acc = 0;
  for (int i = 0; i < k; i++) {
    acc += cost[i % n];
    res = max(res, acc);
  }
  return res;
}
ll compute_pos(deque<ll> cost, ll k, ll sum) {
  ll n = cost.size();
  ll res = INT_MIN;
  ll times = k / n;
  ll acc = 0;
  if (times > 3) {
    ll zip = times - 3;
    acc += zip * sum;
    k -= zip * n;
  }
  res = acc;
  for (int i = 0; i < k; i++) {
    acc += cost[i % n];
    res = max(res, acc);
  }
  return res;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> perm(n, 0);
  vector<ll> cost(n, 0);
  for (auto &val : perm) {
    cin >> val;
    val -= 1;
  }
  for (auto &val : cost)
    cin >> val;
  vector<vector<int>> grp;
  vector<int> visit(n, 0);
  auto dfs = [&](auto &self, int src, int cur, vector<int> &st) -> void {
    if (cur == src) {
      grp.push_back(st);
      return;
    }
    st.push_back(cur);
    visit[cur] = 1;
    self(self, src, perm[cur], st);
  };
  for (int i = 0; i < n; i++) {
    if (visit[i] == 0) {
      vector<int> tmp = {i};
      visit[i] = 1;
      dfs(dfs, i, perm[i], tmp);
    }
  }
  ll ans = INT_MIN;
  for (auto vec : grp) {
    deque<ll> cost2;
    for (int idx : vec) {
      cost2.push_back(cost[idx]);
    }
    ll cycle = vec.size();
    ll acc = accumulate(cost2.begin(), cost2.end(), 0LL);
    for (int i = 0; i < cycle; i++) {
      if (acc < 0)
        ans = max(ans, compute_neg(cost2, k));
      else
        ans = max(ans, compute_pos(cost2, k, acc));
      ll head = cost2.front();
      cost2.push_back(head);
      cost2.pop_front();
    }
  }
  cout << ans << endl;
  return 0;
}