alexjercan/bugnet huggingface dataset:
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;
#define chmax(x, y) x = max(x, y)

int Item[3005][3005];
ll DP[3005][3005][4];

int main() {
  int R, C, k;
  cin >> R >> C >> k;
  rep(i, k) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    Item[r][c] = v;
  }
  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      chmax(DP[i][j][k + 1], DP[i][j][k] + Item[i][j]);
    }
    rep(k, 4) {
      chmax(DP[i + 1][j][0], DP[i][j][k]);
      chmax(DP[i][j + 1][k], DP[i][j][k]);
    }
  }

  ll ans = 0;
  rep(k, 4) chmax(ans, DP[R - 1][C - 1][k]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int DP[R][C], Item[R][C];
  priority_queue<int> check[R][C];
  rep(i, R) { rep(j, C) Item[i][j] = 0; }
  rep(i, K) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    Item[r - 1][c - 1] = v;
  }
  DP[0][0] = Item[0][0];
  for (int i = 1; i < C; i++) {
    DP[0][i] = DP[0][i - 1] + Item[0][i];
    if (Item[0][i] != 0) {
      if (check[0][i].size() == 3) {
        if (check[0][i].top() <= -Item[0][i]) {
          DP[0][i] = DP[0][i - 1];
        } else {
          int ss = check[0][i].top();
          check[0][i].pop();
          check[0][i].push(-Item[0][i]);
          DP[0][i] -= (-ss);
        }
      } else
        check[0][i].push(-Item[0][i]);
    }
  }
  for (int i = 1; i < R; i++) {
    DP[i][0] = DP[i - 1][0] + Item[i][0];
    if (Item[0][i] != 0) {
      check[i][0].push(-Item[0][i]);
    }
  }
  for (int i = 1; i < R; i++) {
    for (int j = 1; j < C; j++) {
      DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]) + Item[i][j];
      if (Item[i][j] != 0) {
        if (check[i][j].size() == 3) {
          if (check[i][j].top() <= -Item[i][j]) {
            DP[i][j] -= Item[i][j];
          } else {
            int ss = check[i][j].top();
            check[i][j].pop();
            check[i][j].push(-Item[i][j]);
            DP[i][j] -= (-ss);
          }
        } else
          check[i][j].push(-Item[i][j]);
      }
    }
  }
  cout << DP[R - 1][C - 1] << 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 Item[3005][3005];
ll DP[3005][3005][4];

int main() {
  int R, C, k;
  cin >> R >> C >> k;
  rep(i, k) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    Item[r][c] = v;
  }
  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      DP[i][j][k + 1] = max(DP[i][j][k + 1], DP[i][j][k] + Item[i][j]);
    }
    rep(k, 4) {
      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]);
    }
  }

  ll ans = 0;
  rep(k, 4) ans = max(ans, DP[R - 1][C - 1][k]);
  cout << ans << endl;
}

Error: 0

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 R, C, k;
  cin >> R >> C >> k;
  int Item[R + 1][C + 1];
  ll DP[R + 1][C + 1][4];
  rep(i, R + 1) rep(j, C + 1) Item[i][j] = 0;
  rep(i, R + 1) rep(j, C + 1) rep(k, 4) DP[i][j][k] = 0;
  rep(i, k) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    Item[r][c] = v;
  }
  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      DP[i][j][k + 1] = max(DP[i][j][k + 1], DP[i][j][k] + Item[i][j]);
    }
    rep(k, 4) {
      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]);
    }
  }

  ll ans = 0;
  rep(k, 4) ans = max(ans, DP[R - 1][C - 1][k]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(a) ((a) & -(a))
#define clean(a, b) memset(a, b, sizeof(a))
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 9;
// typedef pair<int,int>P;

int _;

//========================================================================
ll dp[3009][3009][10], mp[3009][3009];
//========================================================================
int main() {
  int x, y, k, a, b;
  ll c;
  scanf("%d%d%d", &x, &y, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%lld", &a, &b, &c);
    mp[a][b] = c;
  }
  for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= y; j++) {
      if (j == 1) {
        dp[i][j][3] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][2] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][1] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][0] = dp[i - 1][1][3];
        continue;
      }
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][3]);
      for (int t = 1; t <= 3; t++) {
        dp[i][j][t] = max(dp[i][j - 1][t], dp[i][j - 1][t - 1] + mp[i][j]);
        dp[i][j][t] = max(dp[i][j][t], dp[i - 1][j][3] + mp[i][j]);
      }
    }
  }
  printf("%lld\\n", dp[x][y][3]);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(a) ((a) & -(a))
