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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int N = 3005;
int a[N][N];
int dp[N][N][4];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, k, i, j, x, y, v;
  cin >> n >> m >> k;

  for (i = 0; i < k; i++) {
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j][3], dp[i][j - 1][0]));
      dp[i][j][1] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][1]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
      dp[i][j][2] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][2]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + a[i][j]);
      dp[i][j][3] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][3]);
      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + a[i][j]);
    }
  }
  cout << dp[n][m][3];
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, k, i, j, x, y, v;
  cin >> n >> m >> k;
  int a[n + 5][m + 5];
  int dp[n + 5][m + 5][5];
  memset(a, 0, sizeof(a));
  memset(dp, 0, sizeof(dp));
  for (i = 0; i < k; i++) {
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j][3], dp[i][j - 1][0]));
      dp[i][j][1] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][1]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
      dp[i][j][2] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][2]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + a[i][j]);
      dp[i][j][3] = max(dp[i - 1][j][3] + a[i][j], dp[i][j - 1][3]);
      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + a[i][j]);
    }
  }
  cout << dp[n][m][3];
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <locale.h>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int R, C, K;
int v[3002][3002] = {};
long long int mae[3002] = {};   // 前の行の最大スコア
long long int nw[3002][4] = {}; // 今の行のスコア
int main() {
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int a, b, vl;
    scanf("%d %d %d", &a, &b, &vl);
    v[a][b] = vl;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int l = 1; l <= 3; l++) {
        nw[j][l] = max(nw[j - 1][l],
                       max(nw[j - 1][l - 1] + v[i][j], mae[j] + v[i][j]));
      }
    }
    for (int j = 1; j <= C; j++) {
      mae[j] = nw[j][3];
    }
  }
  long long int ans = 0;
  printf("%lld", nw[C][3]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <locale.h>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;

int R, C, K;
int v[3002][3002] = {};
long long int mae[3002] = {};   // 前の行の最大スコア
long long int nw[3002][4] = {}; // 今の行のスコア
int main() {
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int a, b, vl;
    scanf("%d %d %d", &a, &b, &vl);
    v[a][b] = vl;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int l = 0; l <= 3; l++) {
        nw[j][l] = max(nw[j - 1][l], mae[j]);
      } // 拾わない処理
      if (v[i][j] != 0) {
        for (int l = 1; l <= 3; l++) {
          nw[j][l] =
              max(nw[j][l], max(nw[j - 1][l - 1] + v[i][j], mae[j] + v[i][j]));
        } // 拾う処理
      }
      for (int j = 1; j <= C; j++) {
        mae[j] = nw[j][3];
      }
    }
  }
  long long int ans = 0;
  printf("%lld", nw[C][3]);
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 3e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

ll dp[N][N][4], n, m, k, a[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    a[x][y] += v;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int x = 0; x <= 3; x++) {

        ll res = 0;
        for (int y = 0; y <= 3; y++) {
          res = max(res, dp[i - 1][j][y]);
        }
        res = max(res, dp[i][j - 1][x]);
        dp[i][j][x] = res;

        if (!x)
          continue;
        res = 0;
        for (int y = 0; y <= 3; y++)
          res = max(res, dp[i - 1][j][y]);
        res = max(res, dp[i][j - 1][x - 1]);
        dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
      }
    }
  }
  cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 2e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

