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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)

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

int main() {
  int R, C, n;
  cin >> R >> C >> n;

  rep(i, n) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r - 1][c - 1] = v;
  }
  rep(i, R) rep(j, C) {
    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 _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x, 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
using vd = vector<long double>;
#define co(x) cout << x << endl;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
#define couts(v)                                                               \\
  rep(i, v.size()) { cout << v.at(i) << endl; }
// aよりもbが大きいならばaをbで更新する
#define chmax(x, y) x = max(x, y)

// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
/*
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
*/
int main() {
  int R, C, n;
  cin >> R >> C >> n;
  int v[3005][3005];
  ll d[3005][3005][4];
  rep(i, n) {
    int r, c, va;
    cin >> r >> c >> va;
    v[r - 1][c - 1] = va;
  }
  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      chmax(d[i][j][k + 1], d[i][j][k] + v[i][j]);
    }
    rep(k, 4) {
      chmax(d[i + 1][j][0], d[i][j][k]);
      chmax(d[i][j + 1][k], d[i][j][k]);
    }
  }
  ll ans = 0;
  rep(k, 4) { chmax(ans, d[R - 1][C - 1][k]); }
  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)

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

int main() {
  int R, C, n;
  cin >> R >> C >> n;
  rep(i, n) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r - 1][c - 1] = v;
  }

  rep(i, R) rep(j, C) {
    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 _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x, 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
using vd = vector<long double>;
#define co(x) cout << x << endl;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
#define couts(v)                                                               \\
  rep(i, v.size()) { cout << v.at(i) << endl; }
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
/*
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
*/
int main() {
  int R, C, r, c, n;
  cin >> R >> C >> n;
  int v[3005][3005];
  ll d[3005][3005][4];
  rep(i, n) {
    cin >> r >> c;
    cin >> v[r - 1][c - 1];
  }
  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      chmax(d[i][j][k + 1], d[i][j][k] + v[i][j]);
    }
    rep(k, 4) {
      chmax(d[i + 1][j][0], d[i][j][k]);
      chmax(d[i][j + 1][k], d[i][j][k]);
    }
  }
  ll ans = 0;
  rep(k, 4) { chmax(ans, d[R - 1][C - 1][k]); }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FasterIO                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef unsigned long long ull;
typedef long long ll;
typedef double db;

#define mem(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pi pair<int, int>
#define vi vector<int>
#define pL pair<ll, ll>
#define vL vector<ll>
#define vp vector<pi>
#define vpL vector<pL>

const int MX = 4000;
const ll mod = 998244353;
const int LG = 26;

ll a[MX][MX], dp[MX][MX][4];

ll call(int r, int c, int t) {
  if (r <= 0 || c <= 0)
    return -1e18;

  if (r == 1 && c == 1)
    return ((t < 3) ? max(a[r][c], 0LL) : 0);

  if (~dp[r][c][t])
    return dp[r][c][t];

  dp[r][c][t] = 0;

  if (t < 3)
    dp[r][c][t] = max(dp[r][c][t], a[r][c] + call(r - 1, c, 0));

  dp[r][c][t] = max(dp[r][c][t], call(r - 1, c, 0));

  if (t < 3)
    dp[r][c][t] = max(dp[r][c][t], a[r][c] + call(r, c - 1, t + 1));

  dp[r][c][t] = max(dp[r][c][t], call(r, c - 1, t));

  return dp[r][c][t];
}

int main() {
  FasterIO;

  int r, c, k;

  cin >> r >> c >> k;

  for (int i = 1; i <= k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x][y] += z;
  }

  mem(dp, -1);

  ll rs = call(r, c, 0);

  cout << rs << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FasterIO                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef unsigned long long ull;
typedef long long ll;
typedef double db;

#define mem(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pi pair<int, int>
#define vi vector<int>
#define pL pair<ll, ll>
#define vL vector<ll>
#define vp vector<pi>
#define vpL vector<pL>

const int MX = 3000;
const ll mod = 998244353;
const int LG = 26;

ll a[MX][MX], dp[MX][MX][3];

ll call(int r, int c, int t) {
  if (t == 3)
    return -1e18;
  if (r <= 0 || c <= 0)
    return -1e18;

  if (r == 1 && c == 1)
    return a[r][c];

  if (~dp[r][c][t])
    return dp[r][c][t];

  dp[r][c][t] = max(a[r][c] + call(r - 1, c, 0), call(r - 1, c, 0));
  dp[r][c][t] =
      max(dp[r][c][t], max(a[r][c] + call(r, c - 1, t + 1), call(r, c - 1, t)));

  return dp[r][c][t];
}

