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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }

  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  string s;
  cin >> s;
  ll cnt = 0;
  ll ans = 0;
  rep(i, s.size()) {
    if (s[i] == 'R')
      cnt++;
    else {
      chmax(ans, cnt);
      cnt = 0;
    }
  }
  if (s[s.size() - 1] == 'R')
    chmax(ans, cnt);
  cout << ans << endl;
}

void b() {
  ll N;
  cin >> N;
  auto L = v<>(N, 0);
  rep(i, N) cin >> L[i];
  ll cnt = 0;
  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      for (ll k = j + 1; k < N; k++) {
        if (L[i] == L[j] || L[j] == L[k] || L[i] == L[k])
          continue;
        if (L[i] + L[j] > L[k] && L[j] + L[k] > L[i] && L[k] + L[i] > L[j]) {
          cnt++;
        }
      }
    }
  }

  cout << cnt << endl;
}

void c() {
  ll X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  if (K <= X / D) {
    cout << X - K * D << endl;
  } else {
    ll t = X / D;
    ll q = X % D;
    ll e = K - t;
    if (e % 2) {
      cout << D - q << endl;
    } else {
      cout << q << endl;
    }
  }
}

void d() {
  ll N, K;
  cin >> N >> K;
  auto P = v<>(N, 0);
  auto C = v<>(N, 0);
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, N) cin >> C[i];

  ll ans = -1e18;
  rep(i, N) {
    ll cnt = 0;
    ll X = i;
    auto E = v<>();
    do {
      X = P[X];
      cnt += C[X];
      E.push_back(C[X]);

    } while (X != i);

    ll l = E.size();

    ll t = 0;
    rep(j, l) {
      t += E[j];
      ll now = t;
      if (j + 1 > K)
        break;
      if (cnt > 0) {
        ll e = (K - (j + 1)) / l;
        now += cnt * e;
      }
      chmax(ans, now);
    }
  }

  cout << ans << endl;
}

void e() {}

int main(void) {
  d();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }

  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  string s;
  cin >> s;
  ll cnt = 0;
  ll ans = 0;
  rep(i, s.size()) {
    if (s[i] == 'R')
      cnt++;
    else {
      chmax(ans, cnt);
      cnt = 0;
    }
  }
  if (s[s.size() - 1] == 'R')
    chmax(ans, cnt);
  cout << ans << endl;
}

void b() {
  ll N;
  cin >> N;
  auto L = v<>(N, 0);
  rep(i, N) cin >> L[i];
  ll cnt = 0;
  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      for (ll k = j + 1; k < N; k++) {
        if (L[i] == L[j] || L[j] == L[k] || L[i] == L[k])
          continue;
        if (L[i] + L[j] > L[k] && L[j] + L[k] > L[i] && L[k] + L[i] > L[j]) {
          cnt++;
        }
      }
    }
  }

  cout << cnt << endl;
}

void c() {
  ll X, K, D;
  cin >> X >> K >> D;

  if (D > X) {
    if (K % 2 == 1) {
      cout << D - X << endl;
    } else {
      cout << X << endl;
    }
    return;
  }

  if (X >= D * K) {
    cout << X - D * K << endl;
  } else {
    ll t = X / D;
    ll q = X % D;
    if ((K - t) % 2 == 1) {
      cout << abs(D - q) << endl;
    } else {
      cout << q << endl;
    }
  }
}

void d() {
  ll N, K;
  cin >> N >> K;
  auto P = v<>(N, 0);
  auto C = v<>(N, 0);
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, N) cin >> C[i];

  ll ans = 0;
  rep(i, N) {
    auto V = map<ll, ll>();
    ll s = i;
    ll point = 0;
    ll k = 0;
    do {
      point += C[s];
      chmax(ans, point);
      if (V[k] == 0)
        V[k] = point;
      else
        break;
      s = P[s];
      k++;
    } while (k != K);

    if (k == K)
      continue;
    point += V[K - k];
    chmax(ans, point);
  }

  if (ans == 0) {
    cout << *max_element(all(C)) << endl;
  } else {
    cout << ans << endl;
  }
}

void e() {}

int main(void) {
  d();
  return 0;
}