alexjercan/bugnet huggingface dataset:
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    scanf("%lld", &a[x][y]);
  }
  dp[1][1][0] = 0;
  if (a[1][1] >= 0)
    dp[1][1][1] = a[1][1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 && j == 1)
        continue;
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      for (int x = 0; x <= 3; x++)
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][x]),
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][x] + a[i][j]);
      for (int x = 1; x <= min(j, 3); x++) {
        dp[i][j][x] = max(dp[i][j][x],
                          max(dp[i][j - 1][x], dp[i][j - 1][x - 1] + a[i][j]));
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[n][m][i]);
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = -inf;
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x][y] != -inf)
      while (1)
        ;
    scanf("%lld", &a[x][y]);
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      for (int x = 0; x <= 3; x++)
        dp[i][j][3] = -inf;
  if (&dp[1][1][k] != &dp[1][1][3])
    while (1)
      ;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dp[i][j][0] = 0;
  dp[1][1][0] = 0;
  if (a[1][1] >= 0)
    dp[1][1][1] = a[1][1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j][0] = 0;
      if (i == 1 && j == 1)
        continue;
      if (a[i][j] <= 0) {
        for (int x = 0; x <= 3; x++) {
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][x]);
          dp[i][j][x] = max(dp[i][j][x], dp[i][j - 1][x]);
        }
        continue;
      }
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      for (int x = 0; x <= 3; x++)
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][x]),
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][x] + a[i][j]);
      for (int x = 1; x <= min(j, 3); x++) {
        dp[i][j][x] = max(dp[i][j][x],
                          max(dp[i][j - 1][x], dp[i][j - 1][x - 1] + a[i][j]));
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[n][m][i]);
  printf("%lld\\n", ans);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
const long long inf = 0x3f3f3f3f;
long long dp[maxn][maxn][5];
long long b[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  memset(b, -inf, sizeof(b));
  long long n, m, x;
  cin >> n >> m >> x;
  for (long long i = 1; i <= x; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    b[r][c] = v;
  }
  for (long long i = 0; i <= n; i++) {
    for (long long j = 0; j <= m; j++) {
      for (long long k = 0; k <= 3; k++) {
        if (i - 1 >= 0) {
          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] + b[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k - 1 >= 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + b[i][j]);
        }
      }
    }
  }
  long long ans = 0;
  for (long long k = 0; k <= 3; k++)
    ans = max(dp[n][m][k], ans);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