int main() {
  FasterIO;

  int r, c, k;

  cin >> r >> c >> k;

  for (int i = 1; i <= k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x][y] = z;
  }

  mem(dp, -1);

  ll rs = call(r, c, 0);

  cout << rs << endl;

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define MAX 3004

ll vls[MAX][MAX];
ll dp[MAX][MAX][4];
int n, m, k;

ll solve(int x, int y, int usd) {
  if (dp[x][y][usd] != -1)
    return dp[x][y][usd];
  dp[x][y][usd] = usd < 3 ? vls[x][y] : 0;
  if (y + 1 < m && usd < 3)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x, y + 1, usd + 1) + vls[x][y]);
  if (y + 1 < m)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x, y + 1, usd));
  if (x + 1 < n)
    dp[x][y][usd] =
        max(dp[x][y][usd], solve(x + 1, y, 0) + (usd < 3 ? vls[x][y] : 0));
  return dp[x][y][usd];
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  ll x, y, vl;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> vl;
    vls[x - 1][y - 1] = vl;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

vector<vector<ll>> vls;
vector<vector<vector<ll>>> dp;
int n, m, k;

ll solve(int x, int y, int usd) {
  if (dp[x][y][usd] != -1)
    return dp[x][y][usd];
  dp[x][y][usd] = usd < 3 ? vls[x][y] : 0;
  if (y + 1 < m && usd < 3)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x, y + 1, usd + 1) + vls[x][y]);
  if (y + 1 < m)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x, y + 1, usd));
  if (x + 1 < n && usd < 3)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x + 1, y, 0) + vls[x][y]);
  if (x + 1 < n)
    dp[x][y][usd] = max(dp[x][y][usd], solve(x + 1, y, 0));
  return dp[x][y][usd];
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  vls.assign(n, vector<ll>(m, 0));
  dp.assign(n, vector<vector<ll>>(m, vector<ll>(4, -1)));
  ll x, y, vl;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> vl;
    vls[x - 1][y - 1] = vl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  int goods[R][C];
  memset(goods, 0, sizeof(goods));
  // map<int, map<int, ll>> goods;

  int r, c, v;
  for (int i = 0; i < K; ++i) {
    cin >> r >> c >> v;

    goods[r - 1][c - 1] = v;
  }

  ll dp[2][C][4];
  memset(dp, 0, sizeof(dp));

  for (int y = 0; y < R; ++y) {
    int cy = y % 2;
    int by = (y + 1) % 2;

    for (int x = 0; x < C; ++x) {
      ll mv = LLONG_MIN;

      for (int c = 0; c <= 3; ++c) {
        mv = max(mv, dp[by][x][c]);
      }

      dp[cy][x][0] = mv;

      if (goods[y][x] > 0) {
        dp[cy][x][1] = mv + goods[y][x];
      }
    }

    for (int x = 1; x < C; ++x) {
      for (int c = 0; c <= 3; ++c) {
        dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);

        if (c < 3 && goods[y][x] > 0) {
          dp[cy][x][c + 1] =
              max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
        }
      }
    }
  }

  ll ans = LLONG_MIN;

  for (int c = 0; c <= 3; ++c) {
    ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  map<int, map<int, ll>> goods;

  int r, c, v;
  for (int i = 0; i < K; ++i) {
    cin >> r >> c >> v;

    goods[r - 1][c - 1] = v;
  }

  ll dp[2][C][4];
  memset(dp, 0, sizeof(dp));

  for (int y = 0; y < R; ++y) {
    int cy = y % 2;
    int by = (y + 1) % 2;

    for (int x = 0; x < C; ++x) {
      ll mv = LLONG_MIN;

      for (int c = 0; c <= 3; ++c) {
        mv = max(mv, dp[by][x][c]);
      }

      dp[cy][x][0] = mv;

      if (goods[y][x] > 0) {
        dp[cy][x][1] = mv + goods[y][x];
      }
    }

    for (int x = 1; x < C; ++x) {
      for (int c = 0; c <= 3; ++c) {
        dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);

        if (c < 3 && goods[y][x] > 0) {
          dp[cy][x][c + 1] =
              max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
        }
      }
    }
  }

  ll ans = LLONG_MIN;

  for (int c = 0; c <= 3; ++c) {
    ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
  }

  cout << ans << endl;

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

