alexjercan/bugnet huggingface dataset:
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define ll long long

const int N = 1e6 + 100;
const int inf = 0x3f3f3f3f;

ll p[N], c[N];

ll vis[6000], g[6000];
ll n, k, step;
void A(int s) {
  while (1) {
    if (vis[s])
      return;
    step++;
    g[step] = g[step - 1] + c[p[s]];
    vis[s] = 1;
    s = p[s];
  }
}
void solve() {

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];

  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    ll kk = k;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++)
      g[i] = -1e18;
    step = 0;
    A(i);
    ll ma = g[step];
    for (int i = 2; i <= step; i++) {
      g[i] = max(g[i], g[i - 1]);
    }
    // if(step>=kk) ans=max(ans,g[step]);
    // for(int i=1;i<=step;i++) cout<<g[i]<<" ";
    // cout<<endl;
    ll ty = kk / step;

    if (ty >= 1) {
      ans = max(max(ans, g[step]),
                max(g[kk % step] + ty * ma, g[step] + (ty - 1) * ma));
    } else {
      ans = max(ans, g[kk % step]);
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long

const int N = 1e6 + 100;

ll ma = 1e18;

ll p[N], c[N];
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  ll ans = -1e18;
  for (int i = 1; i <= n; i++) {
    int kk = k;
    ll sum = 0;
    int s = i;
    while (kk--) {
      sum += c[p[s]];
      ans = std::max(sum, ans);
      s = p[s];
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  solve();
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int N;
  long long K;
  cin >> N >> K;

  vector<int> P;
  vector<long long> C;

  P.resize(N);
  C.resize(N);

  long long highscore, score;

  for (auto &e : P)
    cin >> e;

  for (auto &e : C)
    cin >> e;

  highscore = C[0];

  vector<long long> loop, loopLength;
  loop.resize(N);
  loopLength.resize(N);

  int i;
  for (int initial = 0; initial < N; initial++) {
    i = initial;
    loop[initial] = C[i];
    loopLength[initial] = 1;
    i = P[i] - 1;

    for (long long k = 1; i != initial; k++) {
      loop[initial] += C[i];
      loopLength[initial] = k + 1;
      i = P[i] - 1;
    }
  }

  for (int initial = 0; initial < N; initial++) {
    if (loop[initial] >= 0) {
      long long tmp = (long long)(K / loopLength[initial]) - 1;
      long long K2 = (long long)(K % loopLength[initial]) + loopLength[initial];
      score = loop[initial] * tmp;
      int i = initial;

      if (tmp == -1) {
        i = P[i] - 1;
        score = C[i];
        if (highscore < score)
          highscore = score;

        for (int k = 1; k < K; k++) {
          i = P[i] - 1;
          score += C[i];
          if (highscore < score)
            highscore = score;
        }
      } else {
        if (highscore < score)
          highscore = score;
        for (int k = 0; k < K2; k++) {
          if (highscore < score)
            highscore = score;
          i = P[i] - 1;
          score += C[i];
          if (highscore < score)
            highscore = score;
        }
      }
    } else {
      i = initial;
      long long K2;
      if (K > loopLength[initial])
        K2 = loopLength[initial] + 1;
      else
        K2 = K;
      score = 0;

      for (int k = 0; k < K2; k++) {
        i = P[i] - 1;
        score += C[i];
        if (highscore < score)
          highscore = score;
      }
    }
  }

  cout << highscore << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int N;
  long long K;
  cin >> N >> K;

  vector<int> P;
  vector<long> C;

  P.resize(N);
  C.resize(N);

  long long highscore, score;

  for (auto &e : P)
    cin >> e;

  for (auto &e : C)
    cin >> e;

  highscore = C[P[0] - 1];

  for (int initial = 0; initial < N; initial++) {
    int i = initial;
    i = P[i] - 1;
    score = C[i];
    if (highscore < score)
      highscore = score;
    for (int k = 1; k < K; k++) {
      i = P[i] - 1;
      score += C[i];
      if (highscore < score)
        highscore = score;
    }
  }

  cout << highscore << endl;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
#define nINF -2000000000000000

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> c(n);
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    a--;
    p[i] = a;
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    c[i] = a;
  }

  ll ans = nINF;
  for (int start = 0; start < n; start++) {
    // 1repを確認
    vector<int> dist(n, -1);
    int replen = 0;
    ll repsum = 0;
    int now = start;
    ll nowans = nINF;
    ll nowsum = 0;
    ll repmax = nINF;
    while (1) {
      now = p[now];
      // cout << now << "→";
      if (dist[now] != -1)
        break;
      dist[now]++;
      repsum += c[now];
      replen++;
      repmax = max(repmax, repsum);
    }
    int reptime = 0;
    if (repsum > 0)
      reptime = k / replen;
    int last = replen;
    if (repsum > 0)
      last = k % replen;
    /*cout << "start:" << start << endl;
    cout << "replen:" << replen << endl;
    cout << "repsum:" << repsum << endl;
    cout << "reptime:" << reptime << endl;
    cout << "last:" << last << endl;*/

    // repsum>0ならできるだけ回す
    if (repsum > 0)
      nowsum = repsum * reptime;
    now = start;
    int flag = 0;
    if (last == replen) {
      flag = 1;
      now = p[now];
      nowsum = c[now];
    }
    nowans = nowsum;
    for (int i = flag; i < last; i++) {
      now = p[now];
      nowsum += c[now];
      nowans = max(nowans, nowsum);
    }

    // cout << nowans << "→";
    if (repsum > 0 && reptime > 0) {
      nowans = max(nowans, repsum * (reptime - 1) + repmax);
    }
    // nowans=max(nowans,repsum*max(0,reptime-1)+repmax);
    // cout << "nowans:" << nowans <<  endl;
    ans = max(ans, nowans);
    // cout << "ans:" << ans << endl << endl;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> c[i];

  ll ans;
  for (int i = 0; i < n; i++) {
    int time = 0;
    int now = i;
    ll sum = 0;
    if (i == 0)
      ans = c[p[0]];
    while (1) {
      time++;
      if (time >= k + 1)
        break;
      now = p[now];
      sum += c[now];
      ans = max(ans, sum);
    }
  }

  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

int main() {
  int N, K;
  std::cin >> N >> K;
  std::vector<int> P(N);
  std::vector<int> C(N);
  for (int i = 0; i < N; i++) {
    std::cin >> P[i];
    P[i]--;
  }
  for (int i = 0; i < N; i++) {
    std::cin >> C[i];
  }

  long long max = -1e18;

  for (int i = 0; i < N; i++) {
    int cnt = 1;
    int index = P[i];
    long long score = C[i];
    long long max_score = C[i];
    while (i != index && cnt < K) {
      score += C[index];
      if (max_score < score) {
        max_score = score;
      }
      index = P[index];
      cnt++;
    }
    if (i == index) {
      if (score > 0) {
        int loop_N = K / cnt - (K % cnt == 0) - 1;
        if (loop_N > 0) {
          score = score * loop_N;
          cnt = cnt * loop_N;
          if (max_score < score) {
            max_score = score;
          }
        }
        while (cnt < K) {
          score += C[index];
          if (max_score < score) {
            max_score = score;
          }
          index = P[index];
          cnt++;
        }
      }
    }

    if (max < max_score) {
      max = max_score;
    }
  }

  std::cout << max << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int N, K;
  std::cin >> N >> K;
  std::vector<int> P(N);
  std::vector<int> C(N);
  for (int i = 0; i < N; i++) {
    std::cin >> P[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> C[i];
  }

  long long max = -1e18;
  for (int i = 0; i < N; i++) {
    std::vector<bool> first(N, true);
    long long cur_max = -1e18;
    long long score = 0;
    int cnt = 0;
    int index = i;
    while (first[index] && cnt < K) {
      score += C[index];
      cnt++;
      index = P[index] - 1;

      if (cur_max < score) {
        cur_max = score;
      }
    }
    if (cnt == K) {
      if (max < cur_max) {
        max = cur_max;
      }
    } else {
      if (score < 0) {
        if (max < cur_max) {
          max = cur_max;
        }
      } else {
        score = score * (K / cnt);
        cur_max = score;
        cnt = K % cnt;
        index = i;
        for (int j = 0; j < cnt; j++) {
          score += C[index];
          index = P[index] - 1;

          if (cur_max < score) {
            cur_max = score;
          }
        }
        if (max < cur_max) {
          max = cur_max;
        }
      }
    }
  }

  std::cout << max << std::endl;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int n, p[5003], len;
int K;
ll C[5002], ans = -INF, ret[5002];
void dfs(int x, int w) {
  if (x == w)
    return;
  ++len;
  ret[len] = ret[len - 1] + C[p[x]];
  dfs(p[x], w);
}
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> C[i], ans = max(ans, C[i]);
  for (int i = 1; i <= n; i++) {
    ret[1] = C[p[i]];
    len = 1;
    dfs(p[i], i);
    for (int j = 1; j <= min(len, K); j++)
      ans = max(ans, ret[j]);
    if (K > len && ret[len] > 0) {
      ll pr = K / len, ps = K % len, res = pr * ret[len];
      for (int j = 0; j <= ps; j++)
        ans = max(ans, res + ret[j]);
      for (int j = 0; j <= len; j++)
        ans = max(ans, res - ret[len] + ret[j]);
    }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAX = 1e4 + 10;
ll p[MAX], c[MAX], pp[MAX];

int solve() {
  int n, kk;
  cin >> n >> kk;
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &p[i]);
  }
  for (int j = 1; j <= n; ++j) {
    scanf("%lld", &c[j]);
  }
  ll maxs = -1e9 - 1;
  for (int i = 1; i <= n; ++i) {
    memset(pp, 0, sizeof(pp));
    ll sum = c[p[i]];
    pp[i] = 1;
    if (sum > maxs)
      maxs = sum;
    int k = p[i], len = 1, flag = 0;
    for (int j = 2; j <= kk; ++j) {
      sum += c[p[k]], len++, pp[k] = 1, k = p[k];
      if (pp[k])
        sum = sum * (kk / len), flag = 1;
      if (sum > maxs)
        maxs = sum;
      if (sum <= 0 || flag)
        break;
    }
  }
  cout << maxs << endl;
}

int main() {
  int _ = 1;
  //    cin>>_;
  while (_--) {
    solve();
  }
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

std::vector<int64_t> p;
std::vector<int64_t> c;

auto main() -> int {
  int64_t n, k;
  std::cin >> n >> k;
  p.reserve(n);
  c.reserve(n);

  for (int64_t i = 0; i < n; ++i) {
    int64_t t;
    std::cin >> t;
    p.emplace_back(t - 1);
  }

  for (int64_t i = 0; i < n; ++i) {
    int64_t t;
    std::cin >> t;
    c.emplace_back(t);
  }

  int64_t bestScore = INT64_MIN;

  for (int64_t i = 0; i < n; ++i) {
    int64_t idx = i;
    int64_t totalScore = 0;
    std::vector<int64_t> scores;

    do {
      idx = p[idx];
      scores.push_back(c[idx]);
      totalScore += c[idx];
    } while (idx != i);

    int64_t tmp = 0;
    uint64_t size = scores.size();
    for (uint64_t j = 0; (j < size) && ((j + 1) <= k); ++j) {
      tmp += scores[j];

      int64_t current = tmp;
      if (totalScore > 0) {
        int64_t e = (k - j - 1) / size;
        current += totalScore * e;
      }

      if (bestScore < current)
        bestScore = current;
    }
  }

  std::cout << bestScore << std::flush;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

auto main() -> int {
  int64_t n, k;
  std::cin >> n >> k;
  std::vector<int64_t> p;
  std::vector<int64_t> c;
  n += 1;
  p.reserve(n);
  p.emplace_back(0);
  c.reserve(n);
  c.emplace_back(0);

  for (int64_t i = 1; i < n; ++i) {
    int64_t t;
    std::cin >> t;
    p.emplace_back(t);
  }

  for (int64_t i = 1; i < n; ++i) {
    int64_t t;
    std::cin >> t;
    c.emplace_back(t);
  }

  std::vector<int64_t> candidates;
  candidates.resize(n, 0);

  for (int64_t i = 1; i < n; ++i) {
    std::vector<int64_t> temp;
    temp.reserve(k);
    int64_t idx = i;
    temp.emplace_back(c[p[idx]]);
    idx = p[idx];
    for (int64_t j = 1; j < k; ++j) {
      temp.emplace_back((*temp.rbegin()) + c[p[idx]]);
      idx = p[idx];
    }

    int64_t max = temp[0];
    for (uint64_t j = 1; j < temp.size(); ++j)
      if (max < temp[j])
        max = temp[j];
    candidates[i] = max;
  }

  int64_t ans = candidates[1];
  for (uint64_t i = 2; i < candidates.size(); ++i)
    if (ans < candidates[i])
      ans = candidates[i];

  std::cout << ans << std::flush;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];
  rep(i, n)-- p[i];

  ll ans = INT_MIN;

  rep(i, n) {
    vector<int> loop; // loopする順列
    int x = i;
    ll loop_sum = 0;

    while (1) {
      loop.emplace_back(c[x]);
      loop_sum += c[x];
      x = p[x];
      if (x == i)
        break;
    }

    int l = loop.size();
    ll rem_sum = 0;
    ll ans_sum = 0;
    rep(st, l) {
      rem_sum += loop[st];
      if (st + 1 > k)
        break;
      ll e = (k - (st + 1)) / l;
      ans_sum = rem_sum + max((ll)0, loop_sum) * e;
      ans = max(ans, ans_sum);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> p(n);
  vector<int> c(n);

  rep(i, n) cin >> p[i];
  rep(i, n) cin >> c[i];

  rep(i, n)-- p[i];

  ll score = 0;
  ll scoreLocalMax = INT_MIN;
  ll scoreGlobalMax = INT_MIN;
  int cnt = 0;

  rep(start, n) // start 初期位置
  {
    cnt = 0;
    scoreLocalMax = INT_MIN;
    score = 0;

    int idx = p[start];

    while (cnt < k) {
      score += c[idx];
      if (scoreLocalMax < score)
        scoreLocalMax = score;

      ++cnt;
      idx = p[idx];
    }

    if (scoreGlobalMax < scoreLocalMax)
      scoreGlobalMax = scoreLocalMax;
  }

  cout << scoreGlobalMax;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
typedef long long int lli;
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < n; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define forll(i, a, b) for (lli i = a; i < b; i++)
#define mp make_pair
#define pb emplace_back
#define sortv(v) sort(v.begin(), v.end())
#define debug(x) cout << #x << " : " << x << endl
#define all(x) x.begin(), x.end()
const int mod1 = 1000000007;
const int mod2 = 998244353;

int main() {
  optimize
#ifndef ONLINE_JUDGE
      freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  forn(i, n) cin >> p[i];
  vector<int> c(n);
  forn(i, n) cin >> c[i];

  vector<lli> rep(n);
  vector<lli> repv(n);
  lli ans = LLONG_MIN;
  forn(i, n) {
    int j = i;
    int cnt = 0;
    lli t = 0LL;
    // set<int>prev;
    vector<int> prev(n, 0);
    while (prev[j] == 0) {
      t += c[j];
      cnt++;
      if (cnt <= k)
        ans = max(ans, t);
      // prev.insert(j);
      prev[j] = 1;
      j = p[j] - 1;
    }
    // debug(cnt);
    // if(cnt+1<=k)
    // 	ans = max(ans, t + c[i]);
    rep[i] = (lli)cnt;
    repv[i] = t;
  }

  // debug(ans);
  forn(i, n) {
    // debug(rep[i]);
    if (k < rep[i] || repv[i] < 0)
      continue;
    lli t = repv[i] * (k / rep[i] - 1);
    // debug(t);
    ans = max(ans, t);
    int x = i;
    forn(tt, k % rep[i] + rep[i]) {
      t += c[x];
      ans = max(ans, t);
      x = p[x] - 1;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
typedef long long int lli;
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < n; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define forll(i, a, b) for (lli i = a; i < b; i++)
#define mp make_pair
#define pb emplace_back
#define sortv(v) sort(v.begin(), v.end())
#define debug(x) cout << #x << " : " << x << endl
#define all(x) x.begin(), x.end()
const int mod1 = 1000000007;
const int mod2 = 998244353;

int main() {
  optimize
#ifndef ONLINE_JUDGE
      freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif

  int n, k;
  cin >> n >> k;
  vector<int> p(n);
  forn(i, n) cin >> p[i];
  vector<int> c(n);
  forn(i, n) cin >> c[i];

  lli ans = LLONG_MIN;
  forn(i, n) {
    lli t = 0LL;
    int x = p[i] - 1;
    forn(tt, k) {
      t += c[x];
      ans = max(ans, t);
      x = p[x] - 1;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n, k;
  ll r;
  cin >> n >> k;
  vector<vector<ll>> v(n);
  vector<ll> p(n), c(n), siz(n, 0);
  for (ll &i : p)
    cin >> i, --i;
  for (ll &i : c)
    cin >> i;
  r = c[0];
  for (ll i = 0; i < n; ++i) {
    ll cur = i, sum = 0;
    do {
      ++siz[i];
      v[i].push_back(sum += c[cur]);
      cur = p[cur];
    } while (cur != i);
  }
  for (ll i = 0; i < n; ++i) {
    ll kt = k, rt = LLONG_MIN;
    ll s = 0, tail = *(v[i].end() - 1);
    if (kt > siz[i] * 2 && tail > 0) {
      s = tail * (k / siz[i] - 1);
      kt = k % siz[i] + siz[i];
    }
    for (ll j = 0; j < min(kt, siz[i] * 2); ++j)
      r = max(r, s + v[i][j % siz[i]] + tail * (j / siz[i]));
  }
  cout << r;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n, k;
  ll r;
  cin >> n >> k;
  vector<vector<ll>> v(n);
  vector<ll> p(n), c(n), siz(n, 0);
  for (ll &i : p)
    cin >> i, --i;
  for (ll &i : c)
    cin >> i;
  r = c[0];
  cerr << "{C}:";
  for (ll i : c)
    cerr << " " << i;
  cerr << endl;
  cerr << "{P}:";
  for (ll i : p)
    cerr << " " << i;
  cerr << endl;
  for (ll i = 0; i < n; ++i) {
    ll cur = i, sum = 0;
    do {
      ++siz[i];
      v[i].push_back(sum += c[cur]);
      cerr << "[" << cur << "]" << sum << " ";
      cur = p[cur];
    } while (cur != i && siz[i] < 100);
    cerr << endl;
  }
  cerr << "first loop end" << endl;
  for (ll i = 0; i < n; ++i) {
    ll kt = k;
    ll s = 0, tail = *(v[i].end() - 1);
    if (tail < 0) {
      for (ll j = 0; j < min(kt, siz[i]); ++j) {
        r = max(r, v[i][j]);
      }
    } else {
      s = (ll)tail * (kt / siz[i]);
      kt %= v[i].size();
      r = max(r, s);
      for (ll j = 0; j < min(kt, siz[i]); ++j) {
        r = max(r, s + v[i][j]);
      }
    }
  }
  cout << r;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  int N;
  ll K;
  cin >> N >> K;
  int P[N];
  ll C[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  ll ans = -100000000000000;

  for (int i = 0; i < N; i++) {
    vector<ll> v;
    int p = P[i];
    ll c_sum = C[p];
    v.push_back(C[p]);
    ll cnt = 1;
    ans = max(ans, c_sum);
    while (true) {
      p = P[p];
      if (P[i] == p)
        break;
      cnt++;
      if (cnt > K)
        break;
      c_sum += C[p];
      v.push_back(C[p]);
      ans = max(ans, c_sum);
    }
    // cout << c_sum << "\\n";
    ll sum = 0;
    p = P[i];
    if (cnt > K)
      continue;
    ans = max(ans, c_sum * (K / cnt));
    for (int i = 0; i < cnt; i++) {
      sum += C[p];
      p = P[p];
      ans = max(ans, sum + c_sum * ((K - i - 1) / cnt));
    }
  }
  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  ll K;
  cin >> N >> K;
  int P[N];
  ll C[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  ll ans = -100000000000000;

  for (int i = 0; i < N; i++) {
    int p = P[i];
    ll c_sum = C[p];
    ll cnt = 1;
    ans = max(ans, c_sum);
    while (p != i) {
      p = P[p];
      cnt++;
      if (cnt > K)
        break;
      c_sum += C[p];
      ans = max(ans, c_sum);
    }
    if (cnt > K)
      continue;
    if (cnt < K)
      while (1) {
      }

    ll sum = 0;
    p = P[i];
    ans = max(ans, c_sum * (K / cnt));
    for (int j = 0; j < K % cnt; j++) {
      sum += C[p];
      p = P[p];
      ans = max(ans, sum + c_sum * (K / cnt));
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  long k;
  cin >> n >> k;

  vector<int> p(n);
  vector<long> c(n);

  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }

  long long ans = -10000000000;
  for (int i = 0; i < n; i++) {
    int now = p[i] - 1;
    long long l_s = 0;
    long long tmp = 0;
    vector<int> t;
    t.push_back(c[now]);
    l_s += c[now];
    while (now != i) {
      // cout << l_s << " ";
      now = p[now] - 1;
      t.push_back(c[now]);
      l_s += c[now];
    }

    // cout << l_s << endl;

    for (int j = 0; j < t.size(); j++) {
      tmp += t[j];
      if (j + 1 > k)
        break;
      long long hoge = tmp;
      if (l_s > 0) {
        long long e = (k - (j + 1)) / t.size();
        hoge += l_s * e;
      }
      ans = max(ans, hoge);
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  long k;
  cin >> n >> k;

  vector<int> p(n);
  vector<long> c(n);

  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }

  long long ans = -1000000000;
  long long tmp = 0;
  int now;
  for (int i = 0; i < n; i++) {
    now = p[i] - 1;
    tmp = 0;
    for (int j = 0; j < k; j++) {
      tmp += c[now];
      // cout << tmp << endl;
      now = p[now] - 1;
      if (tmp > ans)
        ans = tmp;
    }
  }

  cout << ans << endl;
  return 0;
}