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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MIN = -1000000000000000000;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  ll answer = MIN;
  vector<bool> vis(n);
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    vis[i] = true;
    vector<int> s;
    s.push_back(c[i]);
    int nxt = p[i];
    while (!vis[nxt]) {
      vis[nxt] = true;
      s.push_back(c[nxt]);
      nxt = p[nxt];
    }
    int sz = s.size();
    vector<ll> m(sz + 1, MIN);
    vector<ll> pre(sz + 1);
    for (int j = 1; j <= sz; j++) {
      pre[j] = pre[j - 1] + s[j - 1];
    }
    vector<ll> lessm(sz + 1, MIN);
    for (int len = 1; len <= sz; len++) {
      m[len] = pre[len];
      for (int end = len + 1; end <= sz; end++) {
        m[len] = max(m[len], pre[end] - pre[end - len]);
      }
      for (int start = 1; start < len; start++) {
        m[len] = max(m[len], pre[start] + pre[sz] - pre[sz - (len - start)]);
      }
      lessm[len] = max(lessm[len - 1], m[len]);
    }
    if (k >= sz && m[sz] <= 0) {
      answer = max(answer, lessm[sz]);
      continue;
    }
    if (k < sz) {
      answer = max(answer, lessm[k]);
      continue;
    }
    int max_cyc = k / sz;
    answer = max(answer, max_cyc * m[sz] + lessm[k - max_cyc * sz]);
    if (k - ((max_cyc - 1) * sz) <= sz)
      answer =
          max(answer, (max_cyc - 1) * m[sz] + lessm[k - (max_cyc - 1) * sz]);
    else
      answer = max(answer, (max_cyc - 1) * m[sz] + lessm[sz]);
    // for (int cyc = 0; cyc <= max_cyc; cyc++) {
    // if (k - cyc * sz > sz) {
    // answer = max(answer, lessm[sz] + cyc * m[sz]);
    //} else {
    // answer = max(answer, lessm[k - cyc * sz] + cyc * m[sz]);
    //}
    //}
  }
  cout << answer << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MIN = -1000000000000000000;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  ll answer = MIN;
  vector<bool> vis(n);
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    vis[i] = true;
    vector<int> s;
    s.push_back(c[i]);
    int nxt = p[i];
    while (!vis[nxt]) {
      vis[nxt] = true;
      s.push_back(c[nxt]);
      nxt = p[nxt];
    }
    int sz = s.size();
    vector<ll> m(sz + 1, MIN);
    vector<ll> pre(sz + 1);
    for (int j = 1; j <= sz; j++) {
      pre[j] = pre[j - 1] + s[j - 1];
    }
    vector<ll> lessm(sz + 1, MIN);
    for (int len = 1; len <= sz; len++) {
      m[len] = pre[len];
      for (int end = len + 1; end <= sz; end++) {
        m[len] = max(m[len], pre[end] - pre[end - len]);
      }
      for (int start = 1; start < len; start++) {
        m[len] = max(m[len], pre[start] + pre[sz] - pre[sz - (len - start)]);
      }
      lessm[len] = max(lessm[len - 1], m[len]);
    }
    int max_cyc = k / sz;
    for (int cyc = 0; cyc <= max_cyc; cyc++) {
      if (k - cyc * sz > sz) {
        answer = max(answer, lessm[sz] + cyc * m[sz]);
      } else {
        answer = max(answer, lessm[k - cyc * sz] + cyc * m[sz]);
      }
    }
  }
  cout << answer << endl;
}