/**
 *	@file: B.cpp
 *	@author: Huy Le
 *	@created: 08-17-2020 10:29:18
 **/

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

#define ll long long
#define ld long double
#define sz(x) (int)(x).size()
const char newl = '\\n';

int ts;
int r, c, k;
const int mxN = 3005;
ll a[mxN][mxN];
ll dp[mxN][mxN][4];

void solve() {
  cin >> r >> c >> k;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    cin >> a[x][y];
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][1] = a[0][0];
  for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j) {
      if (i > 0) {
        for (int k = 0; k <= 3; ++k) {
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
        }
      }
      if (j > 0) {
        for (int k = 0; k <= 3; ++k) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        }
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; ++i) {
    ans = max(ans, dp[r - 1][c - 1][i]);
  }
  cout << ans << newl;
}

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

  // cin >> ts;
  // while(ts--)
  solve();
}


Error: 0

Fixed submission:

/**
 *	@file: B.cpp
 *	@author: Huy Le
 *	@created: 08-17-2020 10:29:18
 **/

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

#define ll long long
#define ld long double
#define sz(x) (int)(x).size()
const char newl = '\\n';

int ts;
int r, c, k;
const int mxN = 3005;
ll a[mxN][mxN];

void solve() {
  cin >> r >> c >> k;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    cin >> a[x][y];
  }
  ll dp[r + 1][c + 1][4];
  memset(dp, 0, sizeof(dp));
  dp[0][0][1] = a[0][0];
  for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j) {
      if (i > 0) {
        for (int k = 0; k <= 3; ++k) {
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
        }
      }
      if (j > 0) {
        for (int k = 0; k <= 3; ++k) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        }
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; ++i) {
    ans = max(ans, dp[r - 1][c - 1][i]);
  }
  cout << ans << newl;
}

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

  // cin >> ts;
  // while(ts--)
  solve();
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxN = 3009, INF = -10;
long long int dp[maxN][maxN][4], c[maxN][maxN];
int n, m;
long long int f(int i, int j, int d) {
  if (i <= 0 || j <= 0 || i > n || j > m || d < 0)
    return INF;
  if (i == n && j == m)
    return d ? c[i][j] : 0;
  if (dp[i][j][d] != -1)
    return dp[i][j][d];
  long long int ret;
  if (d > 0)
    ret =
        max(c[i][j] + max(f(i + 1, j, 3), f(i, j + 1, d - 1)), f(i, j + 1, d));
  else
    ret = max(f(i + 1, j, 3), f(i, j + 1, d));
  return dp[i][j][d] = ret;
}
int main() {
  int k;
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int ni, mi, vi;
    cin >> ni >> mi >> vi;
    c[ni][mi] = vi;
  }
  memset(dp, -1, sizeof(dp));
  cout << f(1, 1, 3) << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxN = 3000;
const long long int INF = -(1e16);
long long int dp[maxN][maxN][4], c[maxN][maxN];
int n, m;
long long int f(int i, int j, int d) {
  if (i <= 0 || j <= 0 || i > n || j > m)
    return INF;
  if (i == n && j == m)
    return c[i][j];
  if (dp[i][j][d] != -1)
    return dp[i][j][d];
  long long int ret;
  if (d)
    ret = max({c[i][j] + f(i + 1, j, 3), f(i, j + 1, d - 1) + c[i][j],
               f(i, j + 1, d)});
  else
    ret = max(f(i + 1, j, 3), f(i, j + 1, d));
  return dp[i][j][d] = ret;
}
int main() {
  int k;
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int ni, mi, vi;
    cin >> ni >> mi >> vi;
    c[ni][mi] = vi;
  }
  memset(dp, -1, sizeof(dp));
  cout << f(1, 1, 3) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int n, m, ki;
