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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll g[4001][4001];
int r, c, k;
ll dp[4001][4001][4];
ll rec(ll i, ll j, ll taken) {
  if (i >= r)
    return 0;
  if (j >= c)
    return 0;
  if (dp[i][j][taken] != -1)
    return dp[i][j][taken];
  ll ans = 0;
  if (taken < 3 && i < r && j < c) {
    ans = g[i][j];
  } else {
    ans = 0;
  }

  ans = rec(i + 1, j, 0);
  if (taken < 3) {
    ans = max(ans, rec(i + 1, j, 0) + g[i][j]);
  }
  ans = max(ans, rec(i, j + 1, taken));
  if (taken < 3) {
    ans = max(ans, rec(i, j + 1, taken + 1) + g[i][j]);
  }

  return dp[i][j][taken] = ans;
}
int main() {
  cin >> r >> c >> k;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      g[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    g[a][b] = v;
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0, 0);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll g[2001][2001];
int r, c, k;
ll dp[2001][2001][4];
ll rec(ll i, ll j, ll taken) {
  if (i >= r)
    return 0;
  if (j >= c)
    return 0;
  if (dp[i][j][taken] != -1)
    return dp[i][j][taken];
  ll ans = 0;
  if (taken < 3 && i < r && j < c) {
    ans = g[i][j];
  } else {
    ans = 0;
  }

  ans = rec(i + 1, j, 0);
  if (taken < 3) {
    ans = max(ans, rec(i + 1, j, 0) + g[i][j]);
  }
  ans = max(ans, rec(i, j + 1, taken));
  if (taken < 3) {
    ans = max(ans, rec(i, j + 1, taken + 1) + g[i][j]);
  }

  return dp[i][j][taken] = ans;
}
int main() {
  cin >> r >> c >> k;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      g[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    g[a][b] = v;
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0, 0);
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int R, C, K;
vector<vector<ll>> item;
vector<vector<vector<ll>>> dp;

int main() {
  cin >> R >> C >> K;
  item.resize(R);
  dp.resize(R);
  for (size_t i = 0; i < R; i++) {
    item[i].resize(C);
    dp[i].resize(C);
    for (size_t j = 0; j < C; j++) {
      item[i][j] = 0;
      dp[i][j].resize(4);
      for (size_t k = 0; k < 4; k++) {
        dp[i][j][k] = -1;
      }
    }
  }

  for (int i = 0; i < K; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    item[x - 1][y - 1] = v;
  }

  ll ans = 0;
  dp[0][0][0] = 0;

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 2; k >= 0; k--) {
        if (dp[i][j][k] >= 0) {
          chmax(dp[i][j][k + 1], dp[i][j][k] + item[i][j]);
        }
      }
      for (int k = 0; k < 4; k++) {
        if (dp[i][j][k] >= 0) {
          if (i + 1 < R) {
            chmax(dp[i + 1][j][0], dp[i][j][k]);
          }
          if (j + 1 < C) {
            chmax(dp[i][j + 1][k], dp[i][j][k]);
          }
        }
      }
    }
  }
  for (int k = 0; k < 4; k++) {
    chmax(ans, dp[R - 1][C - 1][k]);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

int R, C, K;
vector<vector<ll>> item;

ll ref(int x, int y, int cnt,
       ll sum) { // x, y: x, y座標　　cnt: その行で拾ったアイテムの数　　sum:
                 // 合計得点
  if (x > R - 1 || y > C - 1)
    return sum; // 範囲外に行ったら終了
  if (x == R - 1 && y == C - 1)
    return sum + item[x][y]; // ゴールに行ったら終了

  // アイテムを拾わず進む
  ll ref1 = ref(x + 1, y, 0, sum); // 行が変わるからカウントリセット
  ll ref2 = ref(x, y + 1, cnt, sum);
  // アイテムを拾って進む
  ll ref3 =
      (cnt < 3 && item[x][y] != 0) ? ref(x + 1, y, 0, sum + item[x][y]) : 0;
  ll ref4 = (cnt < 3 && item[x][y] != 0)
                ? ref(x, y + 1, cnt + 1, sum + item[x][y])
                : 0;

  return max({ref1, ref2, ref3, ref4});
}

int main() {
  cin >> R >> C >> K;
  item.resize(R);
  for (size_t i = 0; i < R; i++) {
    item[i].resize(C);
    for (size_t j = 0; j < C; j++) {
      item[i][j] = 0;
    }
  }

  for (int i = 0; i < K; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    item[x - 1][y - 1] = v;
  }

  cout << ref(0, 0, 0, 0) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*
2^3000
DP確定なんだが
どうすんだ
(r,c) = DP[c][r]

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2

0  0  0
0  3  0
0  1  0
0  4  0
0  2  20

同じ行で３個までだった。。。
rとc逆う
グローバル関数でかかないとスタックオーバーフローだ。
２００万こえてる。
*/
ll DP[3001][3001][4];

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

  ll R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> M(R + 1, vector<ll>(C + 1, 0));
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    M[r][c] = v;
  }
  for (ll r = 1; r <= R; r++) {
    for (ll c = 1; c <= C; c++) {
      rep(a, 4) // 上からもってくる場合
          DP[r][c][0] = max(DP[r][c][0], DP[r - 1][c][a]);
      if (M[r][c] > 0)
        DP[r][c][1] = DP[r][c][0] + M[r][c];
      rep(a, 4) // 左から
          DP[r][c][a] = max(DP[r][c][a], DP[r][c - 1][a]);
      if (M[r][c] > 0)
        rep(a, 3) {
          if (DP[r][c - 1][a] > 0)
            DP[r][c][a + 1] = max(DP[r][c][a + 1], DP[r][c - 1][a] + M[r][c]);
        }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, DP[R][C][i]);
  cout << ans << endl;
}
/*  rep(i, R+1){
    rep(j, C+1)
      cout<<DP[i][j][2];
    cout<<endl;
  }
}
*/

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*
2^3000
DP確定なんだが
どうすんだ
(r,c) = DP[c][r]

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2

0  0  0
0  3  0
0  1  0
0  4  0
0  2  20

同じ行で３個までだった。。。
rとc逆う

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

  ll R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> M(R + 1, vector<ll>(C + 1, 0));
  ll DP[3001][3001][4];
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    M[r][c] = v;
  }
  rep(i, 3001) rep(j, 3001) {
    DP[j][i][0] = 0;
    DP[j][i][1] = 0;
    DP[j][i][2] = 0;
    DP[j][i][3] = 0;
    DP[i][j][0] = 0;
    DP[i][j][1] = 0;
    DP[i][j][2] = 0;
    DP[i][j][3] = 0;
  }
  for (ll r = 1; r <= R; r++) {
    for (ll c = 1; c <= C; c++) {
      rep(a, 4) // 上からもってくる場合
          DP[r][c][0] = max(DP[r][c][0], DP[r - 1][c][a]);
      if (M[r][c] > 0)
        DP[r][c][1] = DP[r][c][0] + M[r][c];
      rep(a, 4) // 左から
          DP[r][c][a] = max(DP[r][c][a], DP[r][c - 1][a]);
      if (M[r][c] > 0)
        rep(a, 3) {
          if (DP[r][c - 1][a] > 0)
            DP[r][c][a + 1] = max(DP[r][c][a + 1], DP[r][c - 1][a] + M[r][c]);
        }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, DP[R][C][i]);
  cout << ans << endl;
}
/*  rep(i, R+1){
    rep(j, C+1)
      cout<<DP[i][j][2];
    cout<<endl;
  }
}
*/
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> mp;
  rep(i, k) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[make_pair(r, c)] = v;
  }

  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  dp[0][0][1] = mp[make_pair(0, 0)];

  rep(i, r) rep(j, c) {
    P p = make_pair(i, j);
    if (i == 0 && j == 0)
      continue;
    else if (i == 0) {
      rep(n, 3) {
        dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
      }
    } else if (j == 0) {
      dp[i][j][1] = dp[i - 1][j][1] + mp[p];
      dp[i][j][0] = dp[i - 1][j][1];
    } else {
      rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
      rep(n, 4) { dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]); }
      if (mp.find(p) != mp.end()) {
        rep(n, 4) { dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]); }
        rep(n, 3) {
          dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> mp;
  rep(i, k) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[make_pair(r, c)] = v;
  }

  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  dp[0][0][1] = mp[make_pair(0, 0)];

  rep(i, r) rep(j, c) {
    P p = make_pair(i, j);
    if (i == 0 && j == 0)
      continue;
    else if (i == 0) {
      rep(n, 3) {
        dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
      }
    } else if (j == 0) {
      dp[i][j][1] = dp[i - 1][j][1] + mp[p];
      dp[i][j][0] = dp[i - 1][j][1];
    } else {
      rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
      rep(n, 4) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]);
      }
      rep(n, 3) {
        dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 3e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
  cin >> r >> c >> k;
  while (k--) {
    cin >> x >> y >> z;
    g[x][y] = z;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
                          max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
      }
    }
  }
  cout << dp[r][c][3];
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
  cin >> r >> c >> k;
  while (k--) {
    cin >> x >> y >> z;
    g[x][y] = z;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
                          max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
      }
    }
  }
  cout << dp[r][c][3];
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long dp[3002][3002][4];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  long long grid[R + 3][C + 3];
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      grid[i][j] = 0;
      dp[i][j][0] = 0;
      dp[i][j][1] = -1e18;
      dp[i][j][2] = -1e18;
      dp[i][j][3] = -1e18;
    }
  }
  for (int i = 1; i <= K; i++) {
    int r, c;
    long long v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      if (grid[i][j] > 0) {
        dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + grid[i][j]);
      }
      if (grid[i][j] > 0) {
        for (int k = 3; k >= 1; k--) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + grid[i][j]);
        }
      }
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
    }
  }
  long long ans = max(dp[R][C][0], dp[R][C][1]);
  ans = max(ans, dp[R][C][2]);
  ans = max(ans, dp[R][C][3]);
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  long long grid[R + 3][C + 3], dp[R + 3][C + 3][5];
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      grid[i][j] = 0;
      dp[i][j][0] = 0;
      dp[i][j][1] = -1e18;
      dp[i][j][2] = -1e18;
      dp[i][j][3] = -1e18;
    }
  }
  for (int i = 1; i <= K; i++) {
    int r, c;
    long long v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      if (grid[i][j] > 0) {
        dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + grid[i][j]);
      }
      if (grid[i][j] > 0) {
        for (int k = 3; k >= 1; k--) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + grid[i][j]);
        }
      }
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
    }
  }
  long long ans = max(dp[R][C][0], dp[R][C][1]);
  ans = max(ans, dp[R][C][2]);
  ans = max(ans, dp[R][C][3]);
  cout << ans;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

