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

#include <iostream>
using namespace std;
long long int dp[3001][3001][4];
long long int g[3001][3001];
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  r = 3000;
  c = 3000;
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      g[i][j] = 0;
      for (int kk = 0; kk < 4; kk++) {
        dp[i][j][kk] = 0;
      }
    }
  }
  long long int rr, cc, v;
  for (int i = 0; i < k; i++) {
    cin >> rr >> cc >> v;
    rr -= 1;
    cc -= 1;
    g[rr][cc] = v;
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; 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]);
        if (g[i - 1][j - 1] > 0) {
          if (k > 0) {
            if (dp[i][j][k] < dp[i][j - 1][k - 1] + g[i - 1][j - 1]) {
              dp[i][j][k] = dp[i][j - 1][k - 1] + g[i - 1][j - 1];
            }
          }
          if (dp[i][j][1] < dp[i - 1][j][k] + g[i - 1][j - 1]) {
            dp[i][j][1] = dp[i - 1][j][k] + g[i - 1][j - 1];
          }
        }
      }
    }
  }
  long long int ans = 0;
  for (int kk = 0; kk < 4; kk++) {
    ans = max(ans, dp[r][c][kk]);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  long long int r, c, k;
  long long int dp[3001][3001][4];
  long long int g[3001][3001];
  cin >> r >> c >> k;
  r = 3000;
  c = 3000;
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      g[i][j] = 0;
      for (int kk = 0; kk < 4; kk++) {
        dp[i][j][kk] = 0;
      }
    }
  }
  long long int rr, cc, v;
  for (int i = 0; i < k; i++) {
    cin >> rr >> cc >> v;
    rr -= 1;
    cc -= 1;
    g[rr][cc] = v;
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; 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]);
        if (g[i - 1][j - 1] > 0) {
          if (k > 0) {
            if (dp[i][j][k] < dp[i][j - 1][k - 1] + g[i - 1][j - 1]) {
              dp[i][j][k] = dp[i][j - 1][k - 1] + g[i - 1][j - 1];
            }
          }
          if (dp[i][j][1] < dp[i - 1][j][k] + g[i - 1][j - 1]) {
            dp[i][j][1] = dp[i - 1][j][k] + g[i - 1][j - 1];
          }
        }
      }
    }
  }
  // cout<<dp[r][c][0];
  long long int ans = 0;
  for (int kk = 0; kk < 4; kk++) {
    ans = max(ans, dp[r][c][kk]);
  }
  cout << ans;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  int R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> t(R, vector<ll>(C, 0));
  rep(i, k) {
    int r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    t[r][c] = v;
  }
  vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4, 0)));
  rep(i, R) rep(j, C) {
    if (i != R - 1) {
      rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
      rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
    }
    if (j != C - 1) {
      rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
      rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
    }
  }
  rep(i, 3)
      chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
  ll res = 0;
  rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  int R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> t(R, vector<ll>(C, 0));
  rep(i, k) {
    int r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    t[r][c] = v;
  }
  ll dp[R][C][4];
  rep(i, R) rep(j, C) rep(l, 4) dp[i][j][l] = 0;
  rep(i, R) rep(j, C) {
    if (i != R - 1) {
      rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
      rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
    }
    if (j != C - 1) {
      rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
      rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
    }
  }
  rep(i, 3)
      chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
  ll res = 0;
  rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
  cout << res << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3003][3003][5];
ll mat[3003][3003];
int main() {
  fast;

  ll n, m, k, j, i;
  cin >> n >> m >> k;
  while (k--) {
    ll r, c, v;
    cin >> r >> c >> v;
    mat[r][c] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ll ha = max(
          {dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
      dp[i][j][0] = max({ha, dp[i][j - 1][0]});
      dp[i][j][1] =
          max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
      dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
    }
  }
  cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3000][3000][4];
ll mat[3000][3000];
int main() {
  fast;

  ll n, m, k, j, i;
  cin >> n >> m >> k;
  while (k--) {
    ll r, c, v;
    cin >> r >> c >> v;
    mat[r][c] = v;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ll ha = max(
          {dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
      dp[i][j][0] = max({ha, dp[i][j - 1][0]});
      dp[i][j][1] =
          max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
      dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
    }
  }
  cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

long long dp[5010][5010][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 2; k >= 0; --k) {
        // そのマスを拾う
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + table[i][j]);
      }
      for (int k = 0; k <= 3; ++k) {
        // 右に移動
        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]);
      }
    }
  }

  // //init
  // for(int i=0; i<R; ++i){
  //     for(int j=0; j<C; ++j){
  //         for(int k=0; k<3; ++k){
  //             //右に行く場合
  //             //今のますを使う場合
  //             dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] +
  //             table[i][j]);
  //             // 今のますを使わない場合
  //             dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k+1]);

  //             //下に行く場合
  //             dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j+1][k+1]);
  //         }
  //     }
  // }

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

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

