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

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

vector<vector<long long>> V(3000, vector<long long>(3000, -1));
long long S[3000][3000][4];

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

  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++)
        S[i][j][k] = -1;
  S[0][0][0] = 0;
  if (V[0][0] != -1)
    S[0][0][1] = V[0][0];

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (S[i][j][k] == -1)
          continue;

        if (i != R - 1) {
          S[i + 1][j][0] = max(S[i + 1][j][0], S[i][j][k]);
          if (V[i + 1][j] != -1)
            S[i + 1][j][1] = max(S[i + 1][j][1], S[i][j][k] + V[i + 1][j]);
        }
        if (j != C - 1) {
          S[i][j + 1][k] = max(S[i][j + 1][k], S[i][j][k]);
          if (V[i][j + 1] != -1 && k != 3)
            S[i][j + 1][k + 1] =
                max(S[i][j + 1][k + 1], S[i][j][k] + V[i][j + 1]);
        }
      }
    }
  }

  long long ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, S[R - 1][C - 1][i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

  long long S[R][C][4];
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++)
        S[i][j][k] = -1;
  S[0][0][0] = 0;
  if (V[0][0] != -1)
    S[0][0][1] = V[0][0];

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (S[i][j][k] == -1)
          continue;

        if (i != R - 1) {
          S[i + 1][j][0] = max(S[i + 1][j][0], S[i][j][k]);
          if (V[i + 1][j] != -1)
            S[i + 1][j][1] = max(S[i + 1][j][1], S[i][j][k] + V[i + 1][j]);
        }
        if (j != C - 1) {
          S[i][j + 1][k] = max(S[i][j + 1][k], S[i][j][k]);
          if (V[i][j + 1] != -1 && k != 3)
            S[i][j + 1][k + 1] =
                max(S[i][j + 1][k + 1], S[i][j][k] + V[i][j + 1]);
        }
      }
    }
  }

  long long ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, S[R - 1][C - 1][i]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

// define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define V vector
V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1};
V<int> dy = {0, 0, -1, 1, -1, 1, -1, 1};

ll dp[3030][3030][4];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int v[r][c];
  memset(v, -1, sizeof(v));
  rep(i, k) {
    int R, C;
    cin >> R >> C;
    R--;
    C--;
    cin >> v[R][C];
  }

  memset(dp, 0, sizeof(dp));
  if (v[0][0] != -1)
    dp[0][0][2] = v[0][0];
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        // 拾わない
        if (i != 0)
          chmax(dp[i][j][3], dp[i - 1][j][p]); // 上から
        if (j != 0)
          chmax(dp[i][j][p], dp[i][j - 1][p]); // 左から
        if (v[i][j] == -1)
          continue;

        // 拾う
        if (i != 0)
          chmax(dp[i][j][2], dp[i - 1][j][p] + v[i][j]); // 上から
        if (j != 0 && p != 0)
          chmax(dp[i][j][p - 1], dp[i][j - 1][p] + v[i][j]); // 左から
      }
    }
  }
  ll ans = 0;
  rep(p, 4) chmax(ans, dp[r - 1][c - 1][p]);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define V vector
