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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;

int q[3000][3000][4];
signed main() {
  int r, c, k;
  cin >> r >> c >> k;
  int p[r][c];
  for (int i = 0; i < r; i++)
    fill(p[i], p[i] + c, 0ll);
  int x, y, v;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> v;
    x--;
    y--;
    p[x][y] = v;
  }

  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      q[i][j][0] = 0;
      q[i][j][1] = 0;
      q[i][j][2] = 0;
      q[i][j][3] = 0;
      if (j != 0) {
        q[i][j][0] = max(q[i][j][0], q[i][j - 1][0]);
        for (int ii = 1; ii < 4; ii++) {
          q[i][j][ii] =
              max(q[i][j][ii],
                  max(q[i][j - 1][ii], q[i][j - 1][ii - 1] + p[i][j - 1]));
        }
      }
      if (i != 0) {
        q[i][j][0] = max(q[i][j][0], q[i - 1][j][3]);
        for (int ii = 0; ii < 3; ii++) {
          q[i][j][0] = max(q[i][j][0], q[i - 1][j][ii] + p[i - 1][j]);
        }
      }
    }
  int ans = 0;
  for (int ii = 0; ii < 3; ii++)
    ans = max(ans, q[r - 1][c - 1][ii] + p[r - 1][c - 1]);
  ans = max(ans, q[r - 1][c - 1][3]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;

signed main() {
  int r, c, k;
  cin >> r >> c >> k;
  int p[r][c];
  for (int i = 0; i < r; i++)
    fill(p[i], p[i] + c, 0ll);
  int x, y, v;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> v;
    x--;
    y--;
    p[x][y] = v;
  }
  int q[r][c][4];

  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      q[i][j][0] = 0;
      q[i][j][1] = 0;
      q[i][j][2] = 0;
      q[i][j][3] = 0;
      if (j != 0) {
        q[i][j][0] = max(q[i][j][0], q[i][j - 1][0]);
        for (int ii = 1; ii < 4; ii++) {
          q[i][j][ii] =
              max(q[i][j][ii],
                  max(q[i][j - 1][ii], q[i][j - 1][ii - 1] + p[i][j - 1]));
        }
      }
      if (i != 0) {
        q[i][j][0] = max(q[i][j][0], q[i - 1][j][3]);
        for (int ii = 0; ii < 3; ii++) {
          q[i][j][0] = max(q[i][j][0], q[i - 1][j][ii] + p[i - 1][j]);
        }
      }
    }
  int ans = 0;
  for (int ii = 0; ii < 3; ii++)
    ans = max(ans, q[r - 1][c - 1][ii] + p[r - 1][c - 1]);
  ans = max(ans, q[r - 1][c - 1][3]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

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

  // dp[i][j][k] は i で k 個アイテムを拾った時のmax
  vector<vector<vector<int>>> dp(R + 1,
                                 vector<vector<int>>(C + 1, vector<int>(4, 0)));
  // for(int i=0; i<=R; i++){
  //     for(int j=0; j<=C; j++){
  //         for(int k=0; k<4; k++){
  //             dp[i][j][k] = 0;
  //         }
  //     }
  // }

  // 範囲チェック
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][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] + dist[i][j]);
        if (k != 0 && dist[i][j] != 0) {
          dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
        }
      }
    }
  }

  int ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

