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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

#define MAX 3000

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll item[MAX + 1][MAX + 1];
ll dp[MAX + 1][MAX + 1][4];

signed main() {
  int R, C, k;
  cin >> R >> C >> k;
  int r, c, v;
  rep(i, k) {
    cin >> r >> c >> v;
    item[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (item[i][j] > 0) {
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + item[i][j]);
          chmax(dp[i][j][1], dp[i - 1][j][k] + item[i][j]);
        }
        chmax(dp[i][j][k], dp[i][j - 1][k]);
        chmax(dp[i][j][0], dp[i - 1][j][k]);
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k <= 3; k++) {
    chmax(ans, dp[R][C][k]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

signed main() {
  int R, C, k;
  cin >> R >> C >> k;
  ll item[R][C];
  fill(item[0], item[R], 0);
  int r, c, v;
  rep(i, k) {
    cin >> r >> c >> v;
    r--;
    c--;
    item[r][c] = v;
  }
  ll dp[R + 1][C + 1][4];
  fill(dp[0][0], dp[R + 1][0], 0);
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (item[i][j] > 0 && k != 3) {
          chmax(dp[i + 1][j + 1][k + 1], dp[i + 1][j][k] + item[i][j]);
          chmax(dp[i + 1][j + 1][0], dp[i][j + 1][k] + item[i][j]);
        }
        chmax(dp[i + 1][j + 1][k], dp[i + 1][j][k]);
        chmax(dp[i + 1][j + 1][0], dp[i][j + 1][k]);
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k <= 3; k++) {
    chmax(ans, dp[R][C][k]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define all(v) (v).begin(), (v).end()
#define UNIQUE(v) (v).erase(unique(all(v)), (v).end());
#define MOD 1000000007

ll item[3000][3000];
ll dp[3000][3000][4];

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

  REP(i, r) REP(j, c) item[i][j] = 0;
  REP(i, k) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> item[x][y];
  }

  REP(i, r) REP(j, c) REP(l, 4) dp[i][j][l] = 0;

  REP(i, r) REP(j, c) {
    ll tmp = 0;
    if (i != 0)
      tmp = max(
          {dp[j][i - 1][0], dp[j][i - 1][1], dp[j][i - 1][2], dp[j][i - 1][3]});

    if (j != 0) {
      dp[j][i][0] = max(dp[j - 1][i][0], tmp);
      dp[j][i][1] = dp[j - 1][i][1];
      dp[j][i][2] = dp[j - 1][i][2];
      dp[j][i][3] = dp[j - 1][i][3];

      if (item[i][j] != 0) {
        dp[j][i][1] = max(
            {dp[j - 1][i][1], dp[j - 1][i][0] + item[i][j], tmp + item[i][j]});
        dp[j][i][2] = max({dp[j - 1][i][2], dp[j - 1][i][1] + item[i][j]});
        dp[j][i][3] = max({dp[j - 1][i][3], dp[j - 1][i][2] + item[i][j]});
      }
    } else {
      dp[j][i][0] = tmp;
      dp[j][i][1] = tmp + item[i][j];
      dp[j][i][2] = 0;
      dp[j][i][3] = 0;
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define all(v) (v).begin(), (v).end()
#define UNIQUE(v) (v).erase(unique(all(v)), (v).end());
#define MOD 1000000007

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll item[r][c];
  ll dp[c][r][4];

  REP(i, r) REP(j, c) item[i][j] = 0;
  REP(i, k) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cin >> item[x][y];
  }

  REP(i, r) REP(j, c) REP(l, 4) dp[i][j][l] = 0;

  REP(i, r) REP(j, c) {
    ll tmp = 0;
    if (i != 0)
      tmp = max(
          {dp[j][i - 1][0], dp[j][i - 1][1], dp[j][i - 1][2], dp[j][i - 1][3]});

    if (j != 0) {
      dp[j][i][0] = max(dp[j - 1][i][0], tmp);
      dp[j][i][1] = dp[j - 1][i][1];
      dp[j][i][2] = dp[j - 1][i][2];
      dp[j][i][3] = dp[j - 1][i][3];

      if (item[i][j] != 0) {
        dp[j][i][1] = max(
            {dp[j - 1][i][1], dp[j - 1][i][0] + item[i][j], tmp + item[i][j]});
        dp[j][i][2] = max({dp[j - 1][i][2], dp[j - 1][i][1] + item[i][j]});
        dp[j][i][3] = max({dp[j - 1][i][3], dp[j - 1][i][2] + item[i][j]});
      }
    } else {
      dp[j][i][0] = tmp;
      dp[j][i][1] = tmp + item[i][j];
      dp[j][i][2] = 0;
      dp[j][i][3] = 0;
    }
  }

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

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

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

#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;

int dp[4][3000][3000];
int a[3000][3000];

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int x = 0; x <= 3; x++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        dp[x][i][j] = a[i][j] = 0;
  while (k--) {
    int x, y, z;
    cin >> x >> y >> z;
    a[--x][--y] = z;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      for (int x = 1; x <= 3; x++)
        dp[x][0][0] = a[0][0];
    } else {
      for (int x = 0; x <= 3; x++) {
        dp[0][i][0] = dp[x][i - 1][0];
        dp[1][i][0] = max(dp[1][i][0], dp[x][i - 1][0] + a[i][0]);
      }
    }
    for (int j = 1; j < m; j++) {
      for (int x = 0; x <= 3; x++) {
        dp[x][i][j] = max(dp[x][i][j], dp[x][i][j - 1]);
        if (x)
          dp[x][i][j] = max(dp[x][i][j], dp[x - 1][i][j - 1] + a[i][j]);
      }
      if (i) {
        for (int x = 0; x <= 3; x++) {
          dp[0][i][j] = max(dp[0][i][j], dp[x][i - 1][j]);
          dp[1][i][j] = max(dp[1][i][j], dp[x][i - 1][j] + a[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[i][n - 1][m - 1]);
  cout << ans << "\\n";
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}

Error: 0

Fixed submission:

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

#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n][m];
  memset(a, 0, sizeof(a));
  int dp[4][n][m];
  memset(dp, 0, sizeof(dp));
  while (k--) {
    int x, y, z;
    cin >> x >> y >> z;
    a[--x][--y] = z;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      for (int x = 1; x <= 3; x++)
        dp[x][0][0] = a[0][0];
    } else {
      for (int x = 0; x <= 3; x++) {
        dp[0][i][0] = dp[x][i - 1][0];
        for (int x = 0; x <= 3; x++)
          dp[1][i][0] = max(dp[1][i][0], dp[x][i - 1][0] + a[i][0]);
      }
    }
    for (int j = 1; j < m; j++) {
      for (int x = 0; x <= 3; x++) {
        dp[x][i][j] = max(dp[x][i][j], dp[x][i][j - 1]);
        if (x)
          dp[x][i][j] = max(dp[x][i][j], dp[x - 1][i][j - 1] + a[i][j]);
      }
      if (i) {
        for (int x = 0; x <= 3; x++) {
          dp[0][i][j] = max(dp[0][i][j], dp[x][i - 1][j]);
          dp[1][i][j] = max(dp[1][i][j], dp[x][i - 1][j] + a[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[i][n - 1][m - 1]);
  cout << ans << "\\n";
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

typedef long long int64;

const int MAXN = 3000 + 10;

int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];

#define chkmax(x, y) x = std::max(x, y)

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("e.in", "r", stdin);
  //     freopen("e.out", "w", stdout);
  // #endif

  int i, j, k;
  int x, y;

  scanf("%d %d %d", &n, &m, &q);
  for (i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    scanf("%d", &a[x][y]);
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      for (k = 1; k < 4; k++) {
        chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
        chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
      }
      for (k = 1; k < 4; k++)
        chkmax(f[i][j][k], f[i][j][k - 1]);
      for (k = 0; k < 4; k++)
        chkmax(f[i][j][k], f[i][j - 1][k]);
    }
    for (j = 1; j <= m; j++)
      for (k = 0; k < 4; k++)
        f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
  }
  printf("%lld\\n", f[n][m][3]);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>

typedef long long int64;

const int MAXN = 3000;

int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];

#define chkmax(x, y) x = std::max(x, y)

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("e.in", "r", stdin);
  //     freopen("e.out", "w", stdout);
  // #endif

  int i, j, k;
  int x, y;

  scanf("%d %d %d", &n, &m, &q);
  for (i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    scanf("%d", &a[x][y]);
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      for (k = 1; k < 4; k++) {
        chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
        chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
      }
      for (k = 1; k < 4; k++)
        chkmax(f[i][j][k], f[i][j][k - 1]);
      for (k = 0; k < 4; k++)
        chkmax(f[i][j][k], f[i][j - 1][k]);
    }
    for (j = 1; j <= m; j++)
      for (k = 0; k < 4; k++)
        f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
  }
  printf("%lld\\n", f[n][m][3]);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define double long double
#define all(o) o.begin(), o.end()
using namespace std;
int power(int x, int y) {
  int res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1; // y = y/2
    x = (x * x) % mod;
  }
  return res % mod;
}
int n, m, k, a[3005][3005], dp[3005][3005][4];

int f(int r, int c, int cur) {
  if (r > n || c > m)
    return 0;
  int ans = 0;
  if (dp[r][c][cur] != -1)
    return dp[r][c][cur];
  if (cur == 3)

  {
    ans = max(f(r + 1, c, 0), f(r, c + 1, cur));
  } else {
    ans = max(ans, a[r][c] + f(r, c + 1, cur + 1));
    ans = max(ans, f(r, c + 1, cur));
    ans = max(ans, f(r + 1, c, 0) + a[r][c]);
  }
  return dp[r][c][cur] = ans;
}
int32_t main() {
  fast int i, j, l;
  cin >> n >> m >> k;
  memset(dp, -1, sizeof(dp));
  int x, y, val;
  for (i = 0; i < k; i++) {
    cin >> x >> y >> val;
    a[x][y] = val;
  }
  cout << f(1, 1, 0) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define double long double
#define all(o) o.begin(), o.end()
using namespace std;
int power(int x, int y) {
  int res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1; // y = y/2
    x = (x * x) % mod;
  }
  return res % mod;
}
int n, m, k, a[3005][3005], dp[3005][3005][3];

int f(int r, int c, int rem) {
  if (r > n || c > m || rem < 0)
    return 0;
  int ans = 0;

  ans = max(ans, a[r][c] + f(r, c + 1, rem - 1));
  ans = max(ans, f(r, c + 1, rem));
  if (rem)
    ans = max(ans, f(r + 1, c, 3) + a[r][c]);
  else
    ans = max(ans, f(r + 1, c, 3));
  return dp[r][c][rem] = ans;
}
int32_t main() {
  fast int i, j, l;
  cin >> n >> m >> k;
  memset(dp, -1, sizeof(dp));
  int x, y, val;
  for (i = 0; i < k; i++) {
    cin >> x >> y >> val;
    a[x][y] = val;
  }
  cout << f(1, 1, 3) << "\\n";
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

constexpr int M = 3000;
long long items[M + 2][M + 2];
long long table[M + 2][M + 2][4] = {{}};

int main() {
  int R, C, K;
  std::cin >> R >> C >> K;
  for (int i = 0; i < K; i++) {
    int x, y, value;
    std::cin >> x >> y >> value;
    items[x][y] = value;
  }
  for (int x = 0; x <= R; x++) {
    for (int y = 0; y <= C; y++) {
      for (int count = 0; count <= 3; count++) {
        table[x + 1][y][0] = std::max(table[x + 1][y][0], table[x][y][count]);
        table[x + 1][y][1] =
            std::max(table[x + 1][y][1], table[x][y][count] + items[x + 1][y]);
        table[x][y + 1][count] =
            std::max(table[x][y + 1][count], table[x][y][count]);
        if (count < 3) {
          table[x][y + 1][count + 1] = std::max(
              table[x][y + 1][count + 1], table[x][y][count] + items[x][y + 1]);
        }
      }
    }
  }

  std::cout << std::max({table[R][C][0], table[R][C][1], table[R][C][2],
                         table[R][C][3]})
            << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

constexpr int M = 6;
long long items[M + 2][M + 2];
long long table[M + 2][M + 2][4] = {{}};
/*

移動にはx方向, y方向の２つがあります
x方向移動 table[x][y][count] -> table[x+1][y][count] or
table[x+1][y][count+1]+items[x][y] y方向移動 table[x][y][count] ->
table[x][y+1][0]

*/

/*
3|4|
5|1|

2 2 4
1 1 3
2 1 4
1 2 5
2 2 0
*/

/*
3|  |
1|  |
4|  |
2|20|

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2
 */

int main() {
  int R, C, K;
  std::cin >> R >> C >> K;
  for (int i = 0; i < K; i++) {
    int x, y, value;
    std::cin >> x >> y >> value;
    items[x][y] = value;
  }

  // (0, 0) -> (x:R, y:C)
  // (1, 0) -> (x:R, y:C-1)
  // (1, 1) -> (x:R-1, y:C)
  // (2, 0) -> (x:R, y:C-2)
  // (2, 1) -> (x:R-1, y:C-1)
  // (2, 2) -> (x:R-2, y:C)
  for (int x = 0; x <= R; x++) {
    for (int y = 0; y <= C; y++) {
      for (int count = 0; count <= 3; count++) {
        table[x + 1][y][0] = std::max(table[x + 1][y][0], table[x][y][count]);
        table[x + 1][y][1] =
            std::max(table[x + 1][y][1], table[x][y][count] + items[x + 1][y]);
        table[x][y + 1][count] =
            std::max(table[x][y + 1][count], table[x][y][count]);
        if (count < 3) {
          table[x][y + 1][count + 1] = std::max(
              table[x][y + 1][count + 1], table[x][y][count] + items[x][y + 1]);
        }
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll dp[3010][3010][4];
//(i,j)まで見て、j行目のアイテムをk個持った時の最大値
ll value[3010][3010];
int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<ll> x(k), y(k), v(k);
  for (ll i = 0; i < k; i++)
    cin >> x[i] >> y[i] >> v[i];

  for (ll i = 0; i < 3010; i++) {
    for (ll j = 0; j < 3010; j++) {
      value[i][j] = 0;
    }
  }
  for (ll i = 0; i < k; i++) {
    value[x[i]][y[i]] = v[i];
  }
  for (ll i = 0; i < 3010; i++) {
    for (ll j = 0; j < 3010; j++) {
      for (ll k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (ll i = 0; i < r + 1; i++) {
    for (ll j = 0; j < c + 1; j++) {
      for (ll k = 0; k < 4; k++) {
        // 上
        if (i > 0) {
          chmax(dp[i][j][0], dp[i - 1][j][k]);
          chmax(dp[i][j][1], dp[i - 1][j][k] + value[i][j]);
        }
        // 右
        if (j > 0) {
          chmax(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + value[i][j]);
        }
      }
    }
  }

  ll res = 0;
  for (ll i = 0; i < 4; i++)
    chmax(res, dp[r][c][i]);
  cout << res << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<ll> x(k), y(k), v(k);
  for (ll i = 0; i < k; i++)
    cin >> x[i] >> y[i] >> v[i];
  ll dp[3010][3010][4];
  //(i,j)まで見て、j行目のアイテムをk個持った時の最大値
  ll value[c][r];
  for (ll i = 0; i < c; i++) {
    for (ll j = 0; j < r; j++) {
      value[i][j] = 0;
    }
  }
  for (ll i = 0; i < k; i++) {
    value[y[i] - 1][x[i] - 1] = v[i];
  }
  for (ll i = 0; i < 3010; i++) {
    for (ll j = 0; j < 3010; j++) {
      for (ll k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll k = 0; k < 4; k++) {
        // 上
        if (j > 0) {
          chmax(dp[i][j][0], dp[i][j - 1][k]);
          chmax(dp[i][j][1], dp[i][j - 1][0] + value[j][i]);
        }
        // 右
        if (i > 0) {
          chmax(dp[i][j][k], dp[i - 1][j][k]);
          if (k > 0)
            chmax(dp[i][j][k], dp[i - 1][j][k - 1] + value[j][i]);
        }
      }
    }
  }
  ll res = 0;
  for (ll i = 0; i < 4; i++)
    chmax(res, dp[r - 1][c - 1][i]);
  cout << res << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define all(v) (v).begin(), (v).end()
using namespace std;
const int N = 3e3 + 3;
ll dp[N][N][5], v[N][N];

void solve() {
  ll n, m, k, x, y, z;
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> z;
    v[x][y] = z;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
        if (k < 3)
          dp[i][j + 1][k + 1] =
              max({dp[i][j + 1][k + 1], dp[i][j][k] + v[i][j]});

        dp[i + 1][j][0] =
            max(dp[i + 1][j][0], dp[i][j][k] + v[i][j] * (k == 3 ? 0 : 1));
      }
    }
  }
  cout << max({dp[n][m][3], dp[n][m][2] + v[n][m], dp[n][m][1] + v[n][m],
               dp[n][m][0] + v[n][m]});
}

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

  int TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define all(v) (v).begin(), (v).end()
using namespace std;
const int N = 1e6 + 5;

void solve() {
  ll n, m, k, x, y, z;
  cin >> n >> m >> k;
  std::vector<std::vector<ll>> v(n + 2, std::vector<ll>(m + 2));
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> z;
    v[x][y] = z;
  }
  ll dp[n + 2][m + 2][5];
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
        if (k < 3)
          dp[i][j + 1][k + 1] =
              max({dp[i][j + 1][k + 1], dp[i][j][k] + v[i][j]});
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + v[i][j]);
      }
    }
  }
  cout << max({dp[n][m][3], dp[n][m][2], dp[n][m][1], dp[n][m][0]});
}

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

  int TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
    cout << endl;
  }
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

const int M = 3e3;
int n, m, mat[M][M];
long long memo[M][M][4];

long long solve(int i, int j, int cnt) {
  if (i == n || j == m)
    return 0;
  long long &r = memo[i][j][cnt];
  if (r == -1) {
    r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
    if (cnt < 3 && mat[i][j])
      r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
  }
  return r;
}

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

  memset(memo, -1, sizeof memo);
  int k;
  cin >> n >> m >> k;
  while (k--) {
    int i, j, x;
    cin >> i >> j >> x;
    mat[i - 1][j - 1] = x;
  }
  cout << solve(0, 0, 0) << '\\n';
}

Error: 0

Fixed submission:

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

const int M = 1e3;
int n, m, mat[M][M];
long long memo[M][M][4];

long long solve(int i, int j, int cnt) {
  if (i == n || j == m)
    return 0;
  long long &r = memo[i][j][cnt];
  if (r == -1) {
    r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
    if (cnt < 3 && mat[i][j])
      r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
  }
  return r;
}

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

  memset(memo, -1, sizeof memo);
  int k;
  cin >> n >> m >> k;
  while (k--) {
    int i, j, x;
    cin >> i >> j >> x;
    mat[i - 1][j - 1] = x;
  }
  cout << solve(0, 0, 0) << '\\n';
}