ll dp[N][N][4], n, m, k, a[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    a[x][y] += v;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int x = 0; x <= 3; x++) {

        ll res = 0;
        for (int y = 0; y <= 3; y++) {
          res = max(res, dp[i - 1][j][y]);
        }
        res = max(res, dp[i][j - 1][x]);
        dp[i][j][x] = res;

        if (!x)
          continue;
        res = 0;
        for (int y = 0; y <= 3; y++)
          res = max(res, dp[i - 1][j][y]);
        res = max(res, dp[i][j - 1][x - 1]);
        dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
      }
    }
  }
  cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<int>> item(R + 1, vector<int>(C + 1, 0));
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    item[r][c] = v;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  // vector<vector<int>> item(K,vector<int>(3));
  map<pair<int, int>, ll> m;
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    m[{r, c}] = v;
  }

  vector<vector<vector<ll>>> dp(R + 1,
                                vector<vector<ll>>(C + 1, vector<ll>(4, 0)));
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][0] = max(dp[i - 1][j][k], dp[i][j][0]);
        dp[i][j][1] = max(dp[i - 1][j][k] + m[{i, j}], dp[i][j][1]);
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + m[{i, j}]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      for (int k = 2; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j - 1][k - 1] + m[{i, j}], dp[i][j - 1][k]);
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i < 4; i++) {
    if (ans < dp[R][C][i]) {
      ans = dp[R][C][i];
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int r, c, k;
  cin >> r >> c >> k;
  vector<unordered_map<int, ll>> v(r);
  {
    int R, C, V;
    fr(i, k) {
      cin >> R >> C >> V;
      --R, --C;
      v[R][C] = V;
    }
  }
  ll dp[3000][4] = {}, ndp[3000][4];
  dp[0][1] = v[0][0];
  fr(i, r) {
    fr(j, c) fr(l, 4) ndp[j][l] = 0;
    fr(j, c) fr(l, 4) {
      if (i < r - 1) {
        chmax(ndp[j][0], dp[j][l]);
        chmax(ndp[j][1], dp[j][l] + v[i + 1][j]);
      }
      if (j < c - 1) {
        chmax(dp[j + 1][l], dp[j][l]);
        if (l < 3)
          chmax(dp[j + 1][l + 1], dp[j][l] + v[i][j + 1]);
      }
    }
    swap(dp, ndp);
  }
  cout << max({ndp[c - 1][0], ndp[c - 1][1], ndp[c - 1][2], ndp[c - 1][3]})
       << '\\n';
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int r, c, k;
  cin >> r >> c >> k;
  ll v[3000][3000] = {};
  {
    int R, C, V;
    fr(i, k) {
      cin >> R >> C >> V;
      --R, --C;
      v[R][C] = V;
    }
  }
  ll dp[3000][3000][4] = {};
  dp[0][0][1] = v[0][0];
  fr(i, r) fr(j, c) fr(l, 4) {
    if (i < r - 1) {
      chmax(dp[i + 1][j][0], dp[i][j][l]);
      chmax(dp[i + 1][j][1], dp[i][j][l] + v[i + 1][j]);
    }
    if (j < c - 1) {
      chmax(dp[i][j + 1][l], dp[i][j][l]);
      if (l < 3)
        chmax(dp[i][j + 1][l + 1], dp[i][j][l] + v[i][j + 1]);
    }
  }
  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]})
       << '\\n';
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("Ofast")
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = s; i < (int)(n); ++i)
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
constexpr int INF = 1e9 + 7;
constexpr ll LINF = 1LL << 60;

ll dp[4][3005][3005];

void solve() {
  int R, C, K;
  cin >> R >> C >> K;
  map<ll, ll> d;
  V<ll> s;
  rep(i, K) {
    ll r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    d[r * (ll)C + c] = v;
    s.push_back(r * (ll)C + c);
  }
  sort(s.begin(), s.end());
  rep(k, 4) rep(i, R) rep(j, C) dp[k][i][j] = 0;

  rep(i, R) rep(j, C) {
    rep(k, 4) {
      if (i > 0) {
        dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j]);
      }
    }
    rep(k, 4) {
      if (j > 0) {
        dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1]);
      }
    }
    auto idx = lower_bound(s.begin(), s.end(), i * C + j);
    if (*idx == i * C + j) {
      for (int k = 2; k >= 0; k--) {
        //
        ll v = d[i * C + j];
        dp[k + 1][i][j] = max(dp[k + 1][i][j], dp[k][i][j] + v);
      }
    }
  }

  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[i][R - 1][C - 1]);
  cout << ans << "\\n";
}
int main() {
  cin.tie(nullptr);
  // ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  solve();
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("Ofast")
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = s; i < (int)(n); ++i)
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
constexpr int INF = 1e9 + 7;
constexpr ll LINF = 1LL << 60;

ll dp[4][3005][3005];

