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

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k;
int a[3001][3001];
int f[3001][3001][4];
int dp(int i, int j, int cnt) {
  if (i == n && j == m) {
    if (cnt < 3)
      return a[i][j];
    else
      return 0;
  }
  int &res = f[i][j][cnt];
  if (res != -1)
    return res;
  res = 0;

  if (cnt < 3) {
    if (j < m)
      res = max(res, max(dp(i, j + 1, cnt), a[i][j] + dp(i, j + 1, cnt + 1)));

    if (i < n)
      res = max(res, a[i][j] + dp(i + 1, j, 0));

  } else {
    if (j < m)
      res = max(res, dp(i, j + 1, cnt));
    if (i < n)
      res = max(res, dp(i + 1, j, 0));
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  memset(f, -1, sizeof f);
  for (int i = 1; i <= k; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    a[u][v] = w;
  }
  cout << dp(1, 1, 0);

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k;
int a[3001][3001];
int f[3001][3001][4];
int dp(int i, int j, int cnt) {
  if (i == n && j == m) {
    if (cnt < 3)
      return a[i][j];
    else
      return 0;
  }
  int &res = f[i][j][cnt];
  if (res != -1)
    return res;
  res = 0;

  if (cnt < 3) {
    if (j < m)
      res = max({res, dp(i, j + 1, cnt), a[i][j] + dp(i, j + 1, cnt + 1)});

    if (i < n)
      res = max(res, a[i][j] + dp(i + 1, j, 0));

  } else {
    if (j < m)
      res = max(res, dp(i, j + 1, cnt));
    if (i < n)
      res = max(res, dp(i + 1, j, 0));
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  memset(f, -1, sizeof f);
  for (int i = 1; i <= k; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    a[u][v] = w;
  }
  cout << dp(1, 1, 0);

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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int MXN = 3e3;
int n, m, k;
ll mp[MXN][MXN], dp[MXN][MXN][5];
ll pa, pb, pc, pd;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  //		freopen("in1.txt", "r", stdin);
  //		freopen("in1.txt", "w", stdout);
#endif
  cin >> n >> m >> k;
  memset(mp, 0, sizeof(mp));
  for (int i = 0; i < k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    mp[x][y] = z;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pa = pb = pc = pd = 0;
    for (int j = 0; j < m; j++) {
      dp[i][j][0] = max(pa, (i ? dp[i - 1][j][4] : 0));
      dp[i][j][1] = max(dp[i][j][0] + mp[i][j], pb); // verified
      dp[i][j][2] = max(pb + mp[i][j], pc);
      dp[i][j][3] = max(pc + mp[i][j], pd);
      pa = max(pa, dp[i][j][0]);
      pb = max(pb, dp[i][j][1]);
      pc = max(pc, dp[i][j][2]);
      pd = max(pd, dp[i][j][3]);
      for (int l = 0; l < 4; l++) {
        dp[i][j][4] = max(dp[i][j][4], dp[i][j][l]);
      }
    }
  }
  cout << dp[n - 1][m - 1][4] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int MXN = 1e3;
int n, m, k;
ll mp[MXN][MXN], dp[MXN][MXN][5];
ll pa, pb, pc, pd;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  //		freopen("in1.txt", "r", stdin);
  //		freopen("in1.txt", "w", stdout);
#endif
  cin >> n >> m >> k;
  memset(mp, 0, sizeof(mp));
  for (int i = 0; i < k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    mp[x][y] = z;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pa = pb = pc = pd = 0;
    for (int j = 0; j < m; j++) {
      dp[i][j][0] = max(pa, (i ? dp[i - 1][j][4] : 0));
      dp[i][j][1] = max(dp[i][j][0] + mp[i][j], pb); // verified
      dp[i][j][2] = max(pb + mp[i][j], pc);
      dp[i][j][3] = max(pc + mp[i][j], pd);
      pa = max(pa, dp[i][j][0]);
      pb = max(pb, dp[i][j][1]);
      pc = max(pc, dp[i][j][2]);
      pd = max(pd, dp[i][j][3]);
      for (int k = 0; k < 4; k++) {
        dp[i][j][4] = max(dp[i][j][4], dp[i][j][k]);
      }
    }
  }
  cout << dp[n - 1][m - 1][4] << '\\n';
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 3005;

ll ans;
ll dp[MAX][MAX][4];

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

  rep(i, 1, R + 1) rep(j, 1, C + 1) {
    rep(k, 1, 4) {
      dp[i][j][k] = max(dp[i][j - 1][k - 1] + M[i][j], dp[i][j - 1][k]);
      dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3] + M[i][j]);
    }
  }

  ans = dp[R][C][3];

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
const int MAX = 15;

ll ans;
ll dp[MAX][MAX][4];

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

  rep(i, 1, R + 1) rep(j, 1, C + 1) {
    rep(k, 0, 4) {
      dp[i][j][k] =
          max(dp[i - 1][j][3] + M[i][j], dp[i][j - 1][k - 1] + M[i][j]);
    }
  }

  ans = dp[R][C][3];

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

#include <bits/stdc++.h>

using namespace std;
#define endl "\\n"
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e3 + 5;
int R, C, k;
ll v[N][N], f[N][N][4];

ll trau(int i, int j, int taken) {
  if (i > R || j > C)
    return -1e18;
  if (taken > 3)
    return -1e18;
  if (i == R && j == C) {
    if (taken < 3)
      return v[R][C];
    return 0;
  }
  if (f[i][j][taken] != -1)
    return f[i][j][taken];
  ll res = -1e18;
  res = max(res, trau(i, j + 1, taken + 1)) + v[i][j];
  if (taken < 3)
    res = max(res, trau(i + 1, j, 0) + v[i][j]);
  else
    res = max(res, trau(i + 1, j, 0));
  res = max(res, trau(i, j + 1, taken));
  return f[i][j][taken] = res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 1; i <= k; ++i) {
    int r, c, va;
    cin >> r >> c >> va;
    v[r][c] = va;
  }
  memset(f, -1, sizeof f);
  cout << trau(1, 1, 0);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define endl "\\n"
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e3 + 5;
int R, C, k;
ll v[N][N], f[N][N][4];

ll trau(int i, int j, int taken) {
  if (i > R || j > C)
    return -1e18;
  if (taken > 3)
    return -1e18;
  if (i == R && j == C) {
    if (taken < 3)
      return v[R][C];
    return 0;
  }
  if (f[i][j][taken])
    return f[i][j][taken];
  ll res = -1e18;
  res = max(res, trau(i, j + 1, taken + 1)) + v[i][j];
  if (taken < 3)
    res = max(res, trau(i + 1, j, 0) + v[i][j]);
  else
    res = max(res, trau(i + 1, j, 0));
  res = max(res, trau(i, j + 1, taken));
  return f[i][j][taken] = res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 1; i <= k; ++i) {
    int r, c, va;
    cin >> r >> c >> va;
    v[r][c] = va;
  }
  cout << trau(1, 1, 0);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll r, c, k, a, b, d, dp[3001][3001][4], v[3001][3001];

int main() {
  cin >> r >> c >> k;
  for (int i = 1; i <= k; i++) {
    cin >> a >> b >> d;
    v[a][b] = d;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][3]);
        if (k) {
          dp[i][j][k] = max(
              dp[i][j][k], max(dp[i][j - 1][k - 1], dp[i - 1][j][3]) + v[i][j]);
        }
      }
    }
  }
  cout << dp[r][c][3] << endl;
}


