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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD (long long)(1e9 + 7)
#define _(i, a, n) for (int i = a; i < n; i++)
#define zero(a) a[0] = 0
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll p[5001], c[5001];
  _(i, 0, n)
  cin >> p[i];
  _(i, 0, n)
  cin >> c[i];
  ll ans = -MOD;
  _(i, 0, n) {
    ll s = c[i];
    vector<ll> t;
    t.push_back(s);

    for (int j = p[i]; j != i + 1; j = p[j - 1]) {
      // cout<<j<<" "<<i<<endl;
      s += c[j - 1];
      t.push_back(s);
    }
    int len = t.size();
    _(j, 0, len) {
      int res = k - j - 1;
      if (res < 0)
        break;
      ans = max(ans, t[j] + (res / len) * max(s, 0LL));
      // cout<<t[j]+(res/len)*max(s,0LL)<<endl;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  ll p[5001], c[5001];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  ll ans = c[0];
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      // cout<<j<<endl;
      int xh = j;
      int dt = i;
      ll cp = 0;
      while (dt > 0) {
        dt--;
        cp += c[p[xh] - 1];
        xh = p[xh] - 1;
        if (xh >= n)
          xh = 0;
        // cout<<cp<<endl;
      }
      ans = max(ans, cp);
      // cout<<endl;
    }
  }
  if (n < k) {
    int dt = n;
    ll jans = -1;
    for (int i = 0; i < n; i++) {
      dt = n;
      ll pd = 0;
      int xh = i;
      while (dt > 0) {
        dt--;
        pd += c[p[xh] - 1];
        xh = p[xh] - 1;
        if (xh >= n)
          xh = 0;
        // cout<<cp<<endl;
      }
      jans = max(jans, pd);
    }
  }
  cout << ans << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

int a[N], c[N];
bool vst[N];
vector<int> vec[N];