void solve() {
  int R, C, K;
  cin >> R >> C >> K;
  map<ll, ll> d;
  rep(i, K) {
    ll r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    d[r * (ll)C + c] = v;
  }
  rep(k, 4) rep(i, R) rep(j, C) dp[k][i][j] = 0;

  rep(i, R) rep(j, C) {
    rep(k, 4) {
      if (i > 0) {
        dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j]);
      }
    }
    rep(k, 4) {
      if (j > 0) {
        dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1]);
      }
    }
    auto v = d[i * C + j];
    if (v > 0) {
      for (int k = 2; k >= 0; k--) {
        //
        dp[k + 1][i][j] = max(dp[k + 1][i][j], dp[k][i][j] + v);
      }
    }
  }

  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[i][R - 1][C - 1]);
  cout << ans << "\\n";
}
int main() {
  cin.tie(nullptr);
  // ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  solve();
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
template <class T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define myfill(a, val, type)                                                   \\
  fill((type *)a, (type *)a + sizeof(a) / sizeof(type), val)
const long long INF = 1e18;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int V[3005][3005] = {};
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }
  long long dp[3005][4];
  myfill(dp, -INF, long long);
  dp[0][0] = 0;
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        long long x = dp[j][k];
        if (k < 3)
          chmax(dp[j + 1][k + 1], x + V[i][j]);
        chmax(dp[j + 1][k], x);

        chmax(dp[j][0], x);
        if (k < 3)
          chmax(dp[j][0], x + V[i][j]);
      }
    }
  }
  long long ans = dp[C][0];
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
template <class T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define myfill(a, val, type)                                                   \\
  fill((type *)a, (type *)a + sizeof(a) / sizeof(type), val)
const long long INF = 1e18;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  long long V[3005][3005] = {};
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }
  long long dp[3005][3005][4];
  myfill(dp, -INF, long long);
  dp[0][0][0] = 0;
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3) {
          chmax(dp[i + 1][j][0], dp[i][j][k] + V[i][j]);
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j]);
        }
      }
    }
  }
  long long ans = dp[R + 1][C][0];
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
int A[3100][3100], B[3100][3100][5];
signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  for (int i = 0; i < K; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    A[x - 1][y - 1] = c;
  }
  for (int i = 0; i < H; i++) {
    B[i + 1][0][0] =
        max({B[i + 1][0][0], B[i][0][0], B[i][0][1], B[i][0][2], B[i][0][3]});
    if (A[i][0])
      B[i + 1][0][1] = B[i + 1][0][0] + A[i][0];
    for (int j = 1; j < W; j++) {
      for (int k = 0; k < 4; k++)
        B[i + 1][j][k] = B[i + 1][j - 1][k];
      B[i + 1][j][0] =
          max(B[i + 1][j][0],
              max({B[i][j][0], B[i][j][1], B[i][j][2], B[i][j][3]}));
      if (A[i][j])
        B[i + 1][j][1] = max(B[i + 1][j][1], B[i + 1][j][0] + A[i][j]);
      for (int k = 0; k < 3; k++)
        if (B[i + 1][j - 1][k])
          B[i + 1][j][k + 1] =
              max(B[i + 1][j][k + 1], B[i + 1][j - 1][k] + A[i][j]);
    }
  }
  cout << max({B[H][W - 1][0], B[H][W - 1][1], B[H][W - 1][2], B[H][W - 1][3]})
       << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  int A[3100][3100]{}, B[3100][3100][5]{};
  for (int i = 0; i < K; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    A[x - 1][y - 1] = c;
  }
  for (int i = 0; i < H; i++) {
    B[i + 1][0][0] =
        max({B[i + 1][0][0], B[i][0][0], B[i][0][1], B[i][0][2], A[i][0]});
    for (int j = 1; j < W; j++) {
      for (int k = 0; k < 3; k++)
        B[i + 1][j][k] = B[i + 1][j - 1][k];
      B[i + 1][j][0] = max(B[i + 1][j][0],
                           max({B[i][j][0], B[i][j][1], B[i][j][2]}) + A[i][j]);
      for (int k = 0; k < 2; k++)
        if (B[i + 1][j - 1][k])
          B[i + 1][j][k + 1] =
              max(B[i + 1][j][k + 1], B[i + 1][j - 1][k] + A[i][j]);
    }
  }
  cout << max({B[H][W - 1][0], B[H][W - 1][1], B[H][W - 1][2]}) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;