Error: TLE

Fixed submission:

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

ll r, c, k, a, b, d, ans;
ll v[3001][3001];

void dfs(ll nr, ll nc, ll rcnt, ll now) {
  if (nr == r && nc == c) {
    if (rcnt < 3)
      now += v[nr][nc];
    ans = max(ans, now);
  } else {
    if (nc + 1 <= c)
      dfs(nr, nc + 1, 0, now);
    if (nr + 1 <= r)
      dfs(nr + 1, nc, rcnt, now);
    if (rcnt < 3) {
      rcnt++;
      now += v[nr][nc];
      if (nr + 1 <= r)
        dfs(nr + 1, nc, 0, now);
      if (nc + 1 <= c)
        dfs(nr, nc + 1, rcnt, now);
    }
  }
}

int main() {
  cin >> r >> c >> k;
  for (int i = 1; i <= k; i++) {
    cin >> a >> b >> d;
    v[a][b] = d;
  }
  dfs(1, 1, 0, 0);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits>
#include <numeric>
#include <type_traits>

using namespace std;

#define rep(i, n, m) for (ll(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (ll(i) = (n); (i) > (m); (i)--)
using ll = long long;
const ll mod = 998244353;

int main() {

  ll R, C, K;
  scanf("%lld %lld %lld", &R, &C, &K);

  vector<vector<ll>> lis(R + 1, vector<ll>(C + 1, 0));
  rep(i, 0, K) {
    ll r, c, v;
    scanf("%lld %lld %lld", &r, &c, &v);
    r--;
    c--;
    lis[r][c] += v;
  }

  vector<vector<vector<ll>>> dp(C + 1,
                                vector<vector<ll>>(R + 1, vector<ll>(4, 0)));

  rep(j, 0, C + 1) {
    rep(i, 0, R + 1) {
      rep(k, 0, 4) {
        // 取る
        if (k != 3) {

          // 右に行く
          if (j != C) {
            dp[j + 1][i][k + 1] =
                max(dp[j + 1][i][k + 1], dp[j][i][k] + lis[i][j]);
          }
          // 下に行く
          if (i != R) {
            dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][k] + lis[i][j]);
          }
        }
        // 取らない

        // 右に行く
        if (j != C) {
          dp[j + 1][i][k] = max(dp[j + 1][i][k], dp[j][i][k]);
        }
        // 下に行く
        if (i != R) {
          dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][k]);
        }
      }
    }
  }

  cout << max(dp[C][R][0], max(dp[C][R][1], max(dp[C][R][2], dp[C][R][3])))
       << endl;
}

