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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef long long ll;
const long long MOD = 1000000007;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, m, n) for (ll i = (m); i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back

int main() {

  ll n, k;
  cin >> n >> k;
  vector<ll> orders(n + 1);
  vector<ll> scores(n + 1);

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    orders.at(i + 1) = tmp;
  }

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    scores.at(i + 1) = tmp;
  }

  ll realanswer = -10000000000000;

  for (ll i = 1; i <= n; i++) {

    ll answer;

    vector<ll> cycle;

    cycle.pb(i);

    while (orders.at(cycle.at(int(cycle.size()) - 1)) != cycle.at(0)) {
      ll tmp = orders.at(cycle.at(int(cycle.size()) - 1));
      cycle.pb(tmp);
    }

    ll loopsize = int(cycle.size());

    ll loopsum = 0;

    rep(j, loopsize) { loopsum += scores.at(orders.at(cycle.at(j))); }

    if (loopsum < 0 or k <= loopsize) {

      ll isum = scores.at(orders.at(i));

      ll tmpmax = isum;

      for (int z = 1; z < min(k, loopsize); z++) {
        isum += scores.at(orders.at(cycle.at(z)));
        if (isum > tmpmax) {
          tmpmax = isum;
        }
      }

      answer = tmpmax;
    }

    if (loopsum >= 0 and k > loopsize) {

      ll howmany = k / loopsize;

      ll isum = (howmany - 1) * loopsum;

      ll tmpmax = 0;

      ll tmpsum = 0;

      ll kaisu = k - ((howmany - 1) * loopsize);

      rep(z, kaisu) {
        tmpsum += scores.at(orders.at(cycle.at(z % loopsize)));
        if (tmpsum > tmpmax) {
          tmpmax = tmpsum;
        }
      }

      answer = tmpmax + isum;
    }

    if (answer > realanswer) {
      realanswer = answer;
    }
  }

  cout << realanswer << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef long long ll;
const long long MOD = 1000000007;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, m, n) for (ll i = (m); i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back

int main() {

  ll n, k;
  cin >> n >> k;
  vector<ll> orders(n + 1);
  vector<ll> scores(n + 1);

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    orders.at(i + 1) = tmp;
  }

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    scores.at(i + 1) = tmp;
  }

  ll realanswer = -10000000000000;

  for (ll i = 1; i <= n; i++) {

    ll answer;

    vector<ll> cycle;

    set<ll> checker;

    cycle.pb(i);
    checker.insert(i);

    while (checker.find(orders.at(cycle.at(int(cycle.size()) - 1))) ==
           checker.end()) {
      ll tmp = orders.at(cycle.at(int(cycle.size()) - 1));
      cycle.pb(tmp);
      checker.insert(tmp);
    }

    ll loopsize = int(cycle.size());

    ll loopsum = 0;

    rep(j, loopsize) { loopsum += scores.at(orders.at(cycle.at(j))); }

    if (loopsum < 0 or k <= loopsize) {

      ll isum = scores.at(orders.at(i));

      ll tmpmax = isum;

      for (int z = 1; z < min(k, loopsize); z++) {
        isum += scores.at(orders.at(cycle.at(z)));
        if (isum > tmpmax) {
          tmpmax = isum;
        }
      }

      answer = tmpmax;
    }

    if (loopsum >= 0 and k > loopsize) {

      ll howmany = k / loopsize;

      ll isum = (howmany - 1) * loopsum;

      ll tmpmax = 0;

      ll tmpsum = 0;

      ll kaisu = k - ((howmany - 1) * loopsize);

      rep(z, kaisu) {
        tmpsum += scores.at(orders.at(cycle.at(z % loopsize)));
        if (tmpsum > tmpmax) {
          tmpmax = tmpsum;
        }
      }

      answer = tmpmax + isum;
    }

    if (answer > realanswer) {
      realanswer = answer;
    }
  }

  cout << realanswer << endl;
}