V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1};
V<int> dy = {0, 0, -1, 1, -1, 1, -1, 1};

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll v[r][c];
  memset(v, -1, sizeof(v));
  rep(i, k) {
    int R, C;
    cin >> R >> C;
    R--;
    C--;
    cin >> v[R][C];
  }
  ll dp[r][c][4];
  memset(dp, 0, sizeof(dp));
  if (v[0][0] != -1)
    dp[0][0][2] = v[0][0];
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        // 拾わない
        if (i != 0)
          chmax(dp[i][j][3], dp[i - 1][j][p]); // 上から
        if (j != 0)
          chmax(dp[i][j][p], dp[i][j - 1][p]); // 左から
        if (v[i][j] == -1)
          continue;

        // 拾う
        if (i != 0)
          chmax(dp[i][j][2], dp[i - 1][j][p] + v[i][j]); // 上から
        if (j != 0 && p != 0)
          chmax(dp[i][j][p - 1], dp[i][j - 1][p] + v[i][j]); // 左から
      }
    }
  }
  ll ans = 0;
  rep(p, 4) chmax(ans, dp[r - 1][c - 1][p]);
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3001][3001][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
  if (i > R || i < 1 || j > C || j < 1)
    return 0;
  if (dp[i][j][picked] != -1)
    return dp[i][j][picked];

  dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
  dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);

  return dp[i][j][picked];
}
void solve() {
  ll K;
  cin >> R >> C >> K;
  vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  cout << rec(1, 1, 0, grid) << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3000][3000][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
  if (i > R || i < 1 || j > C || j < 1)
    return 0;
  if (dp[i][j][picked] != -1)
    return dp[i][j][picked];

  dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
  dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);

  return dp[i][j][picked];
}
void solve() {
  ll K;
  cin >> R >> C >> K;
  vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  cout << rec(1, 1, 0, grid) << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define deb(a, b, c)                                                           \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \\
       << " = " << (c) << endl;

typedef long long int ll;
typedef string str;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll arr[3004][3004], dp[5][3004][3004];
int main() {
  ll t, n, m, k, i, a, b, j, c;
  cin >> n >> m >> k;

  for (i = 0; i < n + 2; i++) {
    for (j = 0; j < m + 2; j++) {
      arr[i][j] = 0;
      for (t = 0; t < 5; t++) {
        dp[t][i][j] = 0;
      }
    }
  }
  for (i = 0; i < k; i++) {
    cin >> a >> b >> c;
    arr[a][b] = c;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      a = max(max(dp[0][i - 1][j], dp[1][i - 1][j]),
              max(dp[2][i - 1][j], dp[3][i - 1][j]));
      b = max(dp[1][i][j - 1], arr[i][j]);
      dp[0][i][j] = a;
      dp[1][i][j] = max(max(a + arr[i][j], b), dp[0][i][j - 1] + arr[i][j]);
      dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + arr[i][j]);
      dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + arr[i][j]);
    }
  }
  i = n;
  j = m;
  a = max(max(dp[0][i][j], dp[1][i][j]), max(dp[2][i][j], dp[3][i][j]));
  cout << a << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define deb(a, b, c)                                                           \\
  cout << #a << " = " << (a) << ", " << #b << " = " << (b) << ", " << #c       \\
       << " = " << (c) << endl;

typedef long long int ll;
typedef string str;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  ll t, n, m, k, i, a, b, j, c;
  cin >> n >> m >> k;
  ll arr[n + 2][m + 2], dp[4][n + 2][m + 2];
  for (i = 0; i <= n; i++) {
    for (t = 0; t < 4; t++) {
      dp[t][i][0] = 0;
    }
    for (j = 0; j <= m; j++) {
      arr[i][j] = 0;

      if (i == 0)
        for (t = 0; t < 4; t++) {
          dp[t][i][j] = 0;
        }
    }
  }
  for (i = 0; i < k; i++) {
    cin >> a >> b >> c;
    arr[a][b] = c;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      a = max(dp[1][i - 1][j], max(dp[2][i - 1][j], dp[3][i - 1][j]));
      b = max(dp[1][i][j - 1], arr[i][j]);
      dp[0][i][j] = a;
      dp[1][i][j] = max(b, a + arr[i][j]);
      dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + arr[i][j]);
      dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + arr[i][j]);
    }
  }
  j--;
  a = max(dp[1][i - 1][j], max(dp[2][i - 1][j], dp[3][i - 1][j]));
  cout << a << "\\n";
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const ll inf = 1ll << 60;

ll dp[3010][3010][5];
ll g[3010][3010];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    g[a][b] = d;
  }
  if (g[0][0]) {
    dp[0][0][1] = g[0][0];
    dp[0][0][0] = -inf;
  }
  rep(i, r) rep(j, c) rep(k, 4) {
    if (i - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
      chmax(dp[i][j][0], dp[i - 1][j][k]);
    }
    if (j - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }
  }
  ll ans = -inf;
  rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const ll inf = 1ll << 60;