long long dp[6][6][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  // init
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k < 3; ++k) {
        // 右に行く場合
        // 使う場合
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
        // 使わない場合
        // 上の数をそのまま使う
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);
        // 一枚も選ばない場合の数を使う
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j + 1][k]);
        // dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k+1])

        // 下に行く場合
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
      }
    }
  }

  cout << dp[R][C - 1][0] << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

long long dp[3005][3005][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  // init
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k < 3; ++k) {
        // 右に行く場合
        // 使う場合
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
        // 使わない場合
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);

        // 下に行く場合
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
      }
    }
  }

  cout << dp[R][C - 1][0] << endl;
}

Error: 0

Fixed submission:

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

long long dp[6][6][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> table(R, vector<long long>(C));

  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    table[r][c] = v;
  }

  // init
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k < 3; ++k) {
        // 右に行く場合
        // 使う場合
        dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
        // 使わない場合
        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);

        // 下に行く場合
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
      }
    }
  }

  cout << dp[R][C - 1][0] << endl;
}
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;
template <class T> void chmax(T &a, T b) { a = max(a, b); };
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  // dp[1][1][0]= 0;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      for (int k = 2; k >= 0; k--) {
        chmax(dp[i][j][k + 1], dp[i][j][k] + g[i][j]);
      }
      for (int k = 0; k <= 3; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        chmax(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    chmax(ans, dp[r][c][i]);
  cout << ans << "\\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;
template <class T> void chmax(T &a, T b) { a = max(a, b); };
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3

void solve() {
  cin >> r >> c;
  int k;
  cin >> k;
  memset(g, 0, sizeof(g));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    g[x][y] = val;
  }

  // dp[1][1][0]= 0;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      for (int k = 2; k >= 0; k--) {
        chmax(dp[i][j][k + 1], dp[i][j][k] + g[i][j]);
        // chmax(dp[i+1][j][0], dp[i][j][k]+g[i][j]);
      }
      for (int k = 0; k <= 3; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        chmax(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  ll ans = 0;
  for (int i = 0; i <= 3; i++)
    chmax(ans, dp[r][c][i]);
  cout << ans << "\\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: Runtime Error

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

ll arr[3000 + 1][3000 + 1];
ll dp[3000 + 1][3000 + 1][4];
int main() {
  ll t;
  t = 1;

  // dp[i][j] = max(dp[i-1][j]+arr[i][j], dp[i][j-1] - min + max)
  // cin>>t;
  while (t--) {
    ll r, c, k;
    cin >> r >> c >> k;

    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < k; i++) {
      ll u, v, a;
      cin >> u >> v >> a;
      arr[u][v] = a;
    }

    for (ll i = 1; i <= r; i++) {
      for (ll j = 1; j <= c; j++) {
        if (i == 1 && j == 1) {
          dp[i][j][1] = arr[1][1];
          continue;
        }
        dp[i][j][1] =
            max({dp[i][j - 1][1],
                 max({dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]}) +
                     arr[i][j]});
        dp[i][j][2] = max({dp[i][j - 1][2], dp[i][j - 1][1] + arr[i][j]});
        dp[i][j][3] = max({dp[i][j - 1][3], dp[i][j - 1][2] + arr[i][j]});
      }
    }

    cout << max({dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  ll t;
  t = 1;

  // dp[i][j] = max(dp[i-1][j]+arr[i][j], dp[i][j-1] - min + max)
  // cin>>t;
  while (t--) {
    ll r, c, k;
    cin >> r >> c >> k;
    ll arr[r + 1][c + 1];
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < k; i++) {
      ll u, v, a;
      cin >> u >> v >> a;
      arr[u][v] = a;
    }

    ll dp[r + 1][c + 1][4];
    memset(dp, 0, sizeof(dp));

    for (ll i = 1; i <= r; i++) {
      for (ll j = 1; j <= c; j++) {
        if (i == 1 && j == 1) {
          dp[i][j][1] = arr[1][1];
          continue;
        }
        dp[i][j][1] =
            max({dp[i][j - 1][1],
                 max({dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]}) +
                     arr[i][j]});
        dp[i][j][2] = max({dp[i][j - 1][2], dp[i][j - 1][1] + arr[i][j]});
        dp[i][j][3] = max({dp[i][j - 1][3], dp[i][j - 1][2] + arr[i][j]});
      }
    }

    cout << max({dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << endl;
  }
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long
#define ld long double

using namespace std;

ll INF = (ll)1e20;
int iINF = (int)1e20;
ll mod = (ll)1e9 + 7;

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

ll v[3000][3000];
ll dp[3000][3000][4];
signed main() {
  ll n, m, k;
  cin >> n >> m >> k;
  fill(&v[0][0], &v[0][0] + n * m, 0);
  fill(&dp[0][0][0], &dp[0][0][0] + n * m * 4, 0);
  for (int i = 0; i < k; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[a - 1][b - 1] = c;
  }
  if (v[0][0])
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int l = 0; l < 4; ++l) {
        if (j != m - 1) {
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
          if (v[i][j + 1] && l != 3) {
            dp[i][j + 1][l + 1] =
                max(dp[i][j + 1][l + 1], dp[i][j][l] + v[i][j + 1]);
          }
        }
        if (i != n - 1) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
          if (v[i + 1][j]) {
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][l] + v[i + 1][j]);
          }
        }
      }
    }
  }
  ll ans = max(max(dp[n - 1][m - 1][0], dp[n - 1][m - 1][1]),
               max(dp[n - 1][m - 1][2], dp[n - 1][m - 1][3]));
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ld long double