ll a[3010][3010];
ll d[3010][3010][4];
int main() {
  scanf("%d%d%d", &n, &m, &ki);
  for (int i = 0; i < ki; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    x--, y--;
    a[x][y] = z;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k <= 3; k++)
        d[i][j][k] = -1e18;
  d[0][0][0] = 0, d[0][0][1] = a[0][0];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        // cout <<i<<" "<<j<<" "<<k<<" ##\\n";
        if (k > 3)
          break;
        if (d[i][j][k] == -1e18)
          continue;
        if (j != m - 1) {
          if (k != 3)
            d[i][j + 1][k + 1] =
                max(d[i][j + 1][k + 1], d[i][j][k] + a[i][j + 1]);
          d[i][j + 1][k] = max(d[i][j + 1][k], d[i][j][k]);
        }
        if (i != n - 1) {
          d[i + 1][j][1] = max(d[i + 1][j][1], d[i][j][k] + a[i + 1][j]);
          d[i + 1][j][0] = max(d[i + 1][j][0], d[i][j][k]);
        }
      }
    }
  }
  ll r = 0;
  for (int i = 0; i <= 3; i++)
    r = max(r, d[n - 1][m - 1][i]);
  printf("%lld", r);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int n, m, k;
ll a[3010][3010];
ll d[3010][3010][3];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < k; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    x--, y--;
    a[x][y] = z;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k <= 3; k++)
        d[i][j][k] = -1e18;
  d[0][0][0] = 0, d[0][0][1] = a[0][0];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (d[i][j][k] == -1e18)
          continue;
        if (j != m - 1 && k != 3) {
          d[i][j + 1][k + 1] =
              max(d[i][j + 1][k + 1], d[i][j][k] + a[i][j + 1]);
          d[i][j + 1][k] = max(d[i][j + 1][k], d[i][j][k]);
        }
        if (i != n - 1) {
          d[i + 1][j][1] = max(d[i + 1][j][1], d[i][j][k] + a[i + 1][j]);
          d[i + 1][j][0] = max(d[i + 1][j][0], d[i][j][k]);
        }
      }
    }
  }
  ll r = 0;
  for (int i = 0; i <= 3; i++)
    r = max(r, d[n - 1][m - 1][i]);
  printf("%lld", r);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define MAXN 3001
ll arr[MAXN][MAXN];
ll dp[MAXN][MAXN][4];

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

  ll N, M, K;
  cin >> N >> M >> K;
  for (ll k = 0; k < K; ++k) {
    ll i, j, v;
    cin >> i >> j >> v;
    i--;
    j--;
    arr[i][j] = v;
  }

  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (i == 0 && j == 0) // base case
      {
        dp[0][0][0] = 0;
        dp[0][0][1] = arr[0][0];
        dp[0][0][2] = -1;
        dp[0][0][3] = -1;
        continue;
      }

      for (ll k = 0; k <= 3; ++k) {
        dp[i][j][k] = -1;

        if (j > 0) {
          dp[i][j][k] = dp[i][j - 1][k];
          if ((k > 0) && (dp[i][j - 1][k - 1] != -1))
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + arr[i][j]);
        }
      }

      ll mx = -1;
      for (ll k = 0; k <= 3; ++k) {
        if (i > 0)
          mx = max(mx, dp[i - 1][j][k]);
      }

      dp[i][j][0] = max(dp[i][j][0], mx);

      if (mx != -1)
        dp[i][j][1] = max(dp[i][j][1], mx + arr[i][j]);
    }
  }

  ll ans = 0;
  for (ll k = 0; k <= 3; ++k)
    ans = max(ans, dp[N - 1][M - 1][k]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define MAXN 3000
ll arr[MAXN][MAXN];

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  ll dp[N][M][4];
  for (ll k = 0; k < K; ++k) {
    ll i, j, v;
    cin >> i >> j >> v;
    i--;
    j--;
    arr[i][j] = v;
  }

  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (i == 0 && j == 0) // base case
      {
        dp[0][0][0] = 0;
        dp[0][0][1] = arr[0][0];
        dp[0][0][2] = -1;
        dp[0][0][3] = -1;
        continue;
      }

      for (ll k = 0; k <= 3; ++k) {
        dp[i][j][k] = -1;

        if (j > 0) {
          dp[i][j][k] = dp[i][j - 1][k];
          if ((k > 0) && (dp[i][j - 1][k - 1] != -1))
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + arr[i][j]);
        }
      }

      ll mx = -1;
      for (ll k = 0; k <= 3; ++k) {
        if (i > 0)
          mx = max(mx, dp[i - 1][j][k]);
      }

      dp[i][j][0] = max(dp[i][j][0], mx);

      if (mx != -1)
        dp[i][j][1] = max(dp[i][j][1], mx + arr[i][j]);
    }
  }

  ll ans = 0;
  for (ll k = 0; k <= 3; ++k)
    ans = max(ans, dp[N - 1][M - 1][k]);
  cout << ans << endl;
}