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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll MAX_N = 3005;
ll dp[MAX_N][MAX_N][4];
ll items[MAX_N][MAX_N];

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

  ll r, c, v;

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

  ll value;
  ll h;
  ll b;

  for (ll row = 1; row <= R; ++row) {
    for (ll col = 1; col <= C; ++col) {
      value = items[row][col];
      b = 0;
      for (ll i = 0; i < 4; ++i) {
        b = max(b, dp[row - 1][col][i]);
      }
      b = max(b, dp[row][col - 1][0]);
      dp[row][col][0] = b;
      dp[row][col][1] = max(dp[row][col - 1][1], b + value);
      for (ll i = 2; i < 4; ++i) {
        dp[row][col][i] =
            max(dp[row][col - 1][i], dp[row][col - 1][i - 1] + value);
      }
    }
  }

  ll ans = 0;

  for (ll i = 0; i < 4; ++i) {
    ans = max(ans, dp[R][C][i]);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

  map<ll, ll> items;
  ll r, c, v;

  for (ll i = 0; i < K; ++i) {
    cin >> r >> c >> v;
    ll h = r * 10007 + c;
    items[h] = v;
  }

  ll dp[R + 1][C + 1][4];

  for (ll row = 0; row <= R; ++row) {
    for (ll col = 0; col <= C; ++col) {
      for (ll i = 0; i < 4; ++i) {
        dp[row][col][i] = 0;
      }
    }
  }

  ll value;
  ll h;
  ll b;

  for (ll row = 1; row <= R; ++row) {
    for (ll col = 1; col <= C; ++col) {
      h = row * 10007 + col;
      value = items[h];
      b = 0;
      for (ll i = 0; i < 4; ++i) {
        b = max(b, dp[row - 1][col][i]);
      }
      b = max(b, dp[row][col - 1][0]);
      dp[row][col][0] = b;
      dp[row][col][1] = max(dp[row][col - 1][1], b + value);
      for (ll i = 2; i < 4; ++i) {
        dp[row][col][i] =
            max(dp[row][col - 1][i], dp[row][col - 1][i - 1] + value);
      }
    }
  }

  ll ans = 0;

  for (ll i = 0; i < 4; ++i) {
    ans = max(b, dp[R][C][i]);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define db(a) cout << fixed << #a << " = " << a << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 3000 + 5;
ll dp[MAXN][MAXN][4];
ll W[MAXN][MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll R, C, K;
  cin >> R >> C >> K;
  // vector<vector<ll>> W(R, vector<ll> (C, 0));
  for (int i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    W[r][c] = v;
  }
  // ll dp[R][C][4];
  dp[0][0][0] = 0;
  dp[0][0][1] = W[0][0];
  dp[0][0][2] = 0;
  dp[0][0][3] = 0;
  for (int i = 1; i < R; i++) {
    dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]);
    dp[i][0][1] = max(dp[i - 1][0][0], dp[i - 1][0][1]) + W[i][0];
    dp[i][0][2] = 0;
    dp[i][0][3] = 0;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 1; j < C; j++) {
      ll max_above;
      if (i == 0)
        max_above = 0;
      else
        max_above = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                        max(dp[i - 1][j][2], dp[i - 1][j][3]));
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + W[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + W[i][j]);
      dp[i][j][1] = max(dp[i][j - 1][1],
                        max(dp[i][j - 1][0] + W[i][j], W[i][j] + max_above));
      dp[i][j][0] = max(dp[i][j - 1][0], max_above);
    }
  }
  cout << max(max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]),
              max(dp[R - 1][C - 1][2], dp[R - 1][C - 1][3]))
       << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define db(a) cout << fixed << #a << " = " << a << endl;

using namespace std;

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> W(R, vector<ll>(C, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    W[r][c] = v;
  }
  ll dp[R][C][4];
  dp[0][0][0] = 0;
  dp[0][0][1] = W[0][0];
  dp[0][0][2] = 0;
  dp[0][0][3] = 0;
  for (int i = 1; i < R; i++) {
    dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]);
    dp[i][0][1] = max(dp[i - 1][0][0], dp[i - 1][0][1]) + W[i][0];
    dp[i][0][2] = 0;
    dp[i][0][3] = 0;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 1; j < C; j++) {
      ll max_above;
      if (i == 0)
        max_above = 0;
      else
        max_above = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                        max(dp[i - 1][j][2], dp[i - 1][j][3]));
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + W[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + W[i][j]);
      dp[i][j][1] = max(dp[i][j - 1][1],
                        max(dp[i][j - 1][0] + W[i][j], W[i][j] + max_above));
      dp[i][j][0] = max(dp[i][j - 1][0], max_above);
    }
  }
  cout << max(max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]),
              max(dp[R - 1][C - 1][2], dp[R - 1][C - 1][3]))
       << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int MAXN = 3e3 + 5;

ll ans;
int r, c, k;
int val[MAXN][MAXN];
ll dp[MAXN][MAXN][4];
int x, y, v;