Error: 0

Fixed submission:



#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits>
#include <numeric>
#include <type_traits>

using namespace std;

#define rep(i, n, m) for (ll(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (ll(i) = (n); (i) > (m); (i)--)
using ll = long long;
const ll mod = 998244353;

int main() {

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

  vector<vector<int>> lis(C + 1, vector<int>(R + 1, 0));
  rep(i, 0, K) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    lis[r][c] += v;
  }

  vector<vector<vector<ll>>> dp(C + 1,
                                vector<vector<ll>>(R + 1, vector<ll>(4, 0)));

  rep(j, 0, C + 1) {
    rep(i, 0, R + 1) {
      rep(k, 0, 4) {
        // 取る
        if (k != 3) {

          // 右に行く
          if (j != C) {
            dp[j + 1][i][k + 1] =
                max(dp[j + 1][i][k + 1], dp[j][i][k] + lis[i][j]);
          }
          // 下に行く
          if (i != R) {
            dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][k] + lis[i][j]);
          }
        }
        // 取らない

        // 右に行く
        if (j != C) {
          dp[j + 1][i][k] = max(dp[j + 1][i][k], dp[j][i][k]);
        }
        // 下に行く
        if (i != R) {
          dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][k]);
        }
      }
    }
  }

  cout << max(dp[C][R][0], max(dp[C][R][1], max(dp[C][R][2], dp[C][R][3])))
       << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long int
using namespace std;
int mod = 1e9 + 7;

int mat[3001][3001];
// int track[3001];
int dp[3001][3001][5];
int n, m, k;

