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

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

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

  ll r, c, k;
  std::cin >> r >> c >> k;

  vector<vector<ll>> rcv(r, vector<ll>(c, 0));
  for (int i = 0; i < k; i++) {
    ll rr, cc, vv;
    std::cin >> rr >> cc >> vv;
    rr--;
    cc--;
    rcv[rr][cc] = vv;
  }

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

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i > 0)
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
      if (rcv[i][j] != 0) {
        for (int k = 1; k <= 3; k++) {
          auto &d = dp[i][j][k];
          if (j > 0)
            d = max(d, dp[i][j - 1][k - 1] + rcv[i][j]);
        }
        for (int k = 0; k <= 3; k++) {
          auto &d = dp[i][j][1];
          if (i > 0)
            d = max(d, dp[i - 1][j][k] + rcv[i][j]);
        }
      }
    }
  }

  std::cout << max({dp[r - 1][c - 1][0], dp[r - 1][c - 1][1],
                    dp[r - 1][c - 1][2], dp[r - 1][c - 1][3]})
            << std::endl;
}


Error: TLE

Fixed submission:

typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
  ll r, c, k;
  std::cin >> r >> c >> k;

  map<pair<ll, ll>, ll> rcv;
  for (int i = 0; i < k; i++) {
    ll rr, cc, vv;
    std::cin >> rr >> cc >> vv;
    rr--;
    cc--;
    rcv[{rr, cc}] = vv;
  }

  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  dp[0][0][1] = rcv[{0, 0}];

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i > 0)
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
      if (rcv[{i, j}] != 0) {
        for (int k = 1; k <= 3; k++) {
          auto &d = dp[i][j][k];
          if (j > 0)
            d = max(d, dp[i][j - 1][k - 1] + rcv[{i, j}]);
        }
        for (int k = 0; k <= 3; k++) {
          auto &d = dp[i][j][1];
          if (i > 0)
            d = max(d, dp[i - 1][j][k] + rcv[{i, j}]);
        }
      }
    }
  }

  std::cout << max({dp[r - 1][c - 1][0], dp[r - 1][c - 1][1],
                    dp[r - 1][c - 1][2], dp[r - 1][c - 1][3]})
            << std::endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