long long getMax(long long x, long long y) {
  if (x >= y)
    return x;

  return y;
}

long long getMax2(long long *arr) {
  long long val = 0ll;
  for (int i = 0; i < 4; i++)
    val = getMax(val, arr[i]);

  return val;
}

int main() {
  // your code goes here
  int r, c, k, x, y;
  cin >> r >> c >> k;
  long long dp[r + 1][c + 1][4];
  long long prev_dp[c + 1][4];
  long long new_dp[c + 1][4];
  int grid[r + 1][c + 1];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      grid[i][j] = 0;
    }
  }

  for (int i = 0; i <= c; i++) {
    for (int j = 0; j < 4; j++) {
      prev_dp[i][j] = 0ll;
      new_dp[i][j] = 0ll;
    }
  }

  int v;

  for (int i = 0; i < k; i++) {
    cin >> x >> y >> v;
    grid[x][y] = v;
  }

  for (int row = 1; row <= r; row++) {
    for (int col = 1; col <= c; col++) {
      new_dp[col][0] = getMax(new_dp[col - 1][0], getMax2(prev_dp[col]));
      if (grid[row][col]) {
        for (int taken = 0; taken < 3; taken++) {
          new_dp[col][taken + 1] =
              getMax(new_dp[col - 1][taken], getMax2(prev_dp[col])) +
              grid[row][col];
        }
      }
      for (int taken = 0; taken < 4; taken++) {
        new_dp[col][taken] = getMax(new_dp[col][taken], new_dp[col - 1][taken]);
      }
    }

    for (int i = 0; i <= c; i++) {
      for (int j = 0; j < 4; j++) {
        prev_dp[i][j] = new_dp[i][j];
        new_dp[i][j] = 0ll;
      }
    }
  }

  cout << getMax2(prev_dp[c]) << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