using namespace std;

ll INF = (ll)1e20;
int iINF = (int)1e20;
ll mod = (ll)1e9 + 7;

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll v[n][m];
  fill(&v[0][0], &v[0][0] + n * m, 0);
  ll dp[n][m][4];
  fill(&dp[0][0][0], &dp[0][0][0] + n * m * 4, 0);
  for (int i = 0; i < k; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[a - 1][b - 1] = c;
  }
  if (v[0][0])
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int l = 0; l < 4; ++l) {
        if (j != m - 1) {
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
          if (v[i][j + 1] && l != 3) {
            dp[i][j + 1][l + 1] =
                max(dp[i][j + 1][l + 1], dp[i][j][l] + v[i][j + 1]);
          }
        }
        if (i != n - 1) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
          if (v[i + 1][j]) {
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][l] + v[i + 1][j]);
          }
        }
      }
    }
  }
  ll ans = max(max(dp[n - 1][m - 1][0], dp[n - 1][m - 1][1]),
               max(dp[n - 1][m - 1][2], dp[n - 1][m - 1][3]));
  cout << ans;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll i, j, k, dp[3010][3010][4], g[3010][3010];
int main() {
  ll r, c, l;
  cin >> r >> c >> l;
  for (i = 0; i <= r; i++) {
    for (j = 0; j <= c; j++) {
      g[i][j] = 0;
    }
  }
  for (i = 0; i <= r; i++) {
    for (j = 0; j <= c; j++) {
      for (k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (i = 0; i < l; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    g[x][y] = z;
  }
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      for (k = 0; k <= 3; k++) {
        if (g[i][j] > 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
          if (k != 0) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + g[i][j]);
          }
        }
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
    }
  }
  ll ans = 0;
  for (i = 0; i <= 3; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
int mp[3005][3005];
long long dp[3005][3005][4];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 1; i <= K; i++) {
    cin >> r >> c >> v;
    mp[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; 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] + mp[i][j]);
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + mp[i][j]);
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + mp[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + mp[i][j]);
      //				printf("i:%d j:%d\\n",i,j);
      //				cout<<dp[i][j][0]<<endl<<dp[i][j][1]<<endl<<dp[i][j][2]<<endl<<dp[i][j][3]<<endl;
    }
  }
  long long ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[R][C][i]);
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int mp[2005][2005];
long long dp[2005][2005][4][4];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 1; i <= K; i++) {
    cin >> r >> c >> v;
    mp[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        for (int l = 0; l <= 3; l++) {
          dp[i][j][k][0] = dp[i - 1][j][k][l]; // k为竖着有多少个
          if (k != 0)
            dp[i][j][k][1] =
                max(dp[i][j][k][1], dp[i - 1][j][k - 1][l] + mp[i][j]);
          dp[i][j][0][l] = dp[i][j - 1][k][l];
          if (l != 0)
            dp[i][j][1][l] =
                max(dp[i][j][1][l], dp[i][j - 1][k][l - 1] + mp[i][j]);
          //					printf("%d %d %d %d
          //%lld\\n",i,j,k,l,dp[i][j][k][l]);
          //					dp[i][j][k][0]=max(dp[i-1][j][k][l],dp[i][j-1][k][l]);
          //					dp[i][j][k][l]=max(dp[i][j][k][l],dp[i-1][j][k-1][0]+mp[i][j]);
          //					dp[i][j][k][l]=max(dp[i][j][k][l],dp[i][j-1][0][l-1]+mp[i][j]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i <= 3; i++) {
    for (int j = 0; j <= 3; j++) {
      ans = max(ans, dp[R][C][i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}