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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

long long int f(const vector<int> &v, int k, const vector<int> &vc) {
  vector<long long int> val;
  for (int i = 0; i < v.size(); i++) {
    val.push_back(vc[v[i]]);
  }
  int flag = 1;
  for (int i = 0; i < val.size(); i++) {
    if (val[i] > 0)
      flag = 0;
  }
  if (flag == 1) {
    return *max_element(val.begin(), val.end());
  }
  long long int s = accumulate(val.begin(), val.end(), (long long int)0);
  long long int ret = 0;
  int n = v.size();
  if (s >= 0 && k >= v.size()) {
    if (k % n == 0) {
      ret += s * (k / v.size() - 1);
      k = v.size();
    } else {

      ret += s * (k / v.size());
      k %= v.size();
    }
  }
  long long int res = -0x3f3f3f3f;
  for (int i = 0; i < n; i++) {
    val.push_back(val[i]);
  }
  long long int r = 0;
  int l = 0;
  for (int i = 0; i < 2 * n; i++) {
    if (l == k) {
      r -= val[i - l];
      l--;
    }
    r += val[i];
    l++;
    if (r < 0) {
      r = 0;
      l = 0;
    }
    res = max(r, res);
  }
  while (l > 0) {
    r -= val[2 * n - l];
    res = max(res, r);
    l--;
  }
  return max(res + ret, ret);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vp(n + 1, 0), vc(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> vp[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> vc[i];
  }
  vector<int> v(n + 1, 0);
  long long int res = -0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    if (v[i] == 1)
      continue;
    int index = i;
    vector<int> myv;
    while (v[index] == 0) {
      v[index] = 1;
      myv.push_back(index);
      index = vp[index];
    }

    res = max(res, f(myv, k, vc));
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

long long int f(const vector<int> &v, int k, const vector<int> &vc) {
  vector<long long int> val;
  for (int i = 0; i < v.size(); i++) {
    val.push_back(vc[v[i]]);
  }
  int flag = 1;
  for (int i = 0; i < val.size(); i++) {
    if (val[i] > 0)
      flag = 0;
  }
  if (flag == 1) {
    return *max_element(val.begin(), val.end());
  }
  long long int s = accumulate(val.begin(), val.end(), (long long int)0);
  long long int ret = 0;
  int n = v.size();
  if (s >= 0 && k >= v.size()) {
    if (k % n == 0) {
      ret += s * (k / v.size() - 1);
      k = v.size();
    } else {

      ret += s * (k / v.size());
      k %= v.size();
    }
  }
  long long int res = -0x3f3f3f3f;
  for (int i = 0; i < v.size(); i++) {
    long long int r = 0;
    for (int j = 0; j < k; j++) {
      r += val[(i + j) % n];
      res = max(res, r);
    }
  }
  return max(res + ret, ret);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vp(n + 1, 0), vc(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> vp[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> vc[i];
  }
  vector<int> v(n + 1, 0);
  long long int res = -0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    if (v[i] == 1)
      continue;
    int index = i;
    vector<int> myv;
    while (v[index] == 0) {
      v[index] = 1;
      myv.push_back(index);
      index = vp[index];
    }

    res = max(res, f(myv, k, vc));
  }
  cout << res << endl;
}