alexjercan/bugnet huggingface dataset:
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

int n, k;
int p[5010], c[5010];
int siz[5010];
long long sum[5010];
long long dp[5001][10000];
int st[5010], it, mar[5010];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  if (k < n << 1) {
    long long ans = -1000000000000000;
    for (int j = 0; j < k; ++j) {
      for (int i = 1; i <= n; ++i)
        dp[i][j + 1] = -1000000000000000;
      for (int i = 1; i <= n; ++i) {
        dp[p[i]][j + 1] = max(dp[p[i]][j + 1], max(0ll, dp[i][j]) + c[p[i]]);
        ans = max(ans, dp[p[i]][j + 1]);
      }
    }
    printf("%lld\\n", ans);
    return 0;
  }
  for (int j = 0; j < n << 1; ++j) {
    for (int i = 1; i <= n; ++i)
      dp[i][j + 1] = -1000000000000000;
    for (int i = 1; i <= n; ++i)
      dp[p[i]][j + 1] = max(dp[p[i]][j + 1], max(0ll, dp[i][j]) + c[p[i]]);
  }

  long long ans = -1000000000000000;
  for (int i = 1; i <= n; ++i)
    if (!mar[i]) {
      it = 0;
      long long su = 0;
      for (int j = i; !mar[j]; j = p[j])
        mar[j] = i, su += c[j], st[it++] = j;
      int si = it;
      while (it--)
        siz[st[it]] = si, sum[st[it]] = su;
    }
  for (int i = 1; i <= n; ++i)
    if (sum[i] > 0)
      ans = max(ans, dp[i][k % siz[i] + siz[i]] + (k / siz[i] - 1) * sum[i]);
    else
      ans = max(ans, dp[i][min(k, siz[i])]);
  printf("%lld\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

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

int n, k;
int p[5010], c[5010];
int siz[501];
long long sum[5010];
long long dp[5010][5010];
int st[5010], it, mar[5010];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int j = 0; j < n; ++j) {
    for (int i = 1; i <= n; ++i)
      dp[i][j + 1] = INT_MIN;
    for (int i = 1; i <= n; ++i)
      dp[p[i]][j + 1] = max(dp[p[i]][j + 1], max(0ll, dp[i][j]) + c[p[i]]);
  }

  long long ans = -1000000000000000;
  for (int i = 1; i <= n; ++i)
    if (!mar[i]) {
      int j;
      for (j = i; !mar[j]; j = p[j]) {
        mar[j] = i, siz[j] = it;
        if (!mar[p[j]])
          sum[p[j]] = sum[j] + c[p[j]];
        st[it++] = j;
      }
      if (mar[j] == i) {
        int t1 = it - siz[j];
        long long t2 = sum[st[it - 1]] - sum[j] + c[j];
        while (st[it--] != j)
          siz[st[it]] = t1, sum[st[it]] = t2;
      }
      while (it >= 0)
        siz[st[it]] = 0, sum[st[it--]] = 0;
    }
  if (n >= k)
    for (int i = 1; i <= n; ++i)
      ans = max(ans, dp[i][k]);
  else
    for (int i = 1; i <= n; ++i)
      if (sum[i] > 0)
        ans = max(ans, dp[i][k - ((k - n - 1) / siz[i] + 1) * siz[i]] +
                           ((k - n - 1) / siz[i] + 1) * sum[i]);
      else
        ans = max(ans, dp[i][n]);
  printf("%lld\\n", ans);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N), C(N);
  bool ollzero = true;
  REP(i, N) cin >> P[i], P[i]--;
  REP(i, N) { cin >> C[i]; }
  ll ans = -1e17;
  REP(i, N) {
    vector<bool> Kita(N, false);
    ll tmpK = K, num = 0;
    ll now = i, loopcnt = 0;
    bool cat = false;
    while (tmpK > 0) {

      now = P[now];
      if (Kita[now] == true && !cat) {
        cat = true;
        ll a = (tmpK - 1) / loopcnt;
        ll tmp = num * a;
        if (tmp < 0)
          break;
        num += tmp;
        tmpK -= a * loopcnt;
        ans = max(ans, num);
      }
      num += C[now];
      Kita[now] = true;
      ans = max(ans, num);
      loopcnt++;
      tmpK--;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N), C(N);
  bool ollzero = true;
  REP(i, N) cin >> P[i], P[i]--;
  REP(i, N) { cin >> C[i]; }
  ll ans = -1e17;
  REP(i, N) {
    vector<bool> Kita(N, false);
    ll tmpK = K, num = 0;
    ll now = i, loopcnt = 0;
    while (tmpK > 0) {
      if (Kita[i] == true) {
        ll a = K / loopcnt;
        ll tmp = num * a;
        if (tmp < 0)
          break;
        num += tmp;
        tmpK -= a;
        ans = max(ans, num);
      }
      now = P[now];
      num += C[now];
      ans = max(ans, num);
      loopcnt++;
      tmpK--;
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long long INF = 1e18;

int main() {
  int n;
  long long k;
  cin >> n >> k;
  int p[n + 1];
  long long c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }

  long long ans = -INF;

  for (int i = 1; i <= n; i++) {
    // iスタートにおける1ループ距離遷移数と獲得点
    long long loop_sum = 0;
    int loop_num = 0;
    int pos = i;
    while (true) {
      loop_num++;
      loop_sum += c[pos];
      pos = p[pos];
      if (pos == i) {
        break;
      }
    }

    // iスタートにおける1ループできない場合の最大獲得点
    long long sum = 0;
    int num = 0;
    pos = i;
    while (true) {
      num++;
      sum += c[pos];
      if (num > k) {
        break;
      }

      long long can_loop = (k - num) / loop_num;
      long long point = sum + max(0ll, loop_sum) * can_loop;
      ans = max(ans, point);

      pos = p[pos];
      if (pos == i) {
        break;
      }
    }
  }

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


Error: TLE

Fixed submission:

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  int p[n + 1];
  long long c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }

  int loop = n;
  int now = 1;
  long long loop_sum = 0;
  while (loop-- > 0) {
    now = p[now];
    loop_sum += c[now];
  }

  vector<vector<long long>> dp(n + 1, vector<long long>(n));
  for (int i = 1; i <= n; i++) {
    int loop = n;
    int now = i;
    long long sum = 0;
    dp[i][0] = 0;
    for (int j = 1; j < n; j++) {
      now = p[now];
      sum += c[now];
      dp[i][j] = max(dp[i][j - 1], sum);
    }
  }

  long long ans = 0;
  if (loop_sum > 0) {
    long long use_loop = k / n;
    ans = loop_sum * use_loop;
    k -= use_loop * n;
  } else {
    k = (k > n - 1) ? n - 1 : k;
  }

  long long max_start = dp[1][k];
  for (int i = 2; 1 <= n; i++) {
    max_start = max(max_start, dp[i][k]);
  }
  ans += max_start;

  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;

vector<ll> a(5005);
vector<ll> b(5005);
vector<ll> track;
bool vis[5005];

void dfs(int u, long long total_cycle) {
  if (vis[u])
    return;
  vis[u] = true;
  total_cycle += b[u];
  track.push_back(total_cycle);
  dfs(a[u], total_cycle);
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ll ans = -2e18;
  for (int i = 0; i < n; i++) {
    memset(vis, false, sizeof(vis));
    dfs(i, 0);

    ll m = (ll)track.size();
    if (track.back() > 0 && k >= m) {
      ll cycles = k / m;
      ll left = k % m;
      ll maxi = -2e18;
      for (int x = 0; x < (int)track.size(); x++)
        maxi = max(maxi, track[x]);
      ans = max(ans, (cycles - 1) * (track.back()) + maxi);
      maxi = -2e18;
      for (int x = 0; x < left; x++)
        maxi = max(maxi, track[x]);
      ans = max(ans, (cycles * track.back()) + maxi);
    } else {
      for (int j = 0; j < (min((int)track.size(), k)); j++) {
        ans = max(ans, track[j]);
      }
    }
    track.clear();
  }
  cout << ans;
  return;
}

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

  solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;

vector<ll> a(5005);
vector<ll> b(5005);
vector<ll> track;
bool vis[5005];

void dfs(int u, long long total_cycle) {
  if (vis[u])
    return;
  vis[u] = true;
  total_cycle += b[u];
  track.push_back(total_cycle);
  dfs(a[u], total_cycle);
}
void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ll ans = -(1e18);
  for (int i = 0; i < n; i++) {
    memset(vis, false, sizeof(vis));
    dfs(i, 0);

    ll m = (ll)track.size();
    if (track.back() > 0 && k >= m) {
      ll cycles = (ll)(k / m);
      ll left = (ll)(k % m);

      ll maxi = *max_element(track.begin(), track.end());
      ans = max(ans, (cycles - 1LL) * (track.back()) + maxi);

      maxi = *max_element(track.begin(), track.begin() + left - 1);
      ans = max(ans, (cycles * track.back()) + maxi);
    } else {
      ans =
          max(ans, *max_element(track.begin(), track.begin() + min(k, m) - 1));
    }
    track.clear();
  }
  cout << ans;
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }

vector<ll> a(5005);
vector<ll> b(5005);
vector<ll> track;
bool vis[5005];

void dfs(int u, long long total_cycle) {
  if (vis[u])
    return;
  vis[u] = true;
  total_cycle += b[u];
  track.push_back(total_cycle);
  dfs(a[u], total_cycle);
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ll ans = -(1e18);
  for (int i = 0; i < n; i++) {
    memset(vis, false, sizeof(vis));
    dfs(i, 0);
    if (track.back() > 0 && k >= (int)(track.size())) {
      ll cycles = (ll)(k / (int)track.size());
      ll left = (ll)(k % (int)track.size());
      ll maxi = -1e18;
      for (int x = 0; x < (int)track.size(); x++)
        maxi = max(maxi, track[x]);
      ans = max(ans, (cycles - 1LL) * (track.back()) + maxi);
      maxi = -1e18;
      for (int x = 0; x < left; x++)
        maxi = max(maxi, track[x]);
      ans = max(ans, (cycles * track.back()) + maxi);
    } else {
      for (int j = 0; j < (min((int)track.size(), k)); j++) {
        ans = max(ans, track[j]);
      }
    }
    track.clear();
  }
  cout << ans;
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  //        freopen("timber_input.txt", "r", stdin);
  //        freopen("timber_output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    //            cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define bug(x) cerr << #x << " = " << x << '\\n'
#define ll long long
#define maxn
#define inf 100000000000005

int main() {
  // freopen(".inp","r",stdin);

  int n, k;
  cin >> n >> k;

  vector<int> p(n + 1), c(n + 1);

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

  ll ret = -inf;
  for (int i = 1; i <= n; ++i) {
    int st = i;
    ll o = 0;
    for (int j = 1; j <= k; ++j) {
      o += (ll)c[p[st]];
      st = p[st];
      ret = max(ret, o);
    }
  }
  cout << ret;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1e18;
// const ll mod = 1000000007;
ll N, K;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  vector<ll> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<ll> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];
  ll ans = -INF;
  for (auto tmp : C)
    chmax(ans, tmp);
  if (ans < 0) {
    cout << ans << endl;
    return 0;
  }
  for (int s = 0; s < N; s++) {
    vector<ll> a;
    ll sum = 0;
    ll now = s;
    while (true) {
      sum += C[now];
      a.push_back(C[now]);
      now = P[now];
      if (now == s)
        break;
    }
    ll ret = 0;
    if (sum > 0) {
      ret = ((K - 1) / a.size()) * sum;
    }
    ll nowK = K % a.size();
    if (nowK == 0)
      nowK = a.size();
    if (sum <= 0) {
      nowK = min(K, (ll)a.size());
    }
    ll rest = 0;
    ll nowrest = 0;
    for (int i = 0; i < nowK; i++) {
      nowrest += a[i];
      chmax(rest, nowrest);
    }
    ret += rest;
    /*
    cerr << s << " " << nowK << " " << a.size() << " " << ret << endl;
    for(auto tmp : a) cerr << tmp << " ";
    cerr << endl;
    */
    chmax(ans, ret);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1e18;
// const ll mod = 1000000007;
ll N, K;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  vector<ll> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<ll> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];
  ll ans = -INF;
  for (auto tmp : C)
    chmax(ans, tmp);
  if (ans < 0) {
    cout << ans << endl;
    return 0;
  }
  for (int s = 0; s < N; s++) {
    vector<ll> a;
    ll sum = 0;
    ll now = s;
    while (true) {
      sum += C[now];
      a.push_back(C[now]);
      now = P[now];
      if (now == s)
        break;
    }
    ll ret = 0;
    if (sum > 0) {
      ret = (K / a.size()) * sum;
    }
    ll nowK = K % a.size();
    if (nowK == 0)
      nowK = K;
    ll rest = 0;
    ll nowrest = 0;
    for (int i = 0; i < nowK; i++) {
      nowrest += a[i];
      chmax(rest, nowrest);
    }
    ret += rest;
    /*
    cerr << s << " " << nowK << " " << a.size() << " " << ret << endl;
    for(auto tmp : a) cerr << tmp << " ";
    cerr << endl;
    */
    chmax(ans, ret);
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
vector<int> P, C;
map<int, vector<pair<ll, int>>> memo;
vector<pair<ll, int>> f(int k) {
  if (memo.count(k))
    return memo[k];
  if (k == 1) {
    vector<pair<ll, int>> r(N);
    for (int i = 0; i < N; ++i) {
      r[i] = {C[P[i]], P[i]};
    }
    return memo[k] = r;
  } else {
    int x = k / 2;
    if (N < K - N && k > K - N) {
      x = K - N;
    }
    int y = k - x;
    auto a = f(x);
    auto b = f(y);
    vector<pair<ll, int>> r(N);
    for (int i = 0; i < N; ++i) {
      auto x = a[i];
      auto y = b[x.second];
      r[i] = {x.first + y.first, y.second};
    }
    return memo[k] = r;
  }
}
int main() {
  cin >> N >> K;
  P.resize(N);
  C.resize(N);
  for (auto &x : P) {
    cin >> x;
    --x;
  }
  for (auto &x : C)
    cin >> x;
  auto res = -(1LL << 62);
  for (int kk = 1; kk <= K; ++kk) {
    if (kk == N + 1 && kk < K - N)
      kk = K - N;
    for (auto x : f(kk)) {
      res = max(res, x.first);
    }
  }
  cout << res << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
vector<int> P, C;
map<int, vector<pair<ll, int>>> memo;
vector<pair<ll, int>> f(int k) {
  if (memo.count(k))
    return memo[k];
  if (k == 1) {
    vector<pair<ll, int>> r(N);
    for (int i = 0; i < N; ++i) {
      r[i] = {C[P[i]], P[i]};
    }
    return memo[k] = r;
  } else {
    auto a = f(k / 2);
    auto b = f(k - k / 2);
    vector<pair<ll, int>> r(N);
    for (int i = 0; i < N; ++i) {
      auto x = a[i];
      auto y = b[x.second];
      r[i] = {x.first + y.first, y.second};
    }
    return memo[k] = r;
  }
}
int main() {
  cin >> N >> K;
  P.resize(N);
  C.resize(N);
  for (auto &x : P) {
    cin >> x;
    --x;
  }
  for (auto &x : C)
    cin >> x;
  auto res = -(1LL << 62);
  for (int kk = 1; kk <= K; ++kk) {
    if (kk == N + 1 && kk < K - N)
      kk = K - N;
    for (auto x : f(kk)) {
      res = max(res, x.first);
    }
  }
  cout << res << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

#define FOR(a, b) for (int a = 0; a < b; a++)
#define F0R(a, b, c) for (int a = b; a <= c; a++)
#define f first
#define s second
#define m0(x) memset(x, 0, sizeof(x))

typedef pair<int, int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vi perm(n + 1);
  F0R(i, 1, n) cin >> perm[i];
  vector<ll> cost(n + 1);
  F0R(i, 1, n) cin >> cost[i];
  ll ans = cost[1];
  F0R(i, 1, n) {
    int sz = 0;
    int cur = i;
    vector<ll> pre, best;
    pre.push_back(0);
    best.push_back(0);
    while (1) {
      sz++;
      pre.push_back(pre[sz - 1] + cost[cur]);
      if (sz == 1)
        best.push_back(pre[sz]);
      else
        best.push_back(max(best[sz - 1], pre[sz]));
      cur = perm[cur];
      if (cur == i)
        break;
    }
    ll nk = k % sz;
    if (pre[sz] > 0 && k >= sz) {
      ll times = k / sz;
      ans = max(ans, times * pre[sz] + max(0LL, best[nk]));
      ans = max(ans, (times - 1LL) * pre[sz] + best[sz]);
    } else {
      if (k >= sz)
        ans = max(ans, best[sz]);
      else
        ans = max(ans, best[k]);
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

#define FOR(a, b) for (int a = 0; a < b; a++)
#define F0R(a, b, c) for (int a = b; a <= c; a++)
#define f first
#define s second
#define m0(x) memset(x, 0, sizeof(x))

typedef pair<int, int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vi perm(n + 1);
  F0R(i, 1, n) cin >> perm[i];
  vector<ll> cost(n + 1);
  F0R(i, 1, n) cin >> cost[i];
  ll ans = cost[1];
  F0R(i, 1, n) {
    int sz = 0;
    int cur = i;
    vector<ll> pre, best;
    pre.push_back(0);
    best.push_back(0);
    while (1) {
      sz++;
      pre.push_back(pre[sz - 1] + cost[cur]);
      if (sz == 1)
        best.push_back(pre[sz]);
      else
        best.push_back(max(best[sz - 1], pre[sz]));
      cur = perm[cur];
      if (cur == i)
        break;
    }
    ll nk = k % sz;
    if (pre[sz] > 0 && k >= sz) {
      ll times = k / sz;
      ans = max(ans, times * pre[sz] + max(0LL, best[nk]));
      ans = max(ans, (times - 1LL) * pre[sz] + best[sz]);
    } else {
      if (nk == 0)
        ans = max(ans, best[k]);
      else
        ans = max(ans, best[nk]);
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

int main(int argc, const char *argv[]) {
  int N;
  long K;
  cin >> N;
  cin >> K;
  int *P = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> P[i];
    --P[i];
  }
  long *C = new long[N];
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
  }

  long max = INT64_MIN;
  for (int i = 0; i < N; ++i) { // 開始位置
    // サイクル計算
    int idx = i;
    int length = 1;
    long cycleC = 0;
    for (int j = 0; j < N; ++j) {
      idx = P[idx];
      cycleC += C[idx];
      if (idx == i) {
        break;
      }
      ++length;
    }
    if (cycleC <= 0) {
      idx = i;
      long score = C[idx];
      if (max < score) {
        max = score;
      }
      for (int j = 1, end = min(length, (int)K); j < end; ++j) {
        idx = P[idx];
        score += C[idx];
        if (max < score) {
          max = score;
        }
      }
    } else {
      int mod = K % length;
      if (mod == 0) {
        mod = length;
      }
      long cycleNum = (K - (long)mod) / length;
      long score = cycleNum * cycleC;
      if (max < score) {
        max = score;
      }
      for (int j = 0; j < mod; ++j) {
        idx = P[idx];
        score += C[idx];
        if (max < score) {
          max = score;
        }
      }
    }
  }
  cout << max << endl;

  delete[] P;
  delete[] C;
  return 0;
}


Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

int main(int argc, const char *argv[]) {
  int N;
  long K;
  cin >> N;
  cin >> K;
  int *P = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> P[i];
  }
  long *C = new long[N];
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
  }
  long max1 = C[P[0] - 1];
  for (int i = 0; i < N; ++i) { // 最初のマス
    long score = 0;
    int idx = i;
    for (long j = 0; j < K; ++j) { // マス移動
      idx = P[idx] - 1;
      score += C[idx];
      if (score > max1) {
        max1 = score;
      }
    }
  }
  cout << max1 << endl;
  delete[] P;
  delete[] C;
  return 0;
}