long long dp[3001][3001][4];
long long v[3001][3001];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int R, C, K;
  cin >> R >> C >> K;
  for (int i = 0; i < R + 1; i++)
    for (int j = 0; j < C + 1; j++)
      v[i][j] = 0;
  for (int i = 0; i < K; i++) {
    int r, c;
    cin >> r >> c;
    cin >> v[r][c];
  }
  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 1; i < R + 1; i++) {
    for (int j = 1; j < C + 1; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (v[i][j] > 0)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + v[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (k > 0 && v[i][j] > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int k = 0; k <= 3; k++)
    ans = max(ans, dp[R][C][k]);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int R, C, K;
  cin >> R >> C >> K;
  long long v[R + 1][C + 1];
  for (int i = 0; i < R + 1; i++)
    for (int j = 0; j < C + 1; j++)
      v[i][j] = 0;
  for (int i = 0; i < K; i++) {
    int r, c;
    cin >> r >> c;
    cin >> v[r][c];
  }
  long long dp[R + 1][C + 1][4];
  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 1; i < R + 1; i++) {
    for (int j = 1; j < C + 1; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (v[i][j] > 0)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + v[i][j]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (k > 0 && v[i][j] > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int k = 0; k <= 3; k++)
    ans = max(ans, dp[R][C][k]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

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;
}

long long v[3100][3100], dp[3100][3100][5];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < K; ++i) {
    long long r, c, t;
    cin >> r >> c >> t;
    v[r][c] = t;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= R; ++i) {
    for (int j = 0; j <= C; ++j) {
      for (int k = 0; k <= 3; ++k) {
        // 上へ行く
        if (i - 1 >= 0) {
          chmax(dp[i][j][0], dp[i - 1][j][k]);
          chmax(dp[i][j][1], dp[i - 1][j][k] + v[i][j]);
        }

        // 右へ行く
        if (j - 1 >= 0) {
          chmax(dp[i][j][k], dp[i][j - 1][k]);
          if (k - 1 >= 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
        }
      }
    }
  }
  long long res = 0;
  for (int k = 0; k <= 3; ++k)
    chmax(res, dp[R][C][k]);
  cout << res << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

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

  int R, C, K;
  cin >> R >> C >> K;
  vector<ll> r(K), c(K), v(K);
  vector<vector<ll>> cost(R + 5, vector<ll>(C + 5, 0));
  rep(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    cost[r[i]][c[i]] = v[i];
  }

  vector<vector<vector<ll>>> dp(
      R + 3000, vector<vector<ll>>(C + 3000, vector<ll>(10, 0)));

  if (cost[1][1] != 0)
    dp[1][1][1] = cost[1][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 + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3 && cost[i][j + 1] != 0) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + cost[i][j + 1]);
        }

        // 行
        if (cost[i + 1][j] != 0)
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + cost[i + 1][j]);
        else
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  ll ans = 0;
  rep(i, 4) { ans = max(ans, dp[R][C][i]); }

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

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  long v[r][c];
  rep(i, r) rep(j, c) v[i][j] = 0;
  // long dp[4][r][c];
  vector<vector<vector<long>>> dp(4, vector<vector<long>>(r, vector<long>(c)));
  rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
  rep(i, k) {
    long ri, ci, vi;
    cin >> ri >> ci >> vi;
    ri--;
    ci--;
    v[ri][ci] = vi;
  }
  rep(i, r - 1) {
    rep(j, 4) {
      rep(l, c - 1) {
        if (j != 3)
          dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
        dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
        if (j != 3)
          dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
        dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
      }
      if (j != 3)
        dp[0][i + 1][c - 1] =
            max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
      dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
    }
  }
  rep(j, 4) {
    rep(l, c - 1) {
      if (j != 3)
        dp[j + 1][r - 1][l + 1] =
            max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
      dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
    }
  }
  long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
                 dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[3][r - 1][c - 1]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  long v[r][c];
  rep(i, r) rep(j, c) v[i][j] = 0;
  long dp[4][r][c];
  rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
  rep(i, k) {
    long ri, ci, vi;
    cin >> ri >> ci >> vi;
    ri--;
    ci--;
    v[ri][ci] = vi;
  }
  rep(i, r - 1) {
    rep(j, 4) {
      rep(l, c - 1) {
        if (j != 3)
          dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
        dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
        if (j != 3)
          dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
        dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
      }
      if (j != 3)
        dp[0][i + 1][c - 1] =
            max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
      dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
    }
  }
  rep(j, 4) {
    rep(l, c - 1) {
      if (j != 3)
        dp[j + 1][r - 1][l + 1] =
            max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
      dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
    }
  }
  long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
                 dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
  ans = max(ans, dp[3][r - 1][c - 1]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;

void llchmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void llchmax(ll &a, ll b) {
  if (a < b)
    a = b;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vvi item(3010, vi(3010, -1));

  vector<vvl> dp(3010, vvl(3010, vl(4)));
  rep(i, 0, 3010) {
    rep(j, 0, 3010) {
      item[i][j] = -1;
      rep(d, 0, 4) { dp[i][j][d] = 0; }
    }
  }
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> item[x][y];
  }

  rep(i, 0, r) {
    rep(j, 0, c) {
      rep(d, 0, 4) {
        llchmax(dp[i][j + 1][d], dp[i][j][d]);
        llchmax(dp[i + 1][j][0], dp[i][j][d]);

        if (item[i][j] == -1 || d == 3) {
          continue;
        }

        ll v = item[i][j];
        llchmax(dp[i + 1][j][0], dp[i][j][d] + v);
        llchmax(dp[i][j + 1][d + 1], dp[i][j][d] + v);
      }
    }
  }

  cout << dp[r][c - 1][0] << endl;

  return 0;
}

Error: -11

Fixed submission:

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;

void llchmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void llchmax(ll &a, ll b) {
  if (a < b)
    a = b;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int item[3010][3010];

  ll dp[3010][3010][4];
  rep(i, 0, 3010) {
    rep(j, 0, 3010) {
      item[i][j] = -1;
      rep(d, 0, 4) { dp[i][j][d] = 0; }
    }
  }
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> item[x][y];
  }

  rep(i, 0, r) {
    rep(j, 0, c) {
      rep(d, 0, 4) {
        llchmax(dp[i][j + 1][d], dp[i][j][d]);
        llchmax(dp[i + 1][j][0], dp[i][j][d]);

        if (item[i][j] == -1 || d == 3) {
          continue;
        }

        ll v = item[i][j];
        llchmax(dp[i + 1][j][0], dp[i][j][d] + v);
        llchmax(dp[i][j + 1][d + 1], dp[i][j][d] + v);
      }
    }
  }

  cout << dp[r][c - 1][0] << endl;

  return 0;
}
Problem p02586 (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)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int r, c, k;
  cin >> r >> c >> k;
  vvi val(r + 5, vi(c + 5, 0));
  rep(i, k) {
    int rr, cc, v;
    cin >> rr >> cc >> v;
    rr--;
    cc--;
    val[rr][cc] = v;
  }

  vector<vll> dp(c + 5, vll(4, 0));
  dp[0][1] = val[0][0];
  ll mx = -1;

  rep(i, r) {
    vector<vll> pd(c + 5, vll(4, 0));
    rep(j, c) {
      rep(n, 4) dp[j + 1][n] = max(dp[j + 1][n], dp[j][n]);
      rep(n, 3) dp[j + 1][n + 1] =
          max(dp[j + 1][n + 1], dp[j][n] + val[i][j + 1]);
      mx = max({dp[j][0], dp[j][1], dp[j][2], dp[j][3]});
      pd[j][0] = mx;
      pd[j][1] = mx + val[i + 1][j];
    }
    dp = pd;
  }

  cout << mx << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, int> val;
  rep(i, k) {
    int rr, cc, v;
    cin >> rr >> cc >> v;
    rr--;
    cc--;
    val[{rr, cc}] = v;
  }

  vector<vll> dp(c + 5, vll(4, 0));
  dp[0][0] = 0;
  if (val[{0, 0}] > 0)
    dp[0][1] = val[{0, 0}];

  rep(i, r) {
    vector<vll> pd(c + 5, vll(4, 0));
    rep(j, c) {
      rep(n, 4) dp[j + 1][n] = max(dp[j + 1][n], dp[j][n]);
      if (val[{i, j + 1}] > 0) {
        rep(n, 3) dp[j + 1][n + 1] =
            max(dp[j + 1][n + 1], dp[j][n] + val[{i, j + 1}]);
      }
      ll mx = max({dp[j][0], dp[j][1], dp[j][2], dp[j][3]});
      pd[j][0] = mx;
      if (val[{i + 1, j}] > 0)
        pd[j][1] = max(pd[j][1], mx + val[{i + 1, j}]);
    }
    dp = pd;
  }

  /*
  rep(i, r){
      rep(j, c){
          cout << "{";
          rep(n, 4){
              cout << dp[i][j][n] << ",";
          }
          cout << "}, ";
      }
      cout << endl;
  }
  */

  ll ans = max({dp[c - 1][0], dp[c - 1][1], dp[c - 1][2], dp[c - 1][3]});
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mx = 3005;
ll grid[3005][3005];
ll dp[3005][3005][5];