int main() {
  // freopen("1.in", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> r >> c >> k;
  for (int i = 1; i <= k; i++)
    cin >> x >> y >> v, val[x][y] = v;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      dp[i][j][0] = dp[i][j - 1][0];
      for (int k = 0; k <= 3; k++)
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
      for (int p = 1; p <= 3; p++) {
        dp[i][j][p] = dp[i][j - 1][p];
        if (val[i][j]) {
          dp[i][j][p] = max(dp[i][j][p], dp[i][j - 1][p - 1] + val[i][j]);
          if (p == 1)
            for (int k = 0; k <= 3; k++)
              dp[i][j][p] = max(dp[i][j][p], dp[i - 1][j][k] + val[i][j]);
        }
      }
    }
  for (int k = 0; k <= 3; k++)
    ans = max(ans, dp[r][c][k]);
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int MAXN = 1e3 + 5;
ll ans;
int r, c, k;
ll val[MAXN][MAXN];
int x, y, v;

void dfs(int row, int col, int cnt, ll sum) {
  if (row > r || col > c)
    return;
  if (row == r && col == c) {
    ans = max(ans, sum);
    return;
  }
  if (cnt < 3)
    dfs(row, col + 1, cnt + 1, sum + val[row][col + 1]);
  dfs(row, col + 1, cnt, sum);
  dfs(row + 1, col, 0, sum), dfs(row + 1, col, 1, sum + val[row + 1][col]);
}

int main() {
  // freopen("1.in", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> r >> c >> k;
  for (int i = 1; i <= k; i++)
    cin >> x >> y >> v, val[x][y] = v;
  if (val[1][1])
    dfs(1, 1, 1, val[1][1]);
  dfs(1, 1, 0, 0);
  cout << ans;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

// Hail god Yato

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

#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

ll check(int x, int y, int type) {
  if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
    return LONG_LONG_MIN;

  ll &ans = dp[x][y][type];
  if (ans != -1)
    return ans;
  if (x == 1 && y == 1) {
    if (type != 0)
      return g[1][1];
    // else if(type == 0)
    return 0;
  }

  ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
                  check(x - 1, y, 0)}) +
                 ((type > 0) ? g[x][y] : 0),
             check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
  return ans;
}
void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
       << "\\n";
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

// Hail god Yato

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

#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

ll check(int x, int y, int type) {
  if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
    return LONG_LONG_MIN;

  ll &ans = dp[x][y][type];
  if (ans != -1)
    return ans;
  if (x == 1 && y == 1) {
    if (type != 0)
      return g[1][1];
    // else if(type == 0)
    return 0;
  }

  ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
                  check(x - 1, y, 0)}) +
                 ((type > 0) ? g[x][y] : 0),
             check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
  return ans;
}
void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
       << "\\n";
}
int main() {
  hs;
  ll t;
  t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

ll dp[3002][3002][4];

ll mp[3002][3002];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int r, c, kk;
  cin >> r >> c >> kk;
  while (kk--) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    mp[x][y] = z;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + mp[i][j]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + mp[i][j]);
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        res = max(res, dp[i][j][k]);
      }
    }
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

ll dp[3002][3002][4];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int r, c, kk;
  cin >> r >> c >> kk;
  map<pair<int, int>, int> mp;
  while (kk--) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    mp[make_pair(x, y)] = z;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3) {
          dp[i + 1][j][0] =
              max(dp[i + 1][j][0], dp[i][j][k] + mp[make_pair(i, j)]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + mp[make_pair(i, j)]);
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        res = max(res, dp[i][j][k]);
      }
    }
  }
  cout << res << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C, v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  vector<vector<vector<ll>>> dp(
      4, vector<vector<ll>>(r + 1, vector<ll>(c + 1, 0)));
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
      }
      dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
      dp[1][i][j] = dp[1][i][j - 1];
      dp[2][i][j] = dp[2][i][j - 1];
      dp[3][i][j] = dp[3][i][j - 1];
      if (memo[i][j] != 0) {
        dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
        dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[i][r][c]);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C;
    ll v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  ll dp[3001][3001][4];
  fill(dp[0][0], dp[3001][0], (ll)0);
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C;
    ll v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][l] = 0;
      }
    }
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

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

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C, v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  vector<vector<vector<ll>>> dp(
      4, vector<vector<ll>>(r + 1, vector<ll>(c + 1, 0)));
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
      }
      dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
      dp[1][i][j] = dp[1][i][j - 1];
      dp[2][i][j] = dp[2][i][j - 1];
      dp[3][i][j] = dp[3][i][j - 1];
      if (memo[i][j] != 0) {
        dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
        dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[i][r][c]);
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][4];
int n, m, k;
int main() {
  int x, y, v;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &v);
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k)
          f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
        f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
      }
      for (int k = 0; k <= 3; k++) {
        f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
        f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, f[n][m][i]);
  printf("%lld\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][3];
int n, m, k;
int main() {
  int x, y, v;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &v);
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k)
          f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
        f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
      }
      for (int k = 0; k <= 3; k++) {
        f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
        f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, f[n][m][i]);
  printf("%lld\\n", ans);
  return 0;
}