int main() {
  ios::sync_with_stdio(0);

  int n, lim;
  cin >> n >> lim;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    if (vst[i])
      continue;
    int o = i;
    while (!vst[o]) {
      vst[o] = 1;
      vec[i].push_back(o);
      o = a[o];
    }
  }

  ll ans = -INF_LL;
  for (int i = 1; i <= n; i++) {
    if (vec[i].empty())
      continue;
    int siz = vec[i].size();

    static ll s[N];
    s[0] = 0;
    for (int j = 1; j <= siz; j++)
      s[j] = s[j + siz] = c[vec[i][j - 1]];
    for (int j = 1; j <= 2 * siz; j++)
      s[j] += s[j - 1];

    ll sum = s[siz];
    for (int j = 1; j <= min(siz, lim); j++) {
      ll maxs = -INF_LL;
      for (int k = 1; k <= siz; k++)
        maxs = max(maxs, s[k + j - 1] - s[k - 1]);
      if (sum > 0)
        maxs += sum * ((lim - j) / siz);
      ans = max(ans, maxs);
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

int a[N], c[N];
bool vst[N];
vector<int> vec[N];

int main() {
  ios::sync_with_stdio(0);

  int n, lim;
  cin >> n >> lim;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    if (vst[i])
      continue;
    int o = i;
    while (!vst[o]) {
      vst[o] = 1;
      vec[i].push_back(o);
      o = a[o];
    }
  }

  ll ans = -INF_LL;
  for (int i = 1; i <= n; i++) {
    if (vec[i].empty())
      continue;
    int siz = vec[i].size();
    if (siz != 0 && siz <= n)
      while (1)
        ;

    static ll s[N];
    s[0] = 0;
    for (int j = 1; j <= siz; j++)
      s[j] = s[j + siz] = c[vec[i][j - 1]];
    for (int j = 1; j <= 2 * siz; j++)
      s[j] += s[j - 1];

    ll sum = s[siz];
    for (int j = 1; j <= min(siz, lim); j++) {
      ll maxs = -INF_LL;
      for (int k = 1; k <= siz; k++)
        maxs = max(maxs, s[k + j - 1] - s[k - 1]);
      if (sum > 0)
        maxs += sum * ((lim - j) / siz);
      ans = max(ans, maxs);
    }
  }
  cout << ans << endl;

  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 < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  ll ans = -INF;
  int cm;
  rep(i, n) {
    ll point = 0;
    cm = i;
    //        set<int> s;
    //        s.insert(cm);
    bool bcyc = false;
    ll mcyc = -INF;
    ll lcyc = 0;
    rep(j, k) {
      lcyc++;
      cm = p[cm];
      point += c[cm];
      mcyc = max(mcyc, point);
      //            if(s.count(cm)){
      if (i == cm) {
        bcyc = true;
        break;
      }
      //            s.insert(cm);
    }
    if (bcyc) {
      if (point <= 0)
        ans = max(ans, mcyc);
      else {
        point = (k / lcyc - 1) * point;
        cm = i;
        mcyc = -INF;
        rep(j, k % lcyc + lcyc) {
          cm = p[cm];
          point += c[cm];
          mcyc = max(mcyc, point);
        }
        ans = max(ans, mcyc);
      }
    } else {
      ans = max(ans, mcyc);
    }
    //        cout<<i<<" "<<point<<endl;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];
  ll ans = -INF;
  int cm;
  rep(i, n) {
    ll point = 0;
    cm = i;
    set<int> s;
    s.insert(cm);
    bool bcyc = false;
    ll mcyc = -INF;
    ll lcyc = 0;
    rep(j, k) {
      lcyc++;
      cm = p[cm];
      point += c[cm];
      mcyc = max(mcyc, point);
      if (s.count(cm)) {
        bcyc = true;
        break;
      }
      s.insert(cm);
    }
    if (bcyc) {
      if (point <= 0)
        ans = max(ans, mcyc);
      else {
        point = (k / lcyc - 1) * point;
        cm = i;
        mcyc = -INF;
        rep(j, k % lcyc + lcyc) {
          cm = p[cm];
          point += c[cm];
          mcyc = max(mcyc, point);
        }
        ans = max(ans, mcyc);
      }
    } else {
      ans = max(ans, mcyc);
    }
    //        cout<<i<<" "<<point<<endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define vii vector<int, int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define umii unordered_map<int, int>
#define mii map<int, int>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)

const long long MOD = 1e9 + 7;

void solve() {
  ll n, k;
  cin >> n >> k;
  int p[n], c[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<vector<long long>> cycle, pre;
  for (int i = 0; i < n; i++) {
    vector<long long> temp1;
    // temp.pb(p[i]);
    ll s = p[p[i] - 1];
    ll score = c[p[i] - 1];
    temp1.pb(score);
    while (p[i] != s) {
      // temp.pb(s);
      score += c[s - 1];
      temp1.pb(score);
      s = p[s - 1];
    }
    // cycle.pb(temp);
    pre.pb(temp1);
  }
  ll mx = INT_MIN;
  for (auto it : pre) {
    ll score = INT_MIN;
    if (it.size() >= k) {
      for (int i = 0; i < k; i++) {
        score = max(score, it[i]);
      }
    } else {
      if (*(it.rbegin()) < 0) {
        for (auto itr : it)
          score = max(score, itr);
      } else {
        ll q = k / it.size();
        ll r = k % it.size();
        ll t1 = *(it.rbegin()) * q;
        ll t2 = *(it.rbegin()) * (q - 1);
        ll m = INT_MIN;
        for (int i = 0; i < r; i++)
          m = max(m, it[i]);
        t1 += m;
        m = INT_MIN;
        for (int i = 0; i < it.size(); i++) {
          m = max(m, it[i]);
        }
        t2 += m;
        score = max(t1, t2);
      }
    }
    mx = max(mx, score);
  }
  cout << mx << endl;
}

signed main() {
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}

Error: TLE

Fixed submission:

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

#define ll long long
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define vii vector<int, int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define umii unordered_map<int, int>
#define mii map<int, int>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)

const long long MOD = 1e9 + 7;

void solve() {
  ll n, k;
  cin >> n >> k;
  int p[n], c[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  ll overall_max = INT_MIN;
  for (int i = 0; i < n; i++) {
    int s = i, j = k;
    ll sum = 0, ms = INT_MIN;
    while (j--) {
      sum += c[p[s] - 1];
      s = p[s] - 1;
      ms = max(sum, ms);
      // cout << i << " " << ms << endl;
    }
    overall_max = max(overall_max, ms);
  }
  cout << overall_max << endl;
}

signed main() {
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
Problem p02585 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  for (auto &p : P) {
    cin >> p;
    --p;
  }
  for (auto &c : C) {
    cin >> c;
  }

  vector<int> isAssigned(N);
  vector<vector<int>> orbits;
  for (int i = 0; i < N; ++i) {
    if (!isAssigned[i]) {
      vector<int> o;
      o.push_back(i);
      int j = i;
      while (P[j] != i) {
        o.push_back(P[j]);
        isAssigned[P[j]] = true;
        j = P[j];
      }
      orbits.push_back(o);
    }
  }

  vector<ll> results;
  for (const auto &o : orbits) {
    vector<ll> Co;
    transform(o.cbegin(), o.cend(), back_inserter(Co),
              [&C](int i) { return C[i]; });

    ll Csum = accumulate(Co.cbegin(), Co.cend(), (ll)0);
    ll loopNum = Csum > 0 ? max<ll>(K / o.size() - 1, 0) : 0;
    ll Ko = Csum > 0 ? K - loopNum * o.size() : min<ll>(K, o.size());

    vector<ll> dp(o.size());
    ll res = *max_element(Co.cbegin(), Co.cend());
    for (auto k = 0; k < Ko; ++k) {
      vector<ll> b;
      transform(Co.cbegin(), Co.cend(), dp.cbegin(), back_inserter(b),
                [](ll c, ll d) { return d + c; });
      copy(b.begin() + 1, b.end(), dp.begin());
      dp.back() = b[0];
      res = max(res, *max_element(dp.cbegin(), dp.cend()));
    }
    results.push_back(loopNum * Csum + res);
  }
  ll result = *max_element(results.cbegin(), results.cend());
  cout << result << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  for (auto &p : P) {
    cin >> p;
    --p;
  }
  for (auto &c : C) {
    cin >> c;
  }

  vector<int> isAssigned(N);
  vector<vector<int>> orbits;
  for (int i = 0; i < N; ++i) {
    if (!isAssigned[i]) {
      vector<int> o;
      o.push_back(i);
      int j = i;
      while (P[j] != i) {
        o.push_back(P[j]);
        isAssigned[P[j]] = true;
        j = P[j];
      }
      orbits.push_back(o);
    }
  }

  vector<ll> results;
  for (const auto &o : orbits) {
    vector<int> Co;
    transform(o.cbegin(), o.cend(), back_inserter(Co),
              [&C](int i) { return C[i]; });
    ll Csum = accumulate(Co.cbegin(), Co.cend(), 0);
    ll loopNum = Csum > 0 ? max<ll>(K / o.size() - 1, 0) : 0;
    ll Ko = K - loopNum * o.size();

    vector<vector<ll>> dp(Ko + 1, vector<ll>(N));
    for (auto k = 0; k < Ko; ++k) {
      for (auto i : o) {
        dp[k + 1][i] = dp[k][P[i]] + C[P[i]];
      }
    }
    vector<ll> maxVec(Ko + 1);
    for (auto k = 0; k <= Ko; ++k) {
      maxVec[k] = *max_element(dp[k].cbegin(), dp[k].cend());
    }
    ll maxRes = *max_element(maxVec.cbegin() + 1, maxVec.cend());
    results.push_back(loopNum * Csum + maxRes);
  }
  ll res = *max_element(results.cbegin(), results.cend());
  cout << res << endl;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MAX 5000
#define MOD 1000000007
using namespace std;
vector<int> p, c;
long long go(int start, int k) {
  long long sum = 0;
  int ii = start;
  int steps = 0;
  vector<int> v;
  do {
    ii = p[ii];
    v.push_back(c[ii]);
    sum += 1LL * c[ii];
    ++steps;
  } while (ii ^ start);
  long long tmp = 0, res = LLONG_MIN;
  for (int i = 0; i + 1 <= k && i < v.size(); ++i) {
    tmp += 1LL * v[i];
    if (sum > 0)
      res = max(res, tmp + (k - i - 1) / steps * 1LL * sum);
    else
      res = max(res, tmp);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  p = vector<int>(n + 1), c = vector<int>(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 = LLONG_MIN;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, go(i, k));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX 5000
#define MOD 1000000007
using namespace std;
int p[MAX + 1], c[MAX + 1];
long long go(int start, int k) {
  long long sum = c[p[start]], extra = sum;
  int ii = p[start];
  int steps = 1;
  while (ii ^ start) {
    sum += 1LL * c[p[ii]];
    ii = p[ii];
    ++steps;
    if (steps <= k)
      extra = max(extra, sum);
  }
  long long a = LLONG_MIN, b = LLONG_MIN;
  // cout<<start<<". steps:"<<steps<<" sum:"<<sum<<" extra:"<<extra<<endl;
  if (steps < k) {
    int resto = k % steps;
    a = sum * 1LL * (k / steps);
    long long tmp = c[p[start]];
    b = tmp;
    ii = p[start];
    while (--resto) {
      tmp += 1LL * c[p[ii]];
      ii = p[ii];
      b = max(b, tmp);
    }
    b += a;
  }
  return max({extra, a, b});
}
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  int n, k;
  // cin>>n>>k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; ++i)
    // cin>>p[i];
    scanf("%d", p + i);
  for (int i = 1; i <= n; ++i)
    // cin>>c[i];
    scanf("%d", c + i);
  long long ans = LLONG_MIN;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, go(i, k));
  // cout<<ans<<endl;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02585 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

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

  ll res = -(1ll << 60);
  rep(start, n) {
    ll score = 0;
    int now = start;
    int nxt;
    int endidx = -1;
    ll rangesm;
    rep(i, k) {
      nxt = p[now];
      if (nxt == start) {
        score += c[nxt];
        // scores.insert(score);
        res = max(res, score);
        rangesm = score;
        endidx = i;
        break;
      }

      score += c[nxt];
      // scores.insert(score);
      res = max(res, score);
      now = nxt;
    }

    if (endidx != -1) {
      ll sm = rangesm;
      ll rest = k - endidx - 1;
      ll mx = sm + sm * max(0ll, (rest / (endidx + 1)) - 1);
      ll mod = rest % (endidx + 1);
      // scores.insert(mx);
      res = max(res, mx);
      int nows = start;
      int nxts;
      int t = endidx + 1;
      if (rest < t)
        t = 0;
      rep(j, t + mod) {
        nxts = p[nows];
        mx += c[nxts];
        // scores.insert(mx);
        res = max(res, mx);
        nows = nxts;
      }
    }
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

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

  set<ll> scores;
  rep(start, n) {
    ll score = 0;
    int now = start;
    int nxt;
    int endidx = -1;
    ll rangesm;
    rep(i, k) {
      nxt = p[now];
      if (nxt == start) {
        score += c[nxt];
        scores.insert(score);
        rangesm = score;
        endidx = i;
        break;
      }

      score += c[nxt];
      scores.insert(score);
      now = nxt;
    }

    if (endidx != -1) {
      if (rangesm > 0) {
        ll sm = rangesm;
        ll rest = k - endidx - 1;
        ll mx = sm + sm * (rest / (endidx + 1));
        ll mod = rest % (endidx + 1);
        scores.insert(mx);
        int nows = start;
        int nxts;
        rep(j, mod) {
          nxts = p[nows];
          mx += c[nxts];
          scores.insert(mx);
          nows = nxts;
        }
      }
    }
  }

  // for(auto elem: scores) cout << elem << endl;

  cout << *scores.rbegin() << endl;

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

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const int mx = 3000 + 9;

int R, C, K;
ll g[mx][mx];
ll dp[mx][mx][4];

ll solve(int i, int j, int k) {
  if (i > R)
    return 0;
  if (j > C)
    return 0;
  ll &ans = dp[i][j][k];
  if (ans != -1)
    return ans;

  ans = 0;
  if (k > 0) {
    ans = max(ans, g[i][j] + solve(i, j + 1, k - 1));
    ans = max(ans, g[i][j] + solve(i + 1, j, 3));
  }

  ans = max(ans, solve(i, j + 1, k));
  ans = max(ans, solve(i + 1, j, 3));

  return ans;
}

int main() {
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int r, c;
    ll v;
    scanf("%d %d %lld", &r, &c, &v);
    g[r][c] = v;
  }

  memset(dp, -1, sizeof dp);
  ll ans = solve(1, 1, 3);
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const int mx = 3000 + 9;

int R, C, K;
ll g[mx][mx];
ll dp[mx][mx];

ll solve(int i, int j) {
  if (i > R)
    return 0;
  ll &ans = dp[i][j];
  if (ans != -1)
    return ans;

  ans = 0;
  multiset<ll> st = {0, 0, 0};

  ll sum = 0;
  for (; j <= C; j++) {
    if (g[i][j] > 0) {
      st.insert(g[i][j]);
      sum = 0;
      int cnt = 0;
      for (auto it = st.end(); cnt < 3; cnt++) {
        --it;
        sum += *it;
      }
    }

    ans = max(ans, sum + solve(i + 1, j));
  }

  return ans;
}

int main() {
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int r, c;
    ll v;
    scanf("%d %d %lld", &r, &c, &v);
    g[r][c] = v;
  }

  memset(dp, -1, sizeof dp);
  ll ans = solve(1, 1);
  printf("%lld\\n", ans);
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

#define fi first
#define se second
#define pb push_back
#define mp(a, b) make_pair((a), (b))
#define sz(a) (int)(a).size()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;

ll dp[3005][3005][4];
vvll ar(3005, vll(3005));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int r, c, k;
  cin >> r >> c >> k;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    ar[r][c] = v;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + ar[i][j]);
      }

      for (int k = 0; k <= 3; k++) {
        if (i + 1 <= r) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        }
        if (j + 1 <= c) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

#define fi first
#define se second
#define pb push_back
#define mp(a, b) make_pair((a), (b))
#define sz(a) (int)(a).size()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int r, c, k;
  cin >> r >> c >> k;
  ll dp[r + 5][c + 5][4];
  vvll ar(r + 1, vll(c + 1));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    ar[r][c] = v;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + ar[i][j]);
      }

      for (int k = 0; k <= 3; k++) {
        if (i + 1 <= r) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        }
        if (j + 1 <= c) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << '\\n';
  return 0;
}