signed main() {
  // cout << fixed << setprecision(10) << flush;

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

  // dp[i][j][k][l] は i で k 個、j で l 個 アイテムを拾った時のmax
  int dp[R + 1][C + 1][4];
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  // 範囲チェック
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][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] + dist[i][j]);
        if (k != 0 && dist[i][j] != 0) {
          dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
        }
      }
    }
  }

  int ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;

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

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll dp[3010][3010][5];
ll val[3010][3010] = {};

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  memset(val, 0, sizeof(val));
  for (int i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    val[r][c] = v;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (val[i][j]) {
          if (k + 1 <= 3) {
            dp[i][j + 1][k + 1] =
                max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
          }
          if (k < 3) {
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
          }
        }
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[R][C - 1][i]);
    ans = max(ans, dp[R - 1][C][i]);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll dp[3010][3010][4];

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  map<P, ll> ma;
  for (int i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    ma[{r, c}] = v;
  }
  for (int i = 0; i < 3010; i++) {
    for (int j = 0; j < 3010; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (ma[{i, j}]) {
          if (k + 1 <= 3) {
            dp[i][j + 1][k + 1] =
                max(dp[i][j + 1][k + 1], dp[i][j][k] + ma[{i, j}]);
          }
          if (k < 3) {
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + ma[{i, j}]);
          }
        }
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[R][C - 1][i]);
    ans = max(ans, dp[R - 1][C][i]);
  }
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
#define float long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define endl '\\n'
#define pii pair<int, int>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(x) x.begin(), x.end()
#define fill(a, b) memset(a, b, sizeof(a))
#define sz(x) (int)x.size()
#define sp(x) setprecision(x)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define inf 10000000000000005LL
#define mex 3005
int mat[mex][mex], dp[mex][mex][4];

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

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m, k;
  cin >> n >> m >> k;
  fill(mat, 0);
  fill(dp, 0);
  for (int i = 0; i < k; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    mat[x][y] = v;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][3]);
      for (int l = 1; l <= 3; l++)
        dp[i][j][l] =
            max({dp[i][j - 1][l], dp[i - 1][j][3],
                 dp[i][j - 1][l - 1] + mat[i][j], dp[i - 1][j][3] + mat[i][j]});
    }
  }
  int ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[n][m][i]);
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
#define float long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define endl '\\n'
#define pii pair<int, int>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(x) x.begin(), x.end()
#define fill(a, b) memset(a, b, sizeof(a))
#define sz(x) (int)x.size()
#define sp(x) setprecision(x)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define inf 10000000000000005LL

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

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m, k;
  cin >> n >> m >> k;
  int mat[n + 1][m + 1], dp[n + 1][m + 1][4];
  fill(mat, 0);
  fill(dp, 0);
  for (int i = 0; i < k; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    mat[x][y] = v;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][3]);
      for (int l = 1; l <= 3; l++)
        dp[i][j][l] =
            max({dp[i][j - 1][l], dp[i - 1][j][3],
                 dp[i][j - 1][l - 1] + mat[i][j], dp[i - 1][j][3] + mat[i][j]});
    }
  }
  int ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[n][m][i]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int dx[4] = {0, -1};