ll dp[10][10][5];
ll g[10][10];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    g[a][b] = d;
  }
  if (g[0][0]) {
    dp[0][0][1] = g[0][0];
    dp[0][0][0] = -inf;
  }
  rep(i, r) rep(j, c) rep(k, 4) {
    if (i - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
      chmax(dp[i][j][0], dp[i - 1][j][k]);
    }
    if (j - 1 >= 0) {
      if (g[i][j] != 0)
        chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }
  }
  ll ans = -inf;
  rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

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

const int MAX = 3000;
ll A[MAX + 10][MAX + 10];
ll dp[MAX + 10][MAX + 10][4];

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

  rep(i, MAX + 10) { rep(j, MAX + 10) A[i][j] = 0; }

  ll x, y, z;
  rep(i, k) {
    cin >> x >> y >> z;
    A[x][y] = z;
  }

  rep(i, MAX + 10) {
    rep(j, MAX + 10) { rep(k, 4) dp[i][j][k] = 0; }
  }

  dp[1][1][1] = A[1][1];
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3 && A[i][j + 1] != 0)
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + A[i][j + 1]);
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        if (A[i + 1][j] != 0)
          chmax(dp[i + 1][j][1], dp[i][j][k] + A[i + 1][j]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) { chmax(ans, dp[r][c][i]); }
  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

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

const int MAX = 3000;

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

  ll A[MAX + 10][MAX + 10];
  rep(i, MAX + 10) { rep(j, MAX + 10) A[i][j] = 0; }

  ll x, y, z;
  rep(i, k) {
    cin >> x >> y >> z;
    A[x][y] = z;
  }

  ll dp[MAX + 10][MAX + 10][4];
  rep(i, MAX + 10) {
    rep(j, MAX + 10) { rep(k, 4) dp[i][j][k] = 0; }
  }

  dp[1][1][1] = A[1][1];
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3 && A[i][j + 1] != 0)
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + A[i][j + 1]);
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        if (A[i + 1][j] != 0)
          chmax(dp[i + 1][j][1], dp[i][j][k] + A[i + 1][j]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) { chmax(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;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

ll dp[6][3010][3010];
ll v[3010][3010];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    v[a][b] = c;
  }
  rep(i, r + 1) rep(j, c + 1) rep(m, 4) {
    // printf("dp[%d][%d][%d]=%d\\n", i, j, m, dp[i][j][m]);
    dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[m][i][j]);
    dp[m][i][j + 1] = max(dp[m][i][j + 1], dp[m][i][j]);
    if (m < 3) {
      dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[m][i][j] + v[i][j]);
      dp[m + 1][i][j + 1] = max(dp[m + 1][i][j + 1], dp[m][i][j] + v[i][j]);
    }
  }
  ll ans = 0;
  rep(m, 4) ans = max(ans, dp[m][r][c]);
  cout << ans << endl;
  // cout << 0 << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll dp[3010][3010][6];
  ll v[3010][3010];
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    v[a][b] = c;
  }
  rep(i, r + 1) rep(j, c + 1) rep(m, 4) {
    // printf("dp[%d][%d][%d]=%d\\n", i, j, m, dp[i][j][m]);
    dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][m]);
    dp[i][j + 1][m] = max(dp[i][j + 1][m], dp[i][j][m]);
    if (m < 3) {
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][m] + v[i][j]);
      dp[i][j + 1][m + 1] = max(dp[i][j + 1][m + 1], dp[i][j][m] + v[i][j]);
    }
  }
  ll ans = 0;
  rep(m, 4) ans = max(ans, dp[r][c][m]);
  cout << ans << endl;
  // cout << 0 << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