long long getMax(long long x, long long y) {
  if (x >= y)
    return x;

  return y;
}

long long getMax2(long long *arr) {
  long long val = 0ll;
  for (int i = 0; i < 4; i++)
    val = getMax(val, arr[i]);

  return val;
}

int main() {
  // your code goes here
  int r, c, k, x, y;
  cin >> r >> c >> k;
  long long dp[r + 1][c + 1][4];
  int grid[r + 1][c + 1];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      grid[i][j] = 0;
      for (int t = 0; t <= 3; t++)
        dp[i][j][t] = 0ll;
    }
  }

  int v;

  for (int i = 0; i < k; i++) {
    cin >> x >> y >> v;
    grid[x][y] = v;
  }

  for (int row = 1; row <= r; row++) {
    for (int col = 1; col <= c; col++) {
      dp[row][col][0] = getMax(dp[row][col - 1][0], getMax2(dp[row - 1][col]));
      if (grid[row][col]) {
        for (int taken = 0; taken < 3; taken++) {
          dp[row][col][taken + 1] =
              getMax(dp[row][col - 1][taken], getMax2(dp[row - 1][col])) +
              grid[row][col];
        }
      }
      for (int taken = 0; taken < 4; taken++) {
        dp[row][col][taken] =
            getMax(dp[row][col][taken], dp[row][col - 1][taken]);
      }
    }
  }

  cout << getMax2(dp[r][c]) << '\\n';
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long