const int dy[4] = {-1, 0};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int64_t>> value(h, vector<int64_t>(w, 0));
  for (auto i = 0; i < k; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    value[r][c] = v;
  }
  vector<vector<vector<int64_t>>> dp(
      h, vector<vector<int64_t>>(w, vector<int64_t>(4, 0)));
  dp[0][0][1] = value[0][0];
  for (auto i = 0; i < h; i++) {
    for (auto j = 0; j < w; j++) {
      for (auto l = 0; l < 2; l++) {
        int px = i + dx[l], py = j + dy[l];
        if (px < 0 or py < 0)
          continue;
        for (auto m = 0; m < 4; m++) {
          if (l == 1) {
            // 拾わない場合
            dp[i][j][0] = max(dp[px][py][m], dp[i][j][0]);
            // 拾う場合
            dp[i][j][1] = max(dp[px][py][m] + value[i][j], dp[i][j][1]);
          } else {
            // 拾わない場合
            dp[i][j][m] = max(dp[px][py][m], dp[i][j][m]);
            // 拾う場合
            if (m != 3)
              dp[i][j][m + 1] =
                  max(dp[px][py][m] + value[i][j], dp[i][j][m + 1]);
          }
        }
      }
    }
  }

  int64_t ans = 0;
  for (auto l = 0; l < 4; l++)
    ans = max(ans, dp[h - 1][w - 1][l]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  map<pair<int, int>, int64_t> value;
  for (auto i = 0; i < k; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    value[{r, c}] = v;
  }
  vector<vector<vector<int64_t>>> dp(
      h, vector<vector<int64_t>>(w, vector<int64_t>(4, 0)));
  dp[0][0][1] = value[{0, 0}];
  for (auto i = 0; i < h; i++) {
    for (auto j = 0; j < w; j++) {
      // 縦移動
      int px = i - 1, py = j;
      for (auto l = 0; l < 4; l++) {
        if (px < 0)
          break;
        // 拾わない場合
        dp[i][j][0] = max(dp[px][py][l], dp[i][j][0]);
        // 拾う場合
        dp[i][j][1] = max(dp[px][py][l] + value[{i, j}], dp[i][j][1]);
      }
      // 横移動
      px = i, py = j - 1;
      for (auto l = 0; l < 4; l++) {
        if (py < 0)
          break;
        // 拾わない場合
        dp[i][j][l] = max(dp[px][py][l], dp[i][j][l]);
        // 拾う場合
        if (l != 3)
          dp[i][j][l + 1] = max(dp[px][py][l] + value[{i, j}], dp[i][j][l + 1]);
      }
    }
  }

  int64_t ans = 0;
  for (auto l = 0; l < 4; l++)
    ans = max(ans, dp[h - 1][w - 1][l]);
  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 (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<int>> vv;
typedef pair<int, int> P;

// input
int R, C, K;
vector<int> r, c, v;

ll dp[3001][3001][4];
ll INF = 1e16;

void input() {
  cin >> R >> C >> K;
  r = c = v = vector<int>(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
}

int main() {
  input();
  map<P, int> m;
  rep(i, K) m[make_pair(r[i], c[i])] = v[i];
  rep(i, R + 1) rep(j, C + 1) rep(k, 4) dp[i][j][k] = -INF;
  dp[0][1][0] = dp[1][0][0] = 0;
  rep(i, R) rep(j, C) rep(k, 4) {
    ll x = dp[i + 1][j][k];
    if (k == 0) {
      rep(l, 4) x = max(x, dp[i][j + 1][l]);
    }
    P p(i + 1, j + 1);
    if (k > 0 && m.count(p)) {
      int v = m[p];
      x = max(x, dp[i + 1][j][k - 1] + v);
      if (k == 1) {
        rep(l, 4) x = max(x, dp[i][j + 1][l] + v);
      }
    }
    dp[i + 1][j + 1][k] = x;
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[R][C][k]);
  cout << ans << endl;
}


Error: -11

Fixed submission:

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

// input
int R, C, K;
vector<int> r, c, v;

ll INF = 1e16;

void input() {
  cin >> R >> C >> K;
  r = c = v = vector<int>(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
}

int main() {
  input();
  map<P, int> m;
  rep(i, K) m[make_pair(r[i], c[i])] = v[i];
  ll dp[3001][3001][4];
  rep(i, R + 1) rep(j, C + 1) rep(k, 4) dp[i][j][k] = -INF;
  dp[0][1][0] = dp[1][0][0] = 0;
  rep(i, R) rep(j, C) rep(k, 4) {
    ll x = dp[i + 1][j][k];
    if (k == 0) {
      rep(l, 4) x = max(x, dp[i][j + 1][l]);
    }
    P p(i + 1, j + 1);
    if (k > 0 && m.count(p)) {
      int v = m[p];
      x = max(x, dp[i + 1][j][k - 1] + v);
      if (k == 1) {
        rep(l, 4) x = max(x, dp[i][j + 1][l] + v);
      }
    }
    dp[i + 1][j + 1][k] = x;
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[R][C][k]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define endl '\\n'
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int nax = 3007;
ll v[nax][nax];
ll dp[nax][nax][4];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      v[i][j] = 0;
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
      dp[i][j][2] = 0;
      dp[i][j][3] = 0;
    }
  }
  for (ll i = 0; i < k; i++) {
    ll x, y, val;
    cin >> x >> y >> val;
    --x, --y;
    v[x][y] = val;
  }
  dp[0][0][0] = 0, dp[0][0][1] = v[0][0];
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll x = 0; x < 4; x++) {
        if (i + 1 < r) {
          dp[i + 1][j][0] = max(dp[i][j][x], dp[i + 1][j][0]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][x] + v[i + 1][j]);
        }
        if (j + 1 < c) {
          dp[i][j + 1][x] = max(dp[i][j][x], dp[i][j + 1][x]);
          if (x + 1 <= 3)
            dp[i][j + 1][x + 1] =
                max(dp[i][j + 1][x + 1], dp[i][j][x] + v[i][j + 1]);
        }
      }
    }
  }
  cout << max({dp[r - 1][c - 1][0], dp[r - 1][c - 1][1], dp[r - 1][c - 1][2],
               dp[r - 1][c - 1][3]})
       << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define endl '\\n'
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;
  ll v[r][c];
  ll dp[r][c][4];
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      v[i][j] = 0;
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
      dp[i][j][2] = 0;
      dp[i][j][3] = 0;
    }
  }
  for (ll i = 0; i < k; i++) {
    ll x, y, val;
    cin >> x >> y >> val;
    --x, --y;
    v[x][y] = val;
  }
  dp[0][0][0] = 0, dp[0][0][1] = v[0][0];
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll x = 0; x < 4; x++) {
        if (i + 1 < r) {
          dp[i + 1][j][0] = max(dp[i][j][x], dp[i + 1][j][0]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][x] + v[i + 1][j]);
        }
        if (j + 1 < c) {
          dp[i][j + 1][x] = max(dp[i][j][x], dp[i][j + 1][x]);
          if (x + 1 <= 3)
            dp[i][j + 1][x + 1] =
                max(dp[i][j + 1][x + 1], dp[i][j][x] + v[i][j + 1]);
        }
      }
    }
  }
  cout << max({dp[r - 1][c - 1][0], dp[r - 1][c - 1][1], dp[r - 1][c - 1][2],
               dp[r - 1][c - 1][3]})
       << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  unordered_map<ll, ll> mp;
  for (int i = 0; i < k; i++) {
    int tr, tc, tv;
    cin >> tr >> tc >> tv;
    mp[c * tr + tc] = tv;
  }

  vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      dp[i % 2][j][0] = max({dp[(i + 1) % 2][j][0], dp[(i + 1) % 2][j][1],
                             dp[(i + 1) % 2][j][2], dp[(i + 1) % 2][j][3],
                             dp[i % 2][j - 1][0]});
      ll val = mp[i * c + j];
      if (val == 0) {
        dp[i % 2][j][1] = dp[i % 2][j - 1][1];
        dp[i % 2][j][2] = dp[i % 2][j - 1][2];
        dp[i % 2][j][3] = dp[i % 2][j - 1][3];
      } else {
        dp[i % 2][j][1] =
            max({dp[(i + 1) % 2][j][0] + val, dp[(i + 1) % 2][j][1] + val,
                 dp[(i + 1) % 2][j][2] + val, dp[(i + 1) % 2][j][3] + val,
                 dp[i % 2][j - 1][1], dp[i % 2][j - 1][0] + val});
        dp[i % 2][j][2] = max(dp[i % 2][j - 1][2], dp[i % 2][j - 1][1] + val);
        dp[i % 2][j][3] = max(dp[i % 2][j - 1][3], dp[i % 2][j - 1][2] + val);
      }
    }
  }
  ll ans =
      max({dp[r % 2][c][0], dp[r % 2][c][1], dp[r % 2][c][2], dp[r % 2][c][3]});
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  unordered_map<int, int> mp;
  for (int i = 0; i < k; i++) {
    int tr, tc, tv;
    cin >> tr >> tc >> tv;
    mp[c * tr + tc] = tv;
  }

  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      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][0]});
      ll val = mp[i * c + j];
      if (val == 0) {
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
        dp[i][j][3] = dp[i][j - 1][3];
      } else {
        dp[i][j][1] = max({dp[i - 1][j][0] + val, dp[i - 1][j][1] + val,
                           dp[i - 1][j][2] + val, dp[i - 1][j][3] + val,
                           dp[i][j - 1][1], dp[i][j - 1][0] + val});
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + val);
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + val);
      }
    }
  }
  ll ans = max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]});
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#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>
using namespace std;
typedef long long ll;
#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;
  for (int i = 0; i < n; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r - 1][c - 1] = v;
  }

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

      for (int k = 0; k < 4; k++) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }

  ll ans = 0;
  for (int k = 0; k < 4; k++)
    chmax(ans, dp[R--][C--][k]);
  cout << ans << endl;
  return 0;
}