int find(int i, int j, int track) {
  if (i >= n || j >= m)
    return INT_MIN;
  if (i == n - 1 && j == m - 1)
    return (track < 3 ? mat[i][j] : 0);
  if (dp[i][j][track] != -1)
    return dp[i][j][track];
  int res = INT_MIN;
  if (mat[i][j] && track < 3) {
    res = max(res, mat[i][j] +
                       max(find(i + 1, j, (int)0), find(i, j + 1, track + 1)));
  }
  res = max(res, max(find(i + 1, j, (int)0), find(i, j + 1, track)));
  return dp[i][j][track] = res;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;

  cin >> n >> m >> k;
  int r, c, val;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> val;
    mat[r - 1][c - 1] = val;
  }
  memset(dp, -1, sizeof dp);
  cout << find((int)0, (int)0, (int)0);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
// #define int long long int
using namespace std;
int mod = 1e9 + 7;

int mat[9001][9001];
// int track[3001];
int dp[9001][9001][5];
int n, m, k;

int find(int i, int j, int track) {
  if (i >= n || j >= m)
    return INT_MIN;
  if (i == n - 1 && j == m - 1)
    return (track < 3 ? mat[i][j] : 0);
  if (dp[i][j][track] != -1)
    return dp[i][j][track];
  int res = INT_MIN;
  if (mat[i][j] && track < 3) {
    res = max(res, max(mat[i][j] + find(i + 1, j, (int)0),
                       mat[i][j] + find(i, j + 1, track + 1)));
  }
  res = max(res, max(find(i + 1, j, (int)0), find(i, j + 1, track)));
  return dp[i][j][track] = res;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;

  cin >> n >> m >> k;
  int r, c, val;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> val;
    mat[r - 1][c - 1] = val;
  }
  memset(dp, -1, sizeof dp);
  cout << find((int)0, (int)0, (int)0);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF pow(10, 9) + 7
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long int ll;
using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;
#define chmax(x, y) x = max(x, y)

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