const int inf = 0x3f3f3f3f;
int r[maxn], c[maxn], v[maxn];
int dp[maxn][maxn][5];
int b[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  memset(b, -inf, sizeof(b));
  int n, m, x;
  cin >> n >> m >> x;
  for (int i = 1; i <= x; i++) {
    cin >> r[i] >> c[i] >> v[i];
    b[r[i]][c[i]] = v[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i - 1 >= 0) {
          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] + b[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k - 1 >= 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + b[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k <= 3; k++)
    ans = max(dp[n][m][k], ans);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <cstddef>
#include <cstdint>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using usize = int;
using u64 = long long;
using point = std::pair<usize, usize>;

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

u64 dp[3003][3003][4];

int main() {
  usize r, c, k;
  std::cin >> r >> c >> k;
  std::map<point, u64> items;
  for (usize i = 0; i < k; i++) {
    usize x, y;
    u64 value;
    std::cin >> x >> y >> value;
    items[point(x - 1, y - 1)] = value;
  }

  for (usize i = 0; i <= r; i++)
    for (usize j = 0; j <= c; j++) {
      for (usize s = 0; s < 4; s++) {
        if (i < r)
          chmax(dp[i + 1][j][0], dp[i][j][s]);
        if (j < c)
          chmax(dp[i][j + 1][s], dp[i][j][s]);
      }
      if (items.count(point(i, j))) {
        const u64 item = items[point(i, j)];
        for (usize s = 0; s < 3; s++) {
          if (i < r)
            chmax(dp[i + 1][j][0], dp[i][j][s] + item);
          if (j < c)
            chmax(dp[i][j + 1][s + 1], dp[i][j][s] + item);
        }
      }
    }

  u64 ans = 0;
  for (usize s = 0; s < 4; s++)
    chmax(ans, dp[r][c][s]);

  std::cout << ans << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <cstddef>
#include <cstdint>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using usize = int;
using u64 = long long;
using point = std::pair<usize, usize>;

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int main() {
  usize r, c, k;
  std::cin >> r >> c >> k;
  std::map<point, u64> items;
  for (usize i = 0; i < k; i++) {
    usize x, y;
    u64 value;
    std::cin >> x >> y >> value;
    items[point(x - 1, y - 1)] = value;
  }

  u64 dp[r + 1][c + 1][4];
  for (usize i = 0; i < r + 1; i++) {
    for (usize j = 0; j < c + 1; j++) {
      for (usize k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }

  for (usize i = 0; i <= r; i++)
    for (usize j = 0; j <= c; j++) {
      for (usize s = 0; s < 4; s++) {
        if (i < r)
          chmax(dp[i + 1][j][0], dp[i][j][s]);
        if (j < c)
          chmax(dp[i][j + 1][s], dp[i][j][s]);
      }
      if (items.count(point(i, j))) {
        const u64 item = items[point(i, j)];
        for (usize s = 0; s < 3; s++) {
          if (i < r)
            chmax(dp[i + 1][j][0], dp[i][j][s] + item);
          if (j < c)
            chmax(dp[i][j + 1][s + 1], dp[i][j][s] + item);
        }
      }
    }

  u64 ans = 0;
  for (usize s = 0; s < 4; s++)
    chmax(ans, dp[r][c][s]);

  std::cout << ans << '\\n';

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

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

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

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

  for (size_t r = 1; r <= R; r++) {
    for (size_t c = 1; c <= C; c++) {
      for (size_t k = 0; k <= 3; k++) {
        dp[r][c][0] = max(dp[r][c][0], dp[r - 1][c][k]);
        dp[r][c][1] = max(dp[r][c][1], dp[r - 1][c][k] + rc[r][c]);
        dp[r][c][k] = max(dp[r][c][k], dp[r][c - 1][k]);
      }

      for (size_t k = 1; k <= 3; k++) {
        dp[r][c][k] = max(dp[r][c][k], dp[r][c - 1][k - 1] + rc[r][c]);
      }
    }
  }
  cout << max({dp.back().back()[0], dp.back().back()[1], dp.back().back()[2],
               dp.back().back()[3]})
       << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<vector<vector<long long>>> dp(
      R, vector<vector<long long>>(C, vector<long long>(4, -1)));
  vector<vector<long long>> rc(R, vector<long long>(C, 0));
  for (size_t i = 0; i < K; i++) {
    size_t r, c;
    long long v;
    cin >> r >> c >> v;
    r--;
    c--;
    rc[r][c] = v;
  }

  queue<pair<pair<size_t, size_t>, pair<long long, size_t>>> q;
  q.push(make_pair(make_pair(0, 0), make_pair(0, 0)));
  while (!q.empty()) {
    size_t r = q.front().first.first;
    size_t c = q.front().first.second;
    long long v = q.front().second.first;
    size_t k = q.front().second.second;
    q.pop();

    if (rc[r][c] > 0 && k + 1 <= 3) {
      if (dp[r][c][k + 1] < v + rc[r][c]) {
        dp[r][c][k + 1] = v + rc[r][c];
        if (r + 1 < R) {
          q.push(make_pair(make_pair(r + 1, c), make_pair(dp[r][c][k + 1], 0)));
        }
        if (c + 1 < C) {
          q.push(make_pair(make_pair(r, c + 1),
                           make_pair(dp[r][c][k + 1], k + 1)));
        }
      }
    }
    if (dp[r][c][k] < v) {
      dp[r][c][k] = v;
      if (r + 1 < R) {
        q.push(make_pair(make_pair(r + 1, c), make_pair(dp[r][c][k], 0)));
      }
      if (c + 1 < C) {
        q.push(make_pair(make_pair(r, c + 1), make_pair(dp[r][c][k], k)));
      }
    }
  }
  cout << max({dp.back().back()[0], dp.back().back()[1], dp.back().back()[2],
               dp.back().back()[3]})
       << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

long long int L[3003][3003][4];

int G[3003][3003];
int H, W, K;

signed main() {
  cin >> H >> W >> K;
  int h, w, v;
  for (int i = 1; i <= K; i++) {
    cin >> h >> w >> v;
    G[h][w] = v;
  }
  for (int h = 1; h < H + 1; h++) {
    for (int w = 1; w < W + 1; w++) {
      long long int X = 0;
      for (int k = 0; k < 4; k++) {
        X = max(X, L[h - 1][w][k]);
      }
      for (int k = 1; k < 4; k++) {
        L[h][w][k] =
            max(max(L[h][w - 1][k], L[h][w - 1][k - 1] + G[h][w]), X + G[h][w]);
      }
    }
  }

  long long int ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, L[H][W][i]);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int H;
  int W;
  int K;
  cin >> H >> W >> K;
  long long L[3000 + 3][3000 + 3][4];
  for (int h = 0; h < H + 3; h++) {
    for (int w = 0; w < W + 3; w++) {
      for (int i = 0; i < 4; i++) {
        L[h][w][i] = 0;
      }
    }
  }
  int G[H + 3][W + 3];
  for (int h = 0; h < H + 2; h++) {
    for (int w = 0; w < W + 2; w++) {
      G[h][w] = 0;
    }
  }
  for (int i = 0; i < K; i++) {
    int h, w, v;
    cin >> h >> w >> v;
    G[h][w] = v;
  }
  for (int h = 1; h < H + 1; h++) {
    for (int w = 1; w < W + 1; w++) {
      long long X;
      X = 0;
      for (int x = 0; x < 4; x++) {
        X = max(X, L[h - 1][w][x]);
      }
      L[h][w][0] = max(L[h][w - 1][0], X);
      L[h][w][1] = max(L[h][w - 1][1], L[h][w - 1][0] + G[h][w]);
      L[h][w][1] = max(L[h][w][1], X + G[h][w]);
      L[h][w][2] = max(L[h][w - 1][2], L[h][w - 1][1] + G[h][w]);
      L[h][w][3] = max(L[h][w - 1][3], L[h][w - 1][2] + G[h][w]);
    }
  }
  long long ans;
  ans = L[H][W][0];
  for (int i = 1; i < 4; i++) {
    ans = max(ans, L[H][W][i]);
  }
  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++)
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<int, ll> item;
  rep(i, k) {
    int l, m, v;
    cin >> l >> m >> v;
    l--;
    m--;
    item[c * l + m] = v;
  }

  vector<vector<ll>> dp1(3005, vector<ll>(4, -1));
  vector<vector<ll>> dp2(3005, vector<ll>(4, -1));
  dp2[0][0] = 0;
  if (item.count(0))
    dp2[0][1] = item[0];
  rep(i, r) {
    rep(j, c) {
      if (i > 0) {
        rep(k, 4) {
          if (dp1[j][k] != -1) {
            dp2[j][0] = max(dp2[j][0], dp1[j][k]);
            if (item.count(i * c + j))
              dp2[j][1] = max(dp2[j][1], dp1[j][k] + item[i * c + j]);
          }
        }
      }
      if (j > 0) {
        rep(k, 4) {
          if (dp2[j - 1][k] != -1) {
            dp2[j][k] = max(dp2[j][k], dp2[j - 1][k]);
          }
        }
        rep(k, 3) {
          if (dp2[j - 1][k] != -1) {
            if (item.count(i * c + j))
              dp2[j][k + 1] =
                  max(dp2[j][k + 1], dp2[j - 1][k] + item[i * c + j]);
          }
        }
      }
    }
    dp1 = dp2;
  }

  ll ans = 0;
  rep(i, 4) ans = max(dp1[c - 1][i], ans);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll item[3005][3005];
  rep(i, r) rep(j, c) item[i][j] = -1;
  rep(i, k) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    item[r][c] = v;
  }

  ll dp[3005][3005][4];
  rep(i, r) rep(j, c) rep(k, 4) { dp[i][j][k] = -1; }
  dp[0][0][0] = 0;
  if (item[0][0] != -1)
    dp[0][0][1] = item[0][0];
  rep(i, r) rep(j, c) {
    if (i > 0) {
      rep(k, 4) {
        if (dp[i - 1][j][k] != -1) {
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
          if (item[i][j] != -1)
            dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + item[i][j]);
        }
      }
    }
    if (j > 0) {
      rep(k, 4) {
        if (dp[i][j - 1][k] != -1) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        }
      }
      rep(k, 3) {
        if (dp[i][j - 1][k] != -1) {
          if (item[i][j] != -1)
            dp[i][j][k + 1] =
                max(dp[i][j][k + 1], dp[i][j - 1][k] + item[i][j]);
        }
      }
    }
  }

  /*
  rep(k,4){
    rep(i,r){
      rep(j,c){
        cout<<dp[i][j][k]<<' ';
      }
      cout<<endl;
    }
    cout<<endl;
  }
  */

  ll ans = 0;
  rep(i, 4) ans = max(dp[r - 1][c - 1][i], ans);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long int dp[3000][3000][4] = {0};
long long int f[3000][3000] = {0};
int main() {
  long long int R, C, K;
  cin >> R >> C >> K;

  for (int i = 0; i < K; i++) {
    long long int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    f[c][r] = v;
  }
  dp[0][0][1] = f[0][0];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (j - 1 >= 0 && k - 1 >= 0) {
          dp[j][i][k] = max(max(dp[j - 1][i][k - 1] + f[j][i], dp[j][i][k]),
                            dp[j - 1][i][k]);
        }
        if (i - 1 >= 0) {
          if (k == 0) {
            long long int m = 0;
            for (int l = 0; l < 4; l++)
              m = max(m, dp[j][i - 1][l]);
            dp[j][i][k] = max(m, dp[j][i][k]);
          } else if (k == 1) {
            dp[j][i][k] = max(dp[j][i][k - 1] + f[j][i], dp[j][i][k]);
          }
        }
      }
    }
  }
  long long int m = 0;
  for (int i = 0; i < 4; i++)
    m = max(dp[C - 1][R - 1][i], m);
  cout << m << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int R, C, K;
  cin >> R >> C >> K;
  long long int f[3000][3000] = {0};
  long long int dp[3000][3000][4] = {0};

  for (int i = 0; i < K; i++) {
    long long int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    f[c][r] = v;
  }
  dp[0][0][1] = f[0][0];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (j - 1 >= 0 && k - 1 >= 0) {
          dp[j][i][k] = max(max(dp[j - 1][i][k - 1] + f[j][i], dp[j][i][k]),
                            dp[j - 1][i][k]);
        }
        if (i - 1 >= 0) {
          if (k == 0) {
            long long int m = 0;
            for (int l = 0; l < 4; l++)
              m = max(m, dp[j][i - 1][l]);
            dp[j][i][k] = max(m, dp[j][i][k]);
          } else if (k == 1) {
            dp[j][i][k] = max(dp[j][i][k - 1] + f[j][i], dp[j][i][k]);
          }
        }
      }
    }
  }
  long long int m = 0;
  for (int i = 0; i < 4; i++)
    m = max(dp[C - 1][R - 1][i], m);
  cout << m << endl;
}
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++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(5)));
  vector<vector<ll>> m(r + 1, vector<ll>(c + 1));
  rep(i, r + 1) {
    rep(j, c + 1) {
      rep(p, 5) {
        dp.at(i).at(j).at(p) = 0;
        m.at(i).at(j) = 0;
      }
    }
  }
  rep(i, k) {
    int a, b;
    ll x;
    cin >> a >> b >> x;
    m.at(a).at(b) = x;
  }
  rep(i, r) {
    rep(j, c) {
      dp.at(i + 1).at(j + 1).at(0) =
          max(dp.at(i).at(j + 1).at(4), dp.at(i + 1).at(j).at(0));
      for (int p = 1; p < 4; p++) {
        dp.at(i + 1).at(j + 1).at(p + 1) =
            max(dp.at(i).at(j + 1).at(4) + m.at(i + 1).at(j + 1),
                max(dp.at(i + 1).at(j).at(p) + m.at(i + 1).at(j + 1),
                    dp.at(i + 1).at(j).at(p + 1)));
      }
    }
  }
  cout << dp.at(r).at(c).at(4) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll dp[r + 1][c + 1][5];
  ll m[r + 1][c + 1];
  rep(i, r + 1) {
    rep(j, c + 1) {
      rep(p, 5) {
        dp[i][j][p] = 0;
        m[i][j] = 0;
      }
    }
  }
  rep(i, k) {
    int a, b;
    ll x;
    cin >> a >> b >> x;
    m[a][b] = x;
  }
  rep(i, r) {
    rep(j, c) {
      dp[i + 1][j + 1][0] = max(dp[i][j + 1][4], dp[i + 1][j][0]);
      for (int p = 1; p < 4; p++)
        dp[i + 1][j + 1][p + 1] =
            max(dp[i][j + 1][4] + m[i + 1][j + 1],
                max(dp[i + 1][j][p] + m[i + 1][j + 1], dp[i + 1][j][p + 1]));
    }
  }
  cout << dp[r][c][4] << endl;
}
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;