int items[3000][3000];
long long dp[3000][3000][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  memset(items, 0, sizeof items);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    items[r - 1][c - 1] = v;
  }
  memset(dp, 0, sizeof dp);
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++)
      for (int d = 3; d >= 0; d--)
        dp[r][c][d] = -(1LL << 60);
  dp[0][0][0] = 0;
  dp[0][0][1] = items[0][0];
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++)
      for (int d = 3; d >= 0; d--) {
        if (r < R - 1) {
          dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][d]);
          dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][d] + items[r + 1][c]);
        }
        if (c < C - 1) {
          dp[r][c + 1][d] = max(dp[r][c + 1][d], dp[r][c][d]);
          if (d < 3) {
            dp[r][c + 1][d + 1] =
                max(dp[r][c + 1][d + 1], dp[r][c][d] + items[r][c + 1]);
          }
        }
      }
  // for (int d = 0; d < 4; d++) {
  //     for (int r = 0; r < R; r++) {
  //         for (int c = 0; c < C; c++) {
  //             cerr << dp[r][c][d] << ' ';
  //         }
  //         cerr << endl;
  //     }
  //     cerr << endl;
  // }

  long long ans = 0;
  for (int d = 3; d >= 0; d--)
    ans = max(ans, dp[R - 1][C - 1][d]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int items[R][C];
  memset(items, 0, sizeof items);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    items[r - 1][c - 1] = v;
  }
  long long dp[R][C][4];
  memset(dp, 0, sizeof dp);
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++)
      for (int d = 3; d >= 0; d--)
        dp[r][c][d] = -(1LL << 60);
  dp[0][0][0] = 0;
  dp[0][0][1] = items[0][0];
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++)
      for (int d = 3; d >= 0; d--) {
        if (r < R - 1) {
          dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][d]);
          dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][d] + items[r + 1][c]);
        }
        if (c < C - 1) {
          dp[r][c + 1][d] = max(dp[r][c + 1][d], dp[r][c][d]);
          if (d < 3) {
            dp[r][c + 1][d + 1] =
                max(dp[r][c + 1][d + 1], dp[r][c][d] + items[r][c + 1]);
          }
        }
      }
  // for (int d = 0; d < 4; d++) {
  //     for (int r = 0; r < R; r++) {
  //         for (int c = 0; c < C; c++) {
  //             cerr << dp[r][c][d] << ' ';
  //         }
  //         cerr << endl;
  //     }
  //     cerr << endl;
  // }

  long long ans = 0;
  for (int d = 3; d >= 0; d--)
    ans = max(ans, dp[R - 1][C - 1][d]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];

bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }

long long dp(int r, int c, int rem) {
  if (memo[r][c][rem] != -1)
    return memo[r][c][rem];

  memo[r][c][rem] = 0;
  if (r == R && c == C) {
    if (rem > 0 && grid[r][c] != 0)
      memo[r][c][rem] = grid[r][c];

    else
      memo[r][c][rem] = 0;

    return memo[r][c][rem];
  }

  else {
    if (exists(r + 1, c)) {
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);

      else
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
    }

    if (exists(r, c + 1)) {
      memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] =
            max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
    }

    return memo[r][c][rem];
  }
}

int main() {
  scanf("%d%d%d", &R, &C, &K);

  for (int i = 1; i <= K; i++) {
    int ri, ci, vi;
    scanf("%d%d%d", &ri, &ci, &vi);
    grid[ri][ci] = vi;
  }

  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++)
        memo[i][j][k] = -1;
    }
  }

  printf("%lld", dp(1, 1, 3));
}


Error: -11

Fixed submission:

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

int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];

bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }

long long dp(int r, int c, int rem) {
  if (memo[r][c][rem] != -1)
    return memo[r][c][rem];

  memo[r][c][rem] = 0;
  if (r == R && c == C) {
    if (rem > 0 && grid[r][c] != 0)
      memo[r][c][rem] = grid[r][c];

    else
      memo[r][c][rem] = 0;

    return memo[r][c][rem];
  }

  else {
    if (exists(r + 1, c)) {
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);

      else
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
    }

    if (exists(r, c + 1)) {
      memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] =
            max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
    }

    return memo[r][c][rem];
  }
}

int main() {
  scanf("%d%d%d", &R, &C, &K);

  for (int i = 1; i <= K; i++) {
    int ri, ci, vi;
    scanf("%d%d%d", &ri, &ci, &vi);
    grid[ri][ci] = vi;
  }

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++)
        memo[i][j][k] = -1;
    }
  }

  printf("%lld", dp(1, 1, 3));
}