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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define endl "\\n"
#include "string"

const int inf = 1e16;
const int N = 3003;
int a[N][N], dp[N][N][4];
int r, c, n;

int solve(int x, int y, int cur) {
  if (x > r or y > c)
    return 0;

  int &tmp = dp[x][y][cur];
  if (tmp != -1)
    return tmp;
  tmp = max(solve(x + 1, y, 0), solve(x, y + 1, cur));
  tmp = max(tmp, solve(x, y + 1, cur));
  if (cur + 1 < 4) {
    tmp = max(tmp, solve(x + 1, y, 0) + a[x][y]);
    tmp = max(tmp, solve(x, y + 1, cur + 1) + a[x][y]);
  }
  return tmp;
}

int32_t main() {
  boost;
  cin >> r >> c >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    a[x][y] = val;
  }
  int ans = solve(1, 1, 0);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define endl "\\n"
#include "string"

int32_t main() {
  boost;
  int r, c, n;
  cin >> r >> c >> n;
  int a[r + 1][c + 1], dp[r + 1][c + 1][4];
  ;
  memset(a, -1, sizeof a);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    a[x][y] = max(a[x][y], val);
  }
  if (a[1][1] == -1) {
    cout << 0 << endl;
    return 0;
  }

  int ans = 0;
  dp[1][1][1] = a[1][1];
  dp[1][1][0] = 0;

  queue<array<int, 4>> pq;
  pq.push({a[1][1], 1, 1, 1});
  pq.push({0, 1, 1, 0});
  ans = a[1][1];
  while (!pq.empty()) {
    auto u = pq.front();
    pq.pop();
    ans = max(ans, u[0]);
    int x = u[1], y = u[2], ig = u[3];
    // cout << x << ' ' << y << ' ' <<ig << ' ' <<  u[0] << endl;
    if (x + 1 <= r && a[x + 1][y] > 0 && a[x + 1][y] + u[0] > dp[x + 1][y][1]) {
      dp[x + 1][y][1] = a[x + 1][y] + u[0];
      pq.push({dp[x + 1][y][1], x + 1, y, 1});
    }

    if (x + 1 <= r && a[x + 1][y] > 0 && u[0] > dp[x + 1][y][0]) {
      dp[x + 1][y][0] = u[0];
      pq.push({dp[x + 1][y][0], x + 1, y, 0});
    }

    if (y + 1 <= c && ig + 1 <= 3 && a[x][y + 1] > 0 &&
        a[x][y + 1] + u[0] > dp[x][y + 1][ig + 1]) {
      dp[x][y + 1][ig + 1] = a[x][y + 1] + u[0];
      pq.push({dp[x][y + 1][ig + 1], x, y + 1, ig + 1});
    }

    if (y + 1 <= c && a[x][y + 1] > 0 && u[0] > dp[x][y + 1][ig]) {
      dp[x][y + 1][ig] = u[0];
      pq.push({dp[x][y + 1][ig], x, y + 1, ig});
    }
  }
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
vector<vector<vector<ll>>>
    dp(3005, vector<vector<ll>>(3005, vector<ll>(4, -INT_MAX)));