#define clean(a, b) memset(a, b, sizeof(a))
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 9;
// typedef pair<int,int>P;

int _;

//========================================================================
int dp[3009][3009][10], mp[3009][3009];
//========================================================================
int main() {
  int x, y, k, a, b, c;
  scanf("%d%d%d", &x, &y, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &a, &b, &c);
    mp[a][b] = c;
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      if (j == 1) {
        dp[i][j][3] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][2] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][1] = dp[i - 1][1][3] + mp[i][j];
        dp[i][j][0] = dp[i - 1][1][3];
        continue;
      }
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][3]);
      for (int t = 1; t <= 3; t++) {
        dp[i][j][t] = max(dp[i][j - 1][t], dp[i][j - 1][t - 1] + mp[i][j]);
        dp[i][j][t] = max(dp[i][j][t], dp[i - 1][j][3] + mp[i][j]);
      }
    }
  }
  printf("%d\\n", dp[x][y][3]);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100 // 000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> m;
  rep(i, k) {
    int a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    d[a][b] = v;
  }

  dp[0][0][0] = 0;

  rep(i, r) rep(j, c) {
    for (int k = 2; k >= 0; k--) {
      dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + d[i][j]);
    }
    rep(k, 4) {
      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]);
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100 // 000000000
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<int, int>
#define PP pair<P, P>
#define T tuple<int, int, int>
using namespace std;

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<P, ll> m;
  rep(i, k) {
    int a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    m[P(a, b)] = v;
  }

  dp[0][0][0] = 0;

  rep(i, r) rep(j, c) {
    for (int k = 2; k >= 0; k--) {
      dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + m[P(i, j)]);
    }
    rep(k, 4) {
      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]);
    }
  }
  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 <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

ll val[3001][3001];
ll dp[3001][3001][4];

int main() {
  int n, m, x;
  scanf("%d %d %d", &n, &m, &x);
  for (int i = 0; i < x; i++) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    val[r][c] = v;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i < n) {
          dp[i + 1][j][0] =
              max(dp[i + 1][j][0], dp[i][j][k] + (k < 3 ? val[i][j] : 0));
        }
        if (j < m) {
          if (k < 3) {
            dp[i][j + 1][k + 1] =
                max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
          }
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  printf("%lld\\n", dp[n][m][0]);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  int n, m, x;
  scanf("%d %d %d", &n, &m, &x);
  ll val[3001][3001] = {};
  for (int i = 0; i < x; i++) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    r--;
    c--;
    val[r][c] = v;
  }
  ll dp[3001][3001][4] = {};
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i < n) {
          dp[i + 1][j][0] =
              max(dp[i + 1][j][0], dp[i][j][k] + (k < 3 ? val[i][j] : 0));
        }
        if (j < m) {
          if (k < 3) {
            dp[i][j + 1][k + 1] =
                max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
          }
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  printf("%lld\\n", dp[n][m][0]);
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

typedef long long ll;

bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int R, C, K;
  scanf("%d%d%d", &R, &C, &K);
  int V[R][C + 1];
  fill(V[0], V[R], 0);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    scanf("%d%d%d", &r, &c, &v);
    r--;
    c--;
    V[r][c] = v;
  }
  ll dp[2][C + 1][4];
  for (int i = 0; i < R; i++) {
    int b = i & 1;
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[b][j][k] = 0;
        if ((i) && (k == 0))
          chmax(dp[b][j][k], dp[!b][j][3]);
        if ((i) && (k == 0))
          chmax(dp[b][j][k], dp[!b][j][2] + V[i - 1][j]);
        if (j)
          chmax(dp[b][j][k], dp[b][j - 1][k]);
        if (k)
          chmax(dp[b][j][k], dp[b][j][k - 1]);
        if (k && j)
          chmax(dp[b][j][k], dp[b][j - 1][k - 1] + V[i][j - 1]);
        // fprintf(stderr, " %lld", dp[i][j][k]);
      }
      // fprintf(stderr, "\\n");
    }
    // fprintf(stderr, "\\n");
  }
  printf("%lld\\n", dp[!(R & 1)][C][3]);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

typedef long long ll;

bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int R, C, K;
  scanf("%d%d%d", &R, &C, &K);
  int V[R][C + 1];
  fill(V[0], V[R], 0);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    scanf("%d%d%d", &r, &c, &v);
    r--;
    c--;
    V[r][c] = v;
  }
  ll dp[R][C + 1][4];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
        if ((i) && (k == 0))
          chmax(dp[i][j][k], dp[i - 1][j][3]);
        if ((i) && (k == 0))
          chmax(dp[i][j][k], dp[i - 1][j][2] + V[i - 1][j]);
        if (j)
          chmax(dp[i][j][k], dp[i][j - 1][k]);
        if (k)
          chmax(dp[i][j][k], dp[i][j][k - 1]);
        if (k && j)
          chmax(dp[i][j][k], dp[i][j - 1][k - 1] + V[i][j - 1]);
        // fprintf(stderr, " %lld", dp[i][j][k]);
      }
      // fprintf(stderr, "\\n");
    }
    // fprintf(stderr, "\\n");
  }
  printf("%lld\\n", dp[R - 1][C][3]);
  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main(void) {
  int r, c, t;
  cin >> r >> c >> t;
  fill(v[0], v[r], 0);
  rep(i, t) {
    int a, b;
    cin >> a >> b;
    cin >> v[a - 1][b - 1];
  }
  fill(dp[0][0], dp[r][0], 0);
  if (v[0][0] > 0)
    dp[0][0][2] += v[0][0];
  rep(i, r) {
    rep(j, c) {
      ll val = v[i][j];
      rep(k, 4) {
        if (i > 0) {
          dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
          dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
  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;
using P = pair<int, int>;

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

int main(void) {
  int r, c, t;
  cin >> r >> c >> t;
  fill(v[0], v[r + 1], 0);
  rep(i, t) {
    int a, b;
    cin >> a >> b;
    cin >> v[a - 1][b - 1];
  }
  fill(dp[0][0], dp[r][0], 0);
  if (v[0][0] > 0)
    dp[0][0][2] += v[0][0];
  rep(i, r) {
    rep(j, c) {
      ll val = v[i][j];
      rep(k, 4) {
        if (i > 0) {
          dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
          dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll V[3002][3002];

ll MEMO[3002][3002][4];
ll R, C, K;

int main() {
  cin >> R >> C >> K;
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      V[i][j] == 0;
      for (int k = 0; k <= 3; k++) {
        MEMO[i][j][k] = 0;
      }
    }
  }

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

  for (int r = R; r >= 1; r--) {
    for (int c = C; c >= 1; c--) {
      if (V[r][c] == 0) {
        MEMO[r][c][3] = max(MEMO[r][c + 1][3], MEMO[r + 1][c][3]);
        MEMO[r][c][2] = max(MEMO[r][c + 1][2], MEMO[r + 1][c][3]);
        MEMO[r][c][1] = max(MEMO[r][c + 1][1], MEMO[r + 1][c][3]);
        MEMO[r][c][0] = max(MEMO[r][c + 1][0], MEMO[r + 1][c][3]);
      } else {
        MEMO[r][c][3] =
            max(MEMO[r][c + 1][3],
                V[r][c] + max(MEMO[r][c + 1][2], MEMO[r + 1][c][3]));
        MEMO[r][c][2] =
            max(MEMO[r][c + 1][2],
                V[r][c] + max(MEMO[r][c + 1][1], MEMO[r + 1][c][3]));
        MEMO[r][c][1] =
            max(MEMO[r][c + 1][1],
                V[r][c] + max(MEMO[r][c + 1][0], MEMO[r + 1][c][3]));
        MEMO[r][c][0] = max(MEMO[r][c + 1][0], MEMO[r + 1][c][3]);
      }
    }
  }

  cout << MEMO[1][1][3] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll V[3001][3001];
ll R, C, K;

ll value(ll r, ll c, ll v, int left) {
  if (r > R || c > C) {
    return v;
  }
  if (left == 0) {
    return max(value(r, c + 1, v, left), value(r + 1, c, v, 3));
  }
  if (V[r][c] == 0) {
    return max(value(r, c + 1, v, left), value(r + 1, c, v, 3));
  }
  return max(
      max(value(r, c + 1, v, left), value(r, c + 1, v + V[r][c], left - 1)),
      value(r + 1, c, v + V[r][c], 3));
}

int main() {
  cin >> R >> C >> K;
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      V[i][j] == 0;
    }
  }

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

  cout << value(1, 1, 0, 3);
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const int LMT = 3010;
ll dp[LMT][LMT][4];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll cl[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++)
    for (int j = 0; j < c + 1; j++) {
      cl[i][j] = 0;
    }
  ll x, y, z;
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> z;
    cl[x][y] = z;
  }
  for (int i = 0; i < LMT; i++)
    for (int j = 0; j < LMT; j++)
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }

  for (int i = 1; i < r + 1; i++)
    for (int j = 1; j < c + 1; j++)
      for (int k = 0; k < 4; k++) {
        // 前の行のk=3か、前の列からスライド
        if (k == 0)
          dp[i][j][k] = max(dp[i - 1][j][3], dp[i][j - 1][0]);
        // k-1か前の列からスライド
        if (k == 1)
          dp[i][j][k] = max(dp[i][j][0] + cl[i][j], dp[i][j - 1][1]);
        if (k == 2)
          dp[i][j][k] =
              max({dp[i][j][1], dp[i][j - 1][1] + cl[i][j], dp[i][j - 1][2]});
        if (k == 3)
          dp[i][j][k] =
              max({dp[i][j][2], dp[i][j - 1][2] + cl[i][j], dp[i][j - 1][3]});
      }

  cout << dp[r][c][3] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll cl[r + 1][c + 1];
  for (ll i = 0; i < r + 1; i++)
    for (ll j = 0; j < c + 1; j++) {
      cl[i][j] = 0;
    }
  ll x, y, z;
  for (ll i = 0; i < k; i++) {
    cin >> x >> y >> z;
    cl[x][y] = z;
  }
  ll dp[r + 1][c + 1][4];
  for (ll i = 0; i < r + 1; i++)
    for (ll j = 0; j < c + 1; j++)
      for (ll k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }

  cout << dp[r][c][3] << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://kmjp.hatenablog.jp/entry/2020/08/15/1100
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

// グローバル変数で宣言することで0で初期化
// 宝の場所と価値
int T[3030][3030];
// 同一行内での取得済みお宝数(0,1,2,3個)を状態に持たせる
int dp[3030][3030][4];

void solve() {
  int H, W, K;

  cin >> H >> W >> K;
  REP(i, K) {
    int y, x, r;
    cin >> y >> x >> r;
    T[y - 1][x - 1] = r;
  }

  REP(y, H) REP(x, W) {
    // 宝があれば拾ってスコアを加算する
    if (T[y][x] > 0) {
      // 取得済みの宝が2個以下なら拾う
      for (int i = 2; i >= 0; i--)
        dp[y][x][i + 1] = max(dp[y][x][i + 1], dp[y][x][i] + T[y][x]);
    }

    // 取得済みの宝の数に対してx,y方向へ移動処理
    REP(i, 4) {
      // y方向へ移動する場合は、前の行で拾った宝の数を0にリセット
      dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][i]);
      // x方向へ移動する場合は、宝の数が同じ物に対してスコアを更新
      dp[y][x + 1][i] = max(dp[y][x + 1][i], dp[y][x][i]);
    }
  }

  // ゴール地点のスコアが最大値
  cout << dp[H][W - 1][0] << endl;
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc175/tasks/abc175_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

int item[3001][3001];
int R, C, K, ans;

void dfs(int r, int c, vector<vector<int>> v) {
  if (item[r][c] > 0) {
    v[r].emplace_back(item[r][c]);
  }
  if (r == R && c == C) {
    auto cp = v;
    int sum = 0;
    FOR(i, 1, R + 1) {
      sort(ALLR(cp[i]));
      REP(j, min(3UL, cp[i].size())) { sum += cp[i][j]; }
    }
    ans = max(ans, sum);
  }
  if (r + 1 <= R) {
    dfs(r + 1, c, v);
  }
  if (c + 1 <= C) {
    dfs(r, c + 1, v);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> K;
  vector<vector<int>> A(R + 1);
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    item[r][c] = v;
  }
  dfs(1, 1, A);
  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)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll V[3005][3005];
ll dp[3005][3005][4];

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    V[r][c] = v;
  }

  rep(i, R) {
    rep(j, C) {
      for (int k = 3; k > 0; k--) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + V[i][j]);
      }
      rep(k, 4) {
        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]);
      }
    }
  }

  ll ma = 0;
  rep(k, 4) { ma = max(ma, dp[R - 1][C - 1][k]); }
  cout << ma << 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;
typedef long long ll;
typedef pair<int, int> P;

ll V[3005][3005];
ll dp[3005][3005][4];

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    V[r][c] = v;
  }

  rep(i, C) {
    rep(j, K) {
      for (int k = 3; k > 0; k--) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + V[i][j]);
      }
      rep(k, 4) {
        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]);
      }
    }
  }

  ll ma = 0;
  rep(k, 4) { ma = max(ma, dp[C - 1][K - 1][k]); }
  cout << ma << endl;

  return 0;
}