int main() {

  fastio;

  ll R, C, n;
  cin >> R >> C >> n;
  rep(i, n) {
    ll in1, in2, in3;
    cin >> in1 >> in2 >> in3;
    a[in1 - 1][in2 - 1] = in3;
  }

  rep(i, R) {
    rep(j, C) {
      dp[i + 1][j + 1][0] = max(dp[i][j + 1][3], dp[i + 1][j][0]);
      for (int k = 2; k >= 0; k--) {
        chmax(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      rep(k, 4) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }

  ll ans = 0;
  rep(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define INF pow(10, 9) + 7
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long int ll;
using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

ll R, C, K, in1, in2, in3;
int v[3000][3000];
int dp[3005][3005][4];

int main() {

  fastio;

  cin >> R >> C >> K;
  rep(i, K) {
    cin >> in1 >> in2 >> in3;
    v[in1][in2] = in3;
  }

  rep(i, R) {
    rep(j, C) {
      dp[i + 1][j + 1][0] = max(dp[i][j + 1][3], dp[i + 1][j][0]);
      rep(k, 3) {
        dp[i + 1][j + 1][k + 1] =
            max(dp[i][j + 1][3] + v[i + 1][j + 1],
                max(dp[i + 1][j][k] + v[i + 1][j + 1], dp[i + 1][j][k + 1]));
      }
    }
  }

  ll ans = dp[R][C][3];
  cout << ans << endl;

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

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
ll a[3000][3000], dp[3000][3000][4];
int main() {
  ll R, C, K, i, j, k, r, c, u;
  scanf("%lld%lld%lld", &R, &C, &K);
  for (i = 0; i < K; i++) {
    scanf("%lld%lld%lld", &r, &c, &u);
    r--;
    c--;
    a[r][c] = u;
  }
  dp[0][0][1] = a[0][0];
  for (j = 1; j < C; j++) {
    for (k = 0; k < 4; k++) {
      dp[0][j][k] = dp[0][j - 1][k];
      if (a[0][j] && k > 0)
        dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k - 1] + a[0][j]);
    }
  }
  for (i = 1; i < R; i++) {
    for (j = 0; j < C; j++) {
      dp[i][j][0] = *max_element(dp[i - 1][j], dp[i - 1][j] + 4);
      if (a[i][j])
        dp[i][j][1] = dp[i][j][0] + a[i][j];
      for (k = 0; k < 4; k++) {
        if (j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (a[i][j] && j > 0 && k > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
      }
    }
  }
  printf("%lld\\n", *max_element(dp[R - 1][C - 1], dp[R - 1][C - 1] + 4));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
int main() {
  ll R, C, K, i, j, k, r, c, u;
  scanf("%lld%lld%lld", &R, &C, &K);
  ll a[R][C], dp[R][C][4];
  memset(a, 0, sizeof(a));
  for (i = 0; i < K; i++) {
    scanf("%lld%lld%lld", &r, &c, &u);
    r--;
    c--;
    a[r][c] = u;
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][1] = a[0][0];
  for (j = 1; j < C; j++) {
    for (k = 0; k < 4; k++) {
      dp[0][j][k] = dp[0][j - 1][k];
      if (a[0][j] && k > 0)
        dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k - 1] + a[0][j]);
    }
  }
  for (i = 1; i < R; i++) {
    for (j = 0; j < C; j++) {
      dp[i][j][0] = *max_element(dp[i - 1][j], dp[i - 1][j] + 4);
      if (a[i][j])
        dp[i][j][1] = dp[i][j][0] + a[i][j];
      for (k = 0; k < 4; k++) {
        if (j > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (a[i][j] && j > 0 && k > 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
      }
    }
  }
  printf("%lld\\n", *max_element(dp[R - 1][C - 1], dp[R - 1][C - 1] + 4));
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll mem[3333][3333][5];
ll a[3333][3333];
ll R, C, K;

ll rec(ll x, ll y, ll k) {
  if (mem[x][y][k] != -1)
    return mem[x][y][k];

  ll res = 0;

  if (x + 1 < R) {
    if (a[x + 1][y] > 0) {
      chmax(res, rec(x + 1, y, 1) + a[x + 1][y]);
      chmax(res, rec(x + 1, y, 0));
    } else {
      chmax(res, rec(x + 1, y, 0));
    }
  }

  if (y + 1 < C) {
    if (a[x][y + 1] > 0) {
      if (k + 1 <= 3)
        chmax(res, rec(x, y + 1, k + 1) + a[x][y + 1]);

      chmax(res, rec(x, y + 1, k));
    } else {
      chmax(res, rec(x, y + 1, k));
    }
  }

  return mem[x][y][k] = res;
}

int main(void) {
  ll i, j, k;

  cin >> R >> C >> K;

  for (i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    a[r][c] = v;
  }

  for (i = 0; i < R; i++)
    for (j = 0; j < C; j++)
      for (k = 0; k < 5; k++)
        mem[i][j][k] = -1;

  pt(max(rec(0, 0, 0), rec(0, 0, 1) + a[0][0]));
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll mem[3333][3333][5];
ll a[3333][3333];
ll R, C, K;

ll rec(ll x, ll y, ll k) {
  if (mem[x][y][k] != -1)
    return mem[x][y][k];

  ll res = 0;

  if (x + 1 < R) {
    if (a[x + 1][y] > 0) {
      chmax(res, rec(x + 1, y, 1) + a[x + 1][y]);
      chmax(res, rec(x + 1, y, 0));
    } else {
      chmax(res, rec(x + 1, y, 0));
    }
  }

  if (y + 1 < C) {
    if (a[x][y + 1] > 0) {
      if (k + 1 <= 3)
        chmax(res, rec(x, y + 1, k + 1) + a[x][y + 1]);

      chmax(res, rec(x, y + 1, k));
    } else {
      chmax(res, rec(x, y + 1, k));
    }
  }

  return mem[x][y][k] = res;
}

int main(void) {
  ll i, j, k;

  cin >> R >> C >> K;

  ll r[3333], c[3333], v[3333];
  for (i = 0; i < K; i++) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--;
    c[i]--;
    a[r[i]][c[i]] = v[i];
  }

  memset(mem, -1, sizeof(mem));
  pt(max(rec(0, 0, 0), rec(0, 0, 1) + a[0][0]));
}