const int N = 3000 + 20;
const ll inf = 1e18;
int r, c, k;
ll val[N][N];
ll dp[N][N][4];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> r >> c >> k;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++)
      val[i][j] = -inf;
  for (int i = 0; i < k; i++) {
    int R, C, V;
    cin >> R >> C >> V;
    val[R][C] = V;
  }
  dp[1][1][0] = 0;
  dp[1][1][1] = val[1][1];
  dp[1][1][2] = dp[1][1][3] = -inf;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      if (i > 1 || j > 1)
        dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = dp[i][j][3] = -inf;
      if (i > 1) {
        dp[i][j][0] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                           dp[i - 1][j][3]});
        dp[i][j][1] = dp[i][j][0] + val[i][j];
      }
      for (int k = 0; k <= 3; k++) {
        if (j > 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (j > 1 && k)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + val[i][j]);
      }
    }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << "\\n";
}


Error: -11

Fixed submission:

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

typedef long long ll;

const int N = 2000 + 20;
int r, c, k, val[N][N];
ll dp[N][N][3];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> r >> c >> k;
  for (int i = 0; i < k; i++) {
    int R, C, V;
    cin >> R >> C >> V;
    val[R][C] = V;
  }
  dp[1][1][0] = 1;
  dp[1][1][1] = val[1][1];
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      if (i > 1) {
        dp[i][j][0] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                           dp[i - 1][j][3]});
        dp[i][j][1] = dp[i][j][0] + val[i][j];
      }
      for (int k = 0; k <= 3; k++) {
        if (j > 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (j > 1 && k)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + val[i][j]);
      }
    }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << "\\n";
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

static int R, C, K;
typedef std::pair<int, int> KEY;
typedef std::map<KEY, int> MAP;
static MAP value_map;

static std::vector<int64_t> memo;

static int64_t search(int r, int c, int k) {
  if (r > R || c > C) {
    return 0;
  }

  const size_t memo_i = (C * (r - 1) + (c - 1)) + R * C * k;
  const int64_t memo_v = memo[memo_i];
  if (memo_v >= 0) {
    return memo_v;
  }

  int64_t v = search(r + 1, c, 0);
  v = std::max(v, search(r, c + 1, k));
  if (k < 3) {
    auto itr = value_map.find(KEY(r, c));
    if (itr != value_map.end()) {
      v = std::max(v, itr->second + search(r + 1, c, 0));
      v = std::max(v, itr->second + search(r, c + 1, k + 1));
    }
  }

  memo[memo_i] = v;

  return v;
}

int main() {
  std::cin >> R >> C >> K;
  memo.resize(R * C * 4, -1);
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    std::cin >> r >> c >> v;
    value_map.insert(MAP::value_type(KEY(r, c), v));
  }
  std::cout << search(1, 1, 0) << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

static int R, C, K;
typedef std::pair<int, int> KEY;
typedef std::map<KEY, int> MAP;
static MAP value_map;

typedef std::tuple<int, int, int> MEMO_KEY;
typedef std::map<MEMO_KEY, int64_t> MEMO_MAP;
static MEMO_MAP memo_map;

static int64_t search(int r, int c, int k) {
  if (r > R || c > C) {
    return 0;
  }

  auto memo_itr = memo_map.find(MEMO_KEY(r, c, k));
  if (memo_itr != memo_map.end()) {
    return memo_itr->second;
  }

  int64_t v = search(r + 1, c, 0);
  v = std::max(v, search(r, c + 1, k));
  if (k < 3) {
    auto itr = value_map.find(KEY(r, c));
    if (itr != value_map.end()) {
      v = std::max(v, itr->second + search(r + 1, c, 0));
      v = std::max(v, itr->second + search(r, c + 1, k + 1));
    }
  }

  memo_map.insert(MEMO_MAP::value_type(MEMO_KEY(r, c, k), v));

  return v;
}

int main() {
  std::cin >> R >> C >> K;
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    std::cin >> r >> c >> v;
    value_map.insert(MAP::value_type(KEY(r, c), v));
  }
  std::cout << search(1, 1, 0) << std::endl;
  return 0;
}