Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

ll dp[3005][3005][4], E[3005][3005];

int main() {
  init();
  ll R, C, K;
  cin >> R >> C >> K;

  rep(i, K) {
    ll r, c, k;
    cin >> r >> c >> k;
    E[r][c] = k;
  }

  repe(i, 1, R + 1) {
    repe(j, 1, C + 1) {
      rep(k, 4) dp[i][j][k] = dp[i][j - 1][k];
      rep(k, 4) chmax(dp[i][j][0], dp[i - 1][j][k]);
      if (E[i][j] == 0)
        continue;

      rep(k, 3) chmax(dp[i][j][k + 1], dp[i][j - 1][k] + E[i][j]);
      rep(k, 4) chmax(dp[i][j][1], dp[i - 1][j][k] + E[i][j]);
    }
  }

  // repe(i, 1, R + 1) repe(j, 1, C + 1) {
  //     err(i, j);
  //     verr(dp[i][j], 4);
  // }

  cout << *max_element(dp[R][C], dp[R][C] + 4) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll N, K;
  cin >> N >> K;
  vl P(N), C(N);
  rep(i, N) cin >> P[i], P[i]--;
  rep(i, N) cin >> C[i];

  vc<ll> used(N, -1), Score, R;

  ll usei = 0;
  rep(i, N) {
    if (used[i] != -1)
      continue;
    R.eb(1);
    ll si = P[i];
    ll v = C[si];
    used[si] = usei;
    while (si != i) {
      si = P[si];
      v += C[si];
      used[si] = usei;
      R.back()++;
    }
    usei++;
    Score.eb(v);
  }

  ll sum = accumulate(all(C), 0ll);

  ll ans = -INF;

  rep(i, N) {
    ll id = used[i];
    ll sc = Score[id], num = R[id];
    ll k = K / num, r = K % num;
    ll tmp = (sc > 0 ? sc * k : 0);

    if (k > 0)
      chmax(ans, sc * k);

    ll si = i;
    rep(_, r) {
      si = P[si];
      tmp += C[si];
      chmax(ans, tmp);
    }

    if (r == 0) {
      ll g = max(0ll, K / num - 1) * sc;
      chmax(g, 0ll);
      if (K / num - 1 > 0)
        chmax(ans, (K / num - 1) * sc);
      ll ci = i;
      rep(_, num) {
        ci = P[ci];
        g += C[ci];
        chmax(ans, g);
      }
    }
  }

  cout << ans << endl;
  // verr(used, used.size());
  // verr(Score, Score.size());
  // verr(R, R.size());
}