ll r, c, kk;

ll solve(ll i, ll j, ll k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  ll ans = 0;
  if (grid[i][j] == 0) {
    ans = max(solve(i + 1, j, 0), solve(i, j + 1, k));
  } else {
    ll value = grid[i][j];
    if (k < 3) {
      ans = max(ans, value + solve(i, j + 1, k + 1));
      ans = max(ans, value + solve(i + 1, j, 0));
    }
    ans = max(ans, solve(i, j + 1, k));
    ans = max(ans, solve(i + 1, j, 0));
  }
  return dp[i][j][k] = ans;
}

ll _solve(ll i, ll j, ll k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  ll ans = 0;
  ll value = grid[i][j];
  if (k < 3) {
    ans = max(ans, value + solve(i, j + 1, k + 1));
    ans = max(ans, value + solve(i + 1, j, 0));
  }
  ans = max(ans, solve(i, j + 1, k));
  ans = max(ans, solve(i + 1, j, 0));

  return dp[i][j][k] = ans;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> r >> c >> kk;
  for (ll i = 0; i < kk; i++) {
    ll x, y;
    cin >> x >> y;
    ll val;
    cin >> val;
    grid[x - 1][y - 1] = val;
  }
  cout << _solve(0, 0, 0) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mx = 1005;
ll grid[1005][1005];
ll dp[1005][1005][5];

ll r, c, kk;

ll solve(ll i, ll j, ll k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  ll ans = 0;
  if (grid[i][j] == 0) {
    ans = max(solve(i + 1, j, 0), solve(i, j + 1, k));
  } else {
    ll value = grid[i][j];
    if (k < 3) {
      ans = max(ans, value + solve(i, j + 1, k + 1));
      ans = max(ans, value + solve(i + 1, j, 0));
    }
    ans = max(ans, solve(i, j + 1, k));
    ans = max(ans, solve(i + 1, j, 0));
  }
  return dp[i][j][k] = ans;
}

ll _solve(ll i, ll j, ll k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  ll ans = 0;
  ll value = grid[i][j];
  if (k < 3) {
    ans = max(ans, value + solve(i, j + 1, k + 1));
    ans = max(ans, value + solve(i + 1, j, 0));
  }
  ans = max(ans, solve(i, j + 1, k));
  ans = max(ans, solve(i + 1, j, 0));

  return dp[i][j][k] = ans;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> r >> c >> kk;
  for (ll i = 0; i < kk; i++) {
    ll x, y;
    cin >> x >> y;
    ll val;
    cin >> val;
    grid[x - 1][y - 1] = val;
  }
  cout << solve(0, 0, 0) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long

ll n, m, k;
ll a[3001][3001];
ll dp[3001][3001][4];
ll dfs(ll x, ll y, ll cur) {
  if (x == n && y == m) {
    return 0;
  }
  if (x == n)
    return 0;
  if (y == m)
    return 0;
  if (dp[x][y][cur] != -1)
    return dp[x][y][cur];
  ll ans = 0;
  if (a[x][y] == 0) {
    ans = max(dfs(x + 1, y, 0), dfs(x, y + 1, cur));
  } else {
    ll value = a[x][y];
    if (cur < 3) {
      ans = max(ans, value + dfs(x, y + 1, cur + 1));
      ans = max(ans, value + dfs(x + 1, y, 0));
    }
    ans = max(ans, dfs(x, y + 1, cur));
    ans = max(ans, dfs(x + 1, y, 0));
  }
  return dp[x][y][cur] = ans;
}
signed main() {
  // fastio
  memset(dp, -1, sizeof dp);
  cin >> n >> m >> k;
  for (ll i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--, y--;
    a[x][y] = z;
  }
  ll ans = dfs(0, 0, 0);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int mx = 1005;
int grid[1005][1005];
int dp[1005][1005][5];

int r, c, k;

int solve(int i, int j, int k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  int ans = 0;
  if (grid[i][j] == 0) {
    ans = max(solve(i + 1, j, 0), solve(i, j + 1, k));
  } else {
    int value = grid[i][j];
    if (k < 3) {
      ans = max(ans, value + solve(i, j + 1, k + 1));
      ans = max(ans, value + solve(i + 1, j, 0));
    }
    ans = max(ans, solve(i, j + 1, k));
    ans = max(ans, solve(i + 1, j, 0));
  }
  return dp[i][j][k] = ans;
}

int _solve(int i, int j, int k) {
  if (i == r || j == c) {
    return 0;
  }
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  int ans = 0;
  int value = grid[i][j];
  if (k < 3) {
    ans = max(ans, value + solve(i, j + 1, k + 1));
    ans = max(ans, value + solve(i + 1, j, 0));
  }
  ans = max(ans, solve(i, j + 1, k));
  ans = max(ans, solve(i + 1, j, 0));

  return dp[i][j][k] = ans;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> r >> c >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    int val;
    cin >> val;
    grid[x - 1][y - 1] = val;
  }
  cout << solve(0, 0, 0) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int R, C, K;
int v[3000][3000];

long long dp[3001][3001][4];

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

  cin >> R >> C >> K;
  rep(_, K) {
    int r, c, _v;
    cin >> r >> c >> _v;
    v[r - 1][c - 1] = _v;
  }

  long long ans = 0;
  dp[0][0][1] = v[0][0];
  rep(r, R) rep(c, C) rep(i, 4) {
    if (c + 1 < C) {
      dp[r][c + 1][i] = max(dp[r][c + 1][i], dp[r][c][i]);

      if (i < 3)
        dp[r][c + 1][i + 1] =
            max(dp[r][c + 1][i + 1], dp[r][c][i] + v[r][c + 1]);
    }
    if (r + 1 < R) {
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i]);

      dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][i] + v[r + 1][c]);
    }
    ans = max(ans, dp[r][c][i]);
  }

  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int R, C, K;
int v[3000][3000];

long long dp[3001][3001][4];

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

  cin >> R >> C >> K;
  rep(_, K) {
    int r, c, _v;
    cin >> r >> c >> _v;
    v[r - 1][c - 1] = _v;
  }

  long long ans = 0;
  dp[0][0][1] = v[0][0];
  rep(r, R + 1) rep(c, C + 1) rep(i, 4) {
    if (c < C) {
      dp[r][c + 1][i] = max(dp[r][c + 1][i], dp[r][c][i]);

      if (i < 3)
        dp[r][c + 1][i + 1] =
            max(dp[r][c + 1][i + 1], dp[r][c][i] + v[r][c + 1]);
    }
    if (r < R) {
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i]);

      dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][i] + v[r + 1][c]);
    }
    ans = max(ans, dp[r][c][i]);
  }

  cout << ans << endl;

  return 0;
}