ll dfs(vector<vector<ll>> &g, ll i, ll j, ll k, ll r, ll c) {
  //	cout<<i<<" "<<j<<" "<<k<<endl;
  if (i < 0 || i >= r || j < 0 || j >= c || k > 3)
    return -INT_MAX;
  if ((i == r - 1) && (j == c - 1)) {
    if (k < 3)
      return g[i][j];
    else
      return 0;
  }
  if (dp[i][j][k] != -INT_MAX)
    return dp[i][j][k];
  dp[i][j][k] = max(g[i][j] + dfs(g, i, j + 1, k + 1, r, c),
                    max(dfs(g, i + 1, j, 0, r, c), dfs(g, i, j + 1, k, r, c)));
  if (k < 3)
    dp[i][j][k] = max(dp[i][j][k], g[i][j] + dfs(g, i + 1, j, 0, r, c));
  return dp[i][j][k];
}
int main() {
  ll r, c, k, x, y, z;
  cin >> r >> c >> k;
  vector<vector<ll>> g(r, vector<ll>(c, 0));
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> z;
    g[x - 1][y - 1] = z;
  }
  ll ans = 0;
  ans = dfs(g, 0, 0, 0, r, c);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dfs(vector<vector<vector<ll>>> &dp, vector<vector<ll>> &g, ll i, ll j, ll k,
       ll r, ll c) {
  if (i < 0 || i >= r || j < 0 || j >= c || k > 3)
    return 0;
  if (dp[i][j][k] != -INT_MAX)
    return dp[i][j][k];
  dp[i][j][k] =
      max(dfs(dp, g, i + 1, j, 0, r, c), dfs(dp, g, i, j + 1, k, r, c));
  if (k < 3)
    dp[i][j][k] =
        max(dp[i][j][k], max(g[i][j] + dfs(dp, g, i + 1, j, 0, r, c),
                             g[i][j] + dfs(dp, g, i, j + 1, k + 1, r, c)));
  return dp[i][j][k];
}
int main() {
  ll r, c, k, x, y, z;
  cin >> r >> c >> k;
  vector<vector<ll>> g(r, vector<ll>(c, 0));
  vector<vector<vector<ll>>> dp(
      r + 10, vector<vector<ll>>(c + 10, vector<ll>(k + 10, -INT_MAX)));
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> z;
    g[x - 1][y - 1] = z;
  }
  ll ans = 0;
  ans = dfs(dp, g, 0, 0, 0, r, c);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<vector<ll>>> vec(3100, vector<vector<ll>>(3100, vector<ll>(4)));
  vector<vector<ll>> p(3100, vector<ll>(3100));
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      vec[i][j][0] = 0;
      vec[i][j][1] = 0;
      vec[i][j][2] = 0;
      vec[i][j][3] = 0;
      p[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    ll point;
    cin >> a >> b >> point;
    p[a][b] = point;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
      vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
                          vec[i - 1][j][2], vec[i - 1][j][3]});
      vec[i][j][1] =
          max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
      vec[i][j][1] =
          max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
                                  vec[i - 1][j][2], vec[i - 1][j][3]}) +
                                 p[i][j]});
      vec[i][j][2] =
          max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
      // vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
      vec[i][j][3] =
          max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
      // vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
    }
  }
  cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
       << endl;
}

Error: -11

Fixed submission:

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll vec[3100][3100][4] = {};
  ll p[3100][3100] = {};
  for (int i = 0; i < k; i++) {
    int a, b;
    ll point;
    cin >> a >> b >> point;
    p[a][b] = point;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
      vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
                          vec[i - 1][j][2], vec[i - 1][j][3]});
      vec[i][j][1] =
          max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
      vec[i][j][1] =
          max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
                                  vec[i - 1][j][2], vec[i - 1][j][3]}) +
                                 p[i][j]});
      vec[i][j][2] =
          max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
      // vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
      vec[i][j][3] =
          max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
      // vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
    }
  }
  cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
       << 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 repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<ll> r(K), c(K), v(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
  vector<vector<ll>> point(R + 2, vector<ll>(C + 2));
  rep(i, R + 2) {
    rep(j, C + 2) { point[i][j] = 0; }
  }
  rep(i, K) { point[r[i]][c[i]] = v[i]; }
  vector<vector<vector<ll>>> dp(R + 2,
                                vector<vector<ll>>(C + 2, vector<ll>(5)));
  for (ll i = 0; i <= R + 1; i++) {
    for (ll j = 0; j <= C + 1; j++) {
      for (ll k = 0; k < 5; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][1][1] = point[1][1];
  for (ll i = 1; i <= R; i++) {
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i][j + 1][k + 1] = max(dp[i][j][k] + point[i][j + 1],
                                  max(dp[i][j + 1][k + 1], dp[i][j][k + 1]));
        dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
      }
    }
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + point[i + 1][j]);
      }
    }
  }
  ll ans = max(dp[R][C][0], max(dp[R][C][1], max(dp[R][C][2], dp[R][C][3])));
  cout << ans << 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 repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

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

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

constexpr int N_MAX = 3001;
ll dp[4][N_MAX][N_MAX] = {};