using namespace std;
vector<vector<int>> a;
int f[3002][3002][4];
int r, c, k;

int calc(int i, int j, int cur) {
  if (cur == 3) {
    if (i + 1 < r)
      return calc(i + 1, j - 1, 0);
    else
      return 0;
  }
  if (i == r - 1 && j == c - 1)
    return a[i][j];

  int &res = f[i][j][cur];
  if (res != -1)
    return res;
  res = 0;

  if (i + 1 < r)
    res = max(res, calc(i + 1, j, 0) + a[i][j]);
  if (j + 1 < c) {
    res = max(res, calc(i, j + 1, cur));
    res = max(res, calc(i, j + 1, cur + 1) + a[i][j]);
  }
  return res;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> r >> c >> k;
  a.resize(r + 1, vector<int>(c + 1, 0));
  memset(f, -1, sizeof(f));
  while (k--) {
    int i, j;
    cin >> i >> j;
    --i;
    --j;
    cin >> a[i][j];
  }
  cout << calc(0, 0, 0);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int, int> ii;

int n, m, k, grid[3001][3001];

ii getMin(vector<int> &tmp) {
  int mn = 1e9;
  ii ret;
  for (int i = 0; i < 3; i++) {
    if (tmp[i] < mn) {
      mn = tmp[i];
      ret.first = tmp[i];
      ret.second = i;
    }
  }
  return ret;
}

int cal(int r, int c, vector<int> &save, int prev) {
  if (r < 1 && c == 1)
    return 0;
  if (r == 1 && c < 1)
    return 0;
  if (r < 1 || c < 1)
    return -1e18;
  int ans = 0;
  vector<int> d;
  if (!grid[r][c])
    ans = max(cal(r - 1, c, d, 0), cal(r, c - 1, save, prev));
  else if ((int)save.size() < 3) {
    save.push_back(grid[r][c]);
    ans = max(cal(r - 1, c, d, 0), cal(r, c - 1, save, prev)) + grid[r][c];
  } else if ((int)save.size() == 3) {
    ii mn = getMin(save);
    if (mn.first < grid[r][c]) {
      save[mn.second] = grid[r][c];
      ans = max(cal(r - 1, c, d, 0), cal(r, c - 1, save, getMin(save).first)) +
            grid[r][c] - mn.first;
    } else
      ans = max(cal(r - 1, c, d, 0), cal(r, c - 1, save, prev));
  }
  return ans;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //    freopen("INP.INP", "r", stdin);
  //    freopen("OUT.OUT", "w", stdout);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    int l, r, v;
    cin >> l >> r >> v;
    grid[l][r] = v;
  }
  vector<int> d;
  cout << cal(n, m, d, 0);

  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> arr(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  rep(i, k) {
    cin >> t1 >> t2 >> t3;
    t1--, t2--;
    arr[t1][t2] = t3;
  }
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
        chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
        if (p < 3) {
          chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
        }
        chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
      }
    }
  }
  ll ans = 0;
  rep(p, 4) { chmax(ans, dp[r][c][p]); }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> arr(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  rep(i, k) {
    cin >> t1 >> t2 >> t3;
    t1--, t2--;
    arr[t1][t2] = t3;
  }
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
        chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
        if (p < 3) {
          chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
        }
        chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
      }
    }
  }
  ll ans = 0;
  rep(p, 4) { chmax(ans, dp[r][c][p]); }
  rep(i, 4) { cout << dp[1][4][i] << endl; }
  cout << ans << endl;
}