int v[3000][3000];
ll dp[3000][3000][4];

int main() {

  int R, C, K;
  cin >> R >> C >> K;

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

  dp[0][0][1] = v[0][0];
  rep(r, R) rep(c, C) rep(k, 4) {
    if (r + 1 < R) { // 縦に動く
      // 拾わない
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][k]);
      // 拾う
      dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][k] + v[r + 1][c]);
    }
    if (c + 1 < C) { // 横に動く
      // 拾わない
      dp[r][c + 1][k] = max(dp[r][c + 1][k], dp[r][c][k]);
      // 拾う
      if (k + 1 <= 3) {
        dp[r][c + 1][k + 1] =
            max(dp[r][c + 1][k + 1], dp[r][c][k] + v[r][c + 1]);
      }
    }
  }

  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[R - 1][C - 1][i]);

  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {

  int v[3000][3000];
  ll dp[3000][3000][4];

  int R, C, K;
  cin >> R >> C >> K;

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

  dp[0][0][1] = v[0][0];
  rep(r, R) rep(c, C) rep(k, 4) {
    if (r + 1 < R) { // 縦に動く
      // 拾わない
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][k]);
      // 拾う
      dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][k] + v[r + 1][c]);
    }
    if (c + 1 < C) { // 横に動く
      // 拾わない
      dp[r][c + 1][k] = max(dp[r][c + 1][k], dp[r][c][k]);
      // 拾う
      if (k + 1 <= 3) {
        dp[r][c + 1][k + 1] =
            max(dp[r][c + 1][k + 1], dp[r][c][k] + v[r][c + 1]);
      }
    }
  }

  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[R - 1][C - 1][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 (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG

ll dp[3005][3005][5];
ll m[3005][3005];
int main() {
  int r, c, k;
  cin >> r >> c >> k;

  rep(i, r + 1) {
    rep(j, c + 1) {
      rep(p, 5) {
        dp[i][j][p] = 0;
        m[i][j] = 0;
      }
    }
  }
  rep(i, k) {
    int a, b;
    ll x;
    cin >> a >> b >> x;
    m[a][b] = x;
  }
  rep(i, r) {
    rep(j, c) {
      dp[i + 1][j + 1][0] = max(dp[i][j + 1][4], dp[i + 1][j][0]);
      for (int p = 1; p < 4; p++)
        dp[i + 1][j + 1][p + 1] =
            max(dp[i][j + 1][4] + m[i + 1][j + 1],
                max(dp[i + 1][j][p] + m[i + 1][j + 1], dp[i + 1][j][p + 1]));
    }
  }
  cout << dp[r][c][4] << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll dp[3005][3005][5];
  ll m[3005][3005];
  rep(i, r + 1) {
    rep(j, c + 1) {
      rep(p, 5) {
        dp[i][j][p] = 0;
        m[i][j] = 0;
      }
    }
  }
  rep(i, k) {
    int a, b;
    ll x;
    cin >> a >> b >> x;
    m[a][b] = x;
  }
  rep(i, r) {
    rep(j, c) {
      dp[i + 1][j + 1][0] = max(dp[i][j + 1][4], dp[i + 1][j][0]);
      for (int p = 1; p < 4; p++)
        dp[i + 1][j + 1][p + 1] =
            max(dp[i][j + 1][4] + m[i + 1][j + 1],
                max(dp[i + 1][j][p] + m[i + 1][j + 1], dp[i + 1][j][p + 1]));
    }
  }
  cout << dp[r][c][4] << endl;
}