struct item {
  int r;
  int c;
  ll v;
};

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<item> vitem;
  for (int i = 0; i < k; ++i) {
    int ri, ci;
    ll vi;
    cin >> ri >> ci >> vi;
    vitem.emplace_back(item{ri, ci, vi});
  }
  sort(vitem.begin(), vitem.end(),
       [](item &a, item &b) { return a.r < b.r || (a.r == b.r && a.c < b.c); });
  int item_itr = 0;
  auto &itm = vitem[item_itr];
  for (int ri = 1; ri <= r; ++ri) {
    for (int ci = 1; ci <= c; ++ci) {
      // 上から来る場合
      dp[0][ri][ci] = 0;
      for (int n = 0; n <= 3; ++n)
        dp[0][ri][ci] = max(dp[0][ri][ci], dp[n][ri - 1][ci]);
      // 左から来る
      for (int n = 1; n <= 3; ++n) {
        dp[n][ri][ci] = max(dp[n][ri][ci], dp[n][ri][ci - 1]);
      }
      // 乗せる
      if (itm.r == ri && itm.c == ci) {
        for (int n = 3; n >= 1; --n) {
          dp[n][ri][ci] = max(dp[n][ri][ci], dp[n - 1][ri][ci] + itm.v);
        }
        ++item_itr;
        if (item_itr < k)
          itm = vitem[item_itr];
      }
    }
  }
  ll ans = 0;
  for (int n = 0; n <= 3; ++n) {
    ans = max(ans, dp[n][r][c]);
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

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

constexpr int N_MAX = 6;
ll dp[4][N_MAX][N_MAX] = {};

struct item {
  int r;
  int c;
  ll v;
};

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<item> vitem;
  for (int i = 0; i < k; ++i) {
    int ri, ci;
    ll vi;
    cin >> ri >> ci >> vi;
    vitem.emplace_back(item{ri, ci, vi});
  }
  sort(vitem.begin(), vitem.end(),
       [](item &a, item &b) { return a.r < b.r || (a.r == b.r && a.c < b.c); });
  int item_itr = 0;
  auto &itm = vitem[item_itr];
  for (int ri = 1; ri <= r; ++ri) {
    for (int ci = 1; ci <= c; ++ci) {
      // 上から来る場合
      dp[0][ri][ci] = 0;
      for (int n = 0; n <= 3; ++n)
        dp[0][ri][ci] = max(dp[0][ri][ci], dp[n][ri - 1][ci]);
      // 左から来る
      for (int n = 1; n <= 3; ++n) {
        dp[n][ri][ci] = max(dp[n][ri][ci], dp[n][ri][ci - 1]);
      }
      // 乗せる
      if (itm.r == ri && itm.c == ci) {
        for (int n = 3; n >= 1; --n) {
          dp[n][ri][ci] = max(dp[n][ri][ci], dp[n - 1][ri][ci] + itm.v);
        }
        ++item_itr;
        if (item_itr < k)
          itm = vitem[item_itr];
      }
    }
  }
  ll ans = 0;
  for (int n = 1; n <= 3; ++n) {
    ans = max(ans, dp[n][r][c]);
  }
  cout << ans;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
// const long long INF = 1LL << 60;

long long V[3001][3001];
long long dp[3001][3001][5];

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

  dp[0][0][1] = V[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // to right
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // to down
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  long long int 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>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)

const long long INF = 1LL << 60;

int main() {
  int i, j, k, R, C, K;
  cin >> R >> C >> K;
  long long V[3001][3001];
  long long dp[3001][3001][5];
  memset(V, 0, sizeof(V));
  memset(dp, 0, sizeof(dp));
  int r, c, v;
  rep(i, K) {
    cin >> r >> c >> v;
    V[r - 1][c - 1] = v;
  }

  dp[0][0][1] = V[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // to right
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // to down
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  long long int 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 (i = 0; i < n; i++)
// const long long INF = 1LL << 60;

long long V[3001][3001];
long long dp[3001][3001][5];

int main() {

  int i, j, k, R, C, K;
  cin >> R >> C >> K;
  memset(V, 0, sizeof(V));
  memset(dp, 0, sizeof(dp));
  int r, c, v;
  rep(i, K) {
    cin >> r >> c >> v;
    V[r - 1][c - 1] = v;
  }

  dp[0][0][1] = V[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // to right
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // to down
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  long long int 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>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
// const long long INF = 1LL << 60;

int main() {
  long long V[3001][3001];
  long long dp[3001][3001][5];

  int i, j, k, R, C, K;
  cin >> R >> C >> K;
  memset(V, 0, sizeof(V));
  memset(dp, 0, sizeof(dp));
  int r, c, v;
  rep(i, K) {
    cin >> r >> c >> v;
    V[r - 1][c - 1] = v;
  }

  dp[0][0][1] = V[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // to right
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // to down
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  long long int 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 <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(s, i, n) for (i = s; i < n; i++)
using namespace std;

long long dp[3001][3001][5];
int value[3001][3001];

int main() {
  memset(dp, 0, sizeof(dp));
  memset(value, 0, sizeof(value));

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

  int i, j, k, r, c, v;
  rep0(i, K) {
    cin >> r >> c >> v;
    value[r - 1][c - 1] = v;
  }

  dp[0][0][1] = value[0][0];

  rep0(i, R) {
    rep0(j, C) {
      rep0(k, 4) {
        // to down
        dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
        dp[i + 1][j][1] = max(dp[i][j][k] + value[i + 1][j], dp[i + 1][j][1]);

        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + value[i][j + 1]);
      }
    }
  }

  long long ans = 0;
  rep0(k, 4) ans = max(ans, dp[R - 1][C - 1][k]);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
const long long INF = 1LL << 60;

int main() {
  int V[3001][3001];
  long long dp[3001][3001][5];
  int i, j, k, R, C, K;
  cin >> R >> C >> K;
  memset(V, 0, sizeof(V));
  memset(dp, 0, sizeof(dp));
  int r, c, v;
  rep(i, K) {
    cin >> r >> c >> v;
    V[r - 1][c - 1] = v;
  }

  dp[0][0][1] = V[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // to right
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // to down
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }

  long long 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>
#define lld long long
#define pb push_back
#define mk make_pair
#define MAX (lld)1e18 + 7
#define lim (lld)1e9 + 7
#define MAX2 (lld)2e5 + 9
#define ff first
#define ss second
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
lld inp[3000 + 3][3000 + 3];
lld dp[3000 + 3][3000 + 3][4];
int main() {
  fastio lld r, c, k;
  cin >> r >> c >> k;

  for (lld i = 0; i < k; i++) {
    lld x, y, w;
    cin >> x >> y >> w;
    inp[x][y] = w;
  }

  for (lld i = 1; i <= r; i++) {
    for (lld j = 1; j <= c; j++) {
      lld mx = 0;
      for (lld p = 1; p <= 3; p++) {
        mx = max(mx, dp[i - 1][j][p]);
      }

      for (lld k = 0; k <= 3; k++) {

        if (k > 0)
          dp[i][j][k] =
              max(max(dp[i][j - 1][k - 1], mx) + inp[i][j], dp[i][j][k]);

        dp[i][j][k] = max(dp[i][j][k], max(dp[i][j - 1][k], mx));
      }
    }
  }

  lld ans = 0;
  for (lld k = 1; k <= 3; k++)
    ans = max(ans, dp[r][c][k]);

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lld long long
#define pb push_back
#define mk make_pair
#define MAX (lld)1e18 + 7
#define lim (lld)1e9 + 7
#define MAX2 (lld)2e5 + 9
#define ff first
#define ss second
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

const lld mod = lim;

lld power(lld x, lld y, lld p) {
  lld res = 1;
  x = x % p;
  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

lld extend_gcd(lld a, lld b, lld &x, lld &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  lld x1, y1;
  lld d = extend_gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

lld rowNum[] = {-1, 0, 0, 1};
lld colNum[] = {0, -1, 1, 0};

int main() {
  fastio lld r, c, k;
  cin >> r >> c >> k;
  lld inp[r + 1][c + 1];

  memset(inp, 0, sizeof(inp));

  for (lld i = 0; i < k; i++) {
    lld x, y, w;
    cin >> x >> y >> w;
    inp[x][y] = w;
  }

  lld dp[r + 1][c + 1][4];

  memset(dp, 0, sizeof(dp));

  for (lld i = 1; i <= r; i++) {
    for (lld j = 1; j <= c; j++) {
      lld mx = 0;
      for (lld p = 1; p <= 3; p++) {
        mx = max(mx, dp[i - 1][j][p]);
      }

      for (lld k = 0; k <= 3; k++) {

        if (k > 0)
          dp[i][j][k] =
              max(max(dp[i][j - 1][k - 1], mx) + inp[i][j], dp[i][j][k]);

        dp[i][j][k] = max(dp[i][j][k], max(dp[i][j - 1][k], mx));
      }
    }
  }

  lld ans = 0;
  for (lld k = 1; k <= 3; k++)
    ans = max(ans, dp[r][c][k]);

  cout << ans << endl;
}