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

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

namespace {
constexpr size_t MX = 3001;
int64_t dp[4][MX][MX];
int64_t values[MX][MX];
} // namespace

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

  for (int64_t k = 0; k < K; ++k) {
    int64_t r, c, v;
    cin >> r >> c >> v;
    values[r - 1][c - 1] = v;
  }

  for (size_t r = 0; r < R; ++r) {
    for (size_t c = 0; c < C; ++c) {
      for (size_t k = 0; k < 4; ++k) {
        dp[k][r][c] = -1;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int64_t r = 0; r < R; ++r) {
    for (int64_t c = 0; c < C; ++c) {
      int64_t value = values[r][c];
      int64_t next = 0;
      for (int64_t k = 3; k > 0; --k) {
        if (value != 0 && dp[k - 1][r][c] >= 0) {
          dp[k][r][c] = max(dp[k][r][c], dp[k - 1][r][c] + value);
        }
        if (c != C - 1)
          dp[k][r][c + 1] = dp[k][r][c];
        next = max(next, dp[k][r][c]);
      }
      if (c != C - 1)
        dp[0][r][c + 1] = max(dp[0][r][c + 1], dp[0][r][c]);
      dp[0][r + 1][c] = max(dp[0][r][c] + value, next);
    }
  }
  cout << dp[0][R][C - 1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

namespace {
constexpr size_t MX = 3001;
int64_t dp[4][MX][MX];
} // namespace

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

  map<pair<int64_t, int64_t>, int64_t> values;
  for (int64_t k = 0; k < K; ++k) {
    int64_t r, c, v;
    cin >> r >> c >> v;
    values[{r - 1, c - 1}] = v;
  }

  for (size_t r = 0; r < R; ++r) {
    for (size_t c = 0; c < C; ++c) {
      for (size_t k = 0; k < 4; ++k) {
        dp[k][r][c] = -1;
      }
    }
  }
  dp[0][0][0] = 0;
  for (int64_t r = 0; r < R; ++r) {
    for (int64_t c = 0; c < C; ++c) {
      int64_t value = values[{r, c}];
      int64_t next = 0;
      for (int64_t k = 3; k > 0; --k) {
        if (value != 0 && dp[k - 1][r][c] >= 0) {
          dp[k][r][c] = max(dp[k][r][c], dp[k - 1][r][c] + value);
        }
        if (c != C - 1)
          dp[k][r][c + 1] = dp[k][r][c];
        next = max(next, dp[k][r][c]);
      }
      if (c != C - 1)
        dp[0][r][c + 1] = max(dp[0][r][c + 1], dp[0][r][c]);
      dp[0][r + 1][c] = max(dp[0][r][c] + value, next);
    }
  }
  cout << dp[0][R][C - 1] << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> P;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const int inf = 1e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll val[3010][3010], dp[3010][3010][5];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  memset(val, 0, sizeof(val));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    val[r][c] = v;
  }
  for (int i = 0; i <= R; ++i) {
    for (int j = 0; j <= C; ++j) {
      for (int k = 0; k <= 3; ++k) {
        if (i > 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] + val[i][j]);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + val[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k <= 3; ++k) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> P;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const int inf = 1e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  ll val[R + 1][C + 1], dp[R + 1][C + 1][4];
  memset(val, 0, sizeof(val));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    cin >> r >> c >> v;
    val[r][c] = v;
  }
  dp[0][0][0] = 0;
  for (int i = 0; i <= R; ++i) {
    for (int j = 0; j <= C; ++j) {
      for (int k = 0; k <= 3; ++k) {
        if (i > 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] + val[i][j]);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + val[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k <= 3; ++k) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define repu(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a) for (int i = 0; i < a; i++)
#define ll long long
using namespace std;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  // long long a[r+1][c+1],b[r+1][c+1][4];
  vector<vector<ll>> a(r + 1, vector<ll>(c + 1, 0LL));
  vector<vector<vector<ll>>> b(r + 1,
                               vector<vector<ll>>(c + 1, vector<ll>(4, 0LL)));
  rep(i, k) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x][y] = z;
  }
  repu(i, 1, r) {
    repu(j, 1, c) {
      b[i][j][3] =
          max({b[i][j - 1][3], b[i][j][3],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][2] + a[i][j]});
      b[i][j][2] =
          max({b[i][j - 1][2], b[i][j][2],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][1] + a[i][j]});
      b[i][j][1] =
          max({b[i][j - 1][1], b[i][j][1],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][0] + a[i][j]});
    }
  }
  // cout<<b[1][4][3]<<endl;
  cout << max({b[r][c][3], b[r][c][2], b[r][c][1], b[r][c][0]}) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define repu(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a) for (int i = 0; i < a; i++)

using namespace std;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  long long ans, a[r + 1][c + 1], b[r + 1][c + 1][4];
  rep(i, r + 1) rep(j, c + 1) {
    a[i][j] = 0;
    rep(k, 4) b[i][j][k] = 0;
  }
  rep(i, k) {
    int x, y, z;
    cin >> x >> y >> z;
    a[x][y] = z;
  }
  repu(i, 1, r) {
    repu(j, 1, c) {
      b[i][j][3] =
          max({b[i][j - 1][3], b[i][j][3],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][2] + a[i][j]});
      b[i][j][2] =
          max({b[i][j - 1][2], b[i][j][2],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][1] + a[i][j]});
      b[i][j][1] =
          max({b[i][j - 1][1], b[i][j][1],
               max({b[i - 1][j][3], b[i - 1][j][1], b[i - 1][j][0]}) + a[i][j],
               b[i][j - 1][0] + a[i][j]});
    }
  }
  // cout<<b[1][4][3]<<endl;
  cout << max({b[r][c][3], b[r][c][2], b[r][c][1], b[r][c][0]}) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;
using ll = long long;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll **field = new ll *[r];
  for (int i = 0; i < r; i++) {
    field[i] = new ll[c];
    for (int j = 0; j < c; j++) {
      field[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int tr, tc, v;
    cin >> tr >> tc >> v;
    field[tr - 1][tc - 1] = v;
  }
  ll ***dp = new ll **[r + 1];
  for (int i = 0; i <= r; i++) {
    dp[i] = new ll *[c + 1];
    for (int j = 0; j <= c; j++) {
      dp[i][j] = new ll[4];
    }
  }
  for (int i = 0; i <= c; i++) {
    for (int j = 0; j < 4; j++) {
      dp[0][i][j] = -1e9;
    }
  }
  for (int i = 0; i <= r; i++) {
    for (int k = 0; k < 4; k++) {
      dp[i][0][k] = -1e9;
    }
  }
  dp[1][1][0] = 0;
  dp[1][1][1] = dp[1][1][2] = dp[1][1][3] = field[0][0];
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (i == 1 && j == 1)
        continue;
      dp[i][j][0] = max(dp[i - 1][j][3], dp[i][j - 1][0]);
      for (int k = 1; k < 4; k++) {
        dp[i][j][k] = max(
            dp[i - 1][j][3] + field[i - 1][j - 1],
            max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + field[i - 1][j - 1]));
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
using ll = long long;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll **field = new ll *[r];
  for (int i = 0; i < r; i++) {
    field[i] = new ll[c];
    for (int j = 0; j < c; j++) {
      field[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int tr, tc, v;
    cin >> tr >> tc >> v;
    field[tr - 1][tc - 1] = v;
  }
  ll **dp = new ll *[r];
  for (int i = 0; i < r; i++) {
    dp[i] = new ll[c];
    for (int j = 0; j < c; j++) {
      dp[i][j] = 0;
    }
  }
  // dp[r-1]の作成
  int a[3];
  for (int i = 0; i < c; i++) {
    a[0] = a[1] = a[2] = 0;
    for (int j = i; j < c; j++) {
      if (field[r - 1][j] > a[0]) {
        a[0] = field[r - 1][j];
        if (a[0] > a[1])
          swap(a[0], a[1]);
        if (a[1] > a[2])
          swap(a[1], a[2]);
      }
    }
    dp[r - 1][i] = a[0] + a[1] + a[2];
  }
  for (int i = r - 2; i >= 0; i--) {
    for (int j = 0; j < c; j++) {
      a[0] = a[1] = a[2] = 0;
      for (int k = j; k < c; k++) {
        if (a[0] < field[i][k]) {
          a[0] = field[i][k];
          if (a[0] > a[1])
            swap(a[0], a[1]);
          if (a[1] > a[2])
            swap(a[1], a[2]);
        }
        dp[i][j] = max(dp[i][j], a[0] + a[1] + a[2] + dp[i + 1][k]);
      }
    }
  }
  cout << dp[0][0] << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3003;

ll dp[maxn][maxn][4];
ll val[maxn][maxn];
int main() {
  int n, m, k, x, y;
  ll v;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%lld", &x, &y, &v);
    val[x][y] = v;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][0]);
        dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][1]);
        dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][2]);
        dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3]);
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
      }
      if (val[i][j])
        for (int k = 3; k; k--)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + val[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[n][m][i]);
  }
  printf("%lld", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3004;
int dir[2][2] = {0, 1, 1, 0};
struct node {
  int c[maxn];
  ll val;
  int x, y;
};
ll a[maxn][maxn];
int n, m, k;
ll bfs() {
  ll ans = 0;
  queue<node> q;
  node c;
  c.x = 1;
  c.y = 1;
  c.val = 0;
  memset(c.c, 0, sizeof(c.c));
  q.push(c);
  if (a[1][1])
    c.val += a[1][1], c.c[1]++;
  q.push(c);
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    for (int i = 0; i <= 1; i++) {
      int x = now.x + dir[i][0], y = now.y + dir[i][1];
      if (x > n || y > m)
        continue;
      if (x == n && y == m) {
        if (now.c[x] < 3)
          now.val += a[n][m];
        ans = max(ans, now.val);
        continue;
      }
      node k;
      memcpy(k.c, now.c, sizeof(k.c));
      k.val = now.val, k.x = x, k.y = y;
      q.push(k);
      if (a[x][y] && k.c[x] < 3) {
        k.c[x]++;
        k.val += 1ll * a[x][y];
        q.push(k);
      }
    }
  }
  return ans;
}
int main() {
  int x, y;
  ll vl;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%lld", &x, &y, &vl);
    a[x][y] = vl;
  }
  printf("%lld", bfs());
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

ll bd[3100][3100], dp[3100][3100][5];

int main(void) {
  int r, c, K;
  cin >> r >> c >> K;

  // ll bd[3100][3100];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      bd[i][j] = 0;
    }
  }

  for (int i = 0; i < K; i++) {
    int nr, nc;
    cin >> nr >> nc;
    ll nval;
    cin >> nval;
    bd[nr][nc] = nval;
  }

  // ll dp[3100][3100][5];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        // 下向きへの遷移(kの値は0にリセットされる)
        if (i - 1 >= 0) {
          // (i, j) で「取らない」
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
          // (i, j)
          // で「取る」(何もないところを0にしておいたので、いつでもとってok)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + bd[i][j]);
        }
        // 右向きへの遷移(kの値によって状態が拡張されている)
        if (j - 1 >= 0) {
          // (i, j) で「取らない」
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          // (i, j) で「取る」
          if (k - 1 >= 0) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + bd[i][j]);
          }
        }
      }
    }
  }

  ll 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;
typedef long long ll;
using P = pair<int, int>;

int main(void) {
  int r, c, K;
  cin >> r >> c >> K;

  ll bd[3100][3100];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      bd[i][j] = 0;
    }
  }

  for (int i = 0; i < K; i++) {
    int nr, nc;
    cin >> nr >> nc;
    ll nval;
    cin >> nval;
    bd[nr][nc] = nval;
  }

  ll dp[3100][3100][5];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        // 下向きへの遷移(kの値は0にリセットされる)
        if (i - 1 >= 0) {
          // (i, j) で「取らない」
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
          // (i, j)
          // で「取る」(何もないところを0にしておいたので、いつでもとってok)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + bd[i][j]);
        }
        // 右向きへの遷移(kの値によって状態が拡張されている)
        if (j - 1 >= 0) {
          // (i, j) で「取らない」
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          // (i, j) で「取る」
          if (k - 1 >= 0) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + bd[i][j]);
          }
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= 3; i++) {
    ans = max(ans, dp[r][c][i]);
  }
  cout << ans << endl;

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

#include "bits/stdc++.h"

#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)

using namespace std;
typedef pair<int, int> pii;

ll dp[3001][3001][4];
ll g[3001][3001];
int r, c, k;

ll memo(int row, int col, int items) {
  if (dp[row][col][items] != -1)
    return dp[row][col][items];

  ll best = pow(-10, 13);
  if (row < r - 1) {
    best = (items < 3) ? g[row][col] + max(best, memo(row + 1, col, 0))
                       : max(best, memo(row + 1, col, 0));
  }

  if (col < c - 1) {
    best = max(best, memo(row, col + 1, items));
    if (g[row][col] != 0 && items < 3)
      best = max(best, g[row][col] + memo(row, col + 1, items + 1));
  }

  return dp[row][col][items] = best;
}

int main() {
  fastIO;
  cin >> r >> c >> k;
  memset(g, 0, sizeof(g));
  memset(dp, -1LL, sizeof(dp));

  for (int i = 0; i < k; i++) {
    int ri, ci, ki;
    cin >> ri >> ci >> ki;
    ri--, ci--;
    g[ri][ci] = ki;
  }

  dp[r - 1][c - 1][0] = dp[r - 1][c - 1][1] = dp[r - 1][c - 1][2] =
      g[r - 1][c - 1];
  dp[r - 1][c - 1][3] = 0;

  cout << memo(0, 0, 0);
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)

using namespace std;
typedef pair<int, int> pii;

ll max4(ll m1, ll m2, ll m3, ll m4) { return max(max(m1, m2), max(m3, m4)); }

ll max3(ll m1, ll m2, ll m3) { return max(max(m1, m2), m3); }

int main() {
  fastIO;
  int r, c, k;
  cin >> r >> c >> k;

  ll g[r][c];
  memset(g, 0, sizeof(g));

  for (int i = 0; i < k; i++) {
    int ri, ci, ki;
    cin >> ri >> ci >> ki;
    g[ri - 1][ci - 1] = ki;
  }

  ll dp[r][c][4];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = dp[i][j][3] = pow(-10, 15);
    }
  }

  dp[r - 1][c - 1][0] = 0;
  dp[r - 1][c - 1][1] = g[r - 1][c - 1];

  for (int i = r - 1; i >= 0; i--) {
    for (int j = c - 1; j >= 0; j--) {
      if (i < r - 1) {
        dp[i][j][0] = g[i][j] + max4(dp[i + 1][j][0], dp[i + 1][j][1],
                                     dp[i + 1][j][2], dp[i + 1][j][3]);
      }
      if (j < c - 1) {
        dp[i][j][0] = max(dp[i][j][0], dp[i][j + 1][0]);
        dp[i][j][1] = max(g[i][j] + dp[i][j + 1][0], dp[i][j + 1][1]);
        dp[i][j][2] = max(g[i][j] + dp[i][j + 1][1], dp[i][j + 1][2]);
        dp[i][j][3] = dp[i][j + 1][3];
      }
    }
  }

  cout << max4(dp[0][0][0], dp[0][0][1], dp[0][0][2], dp[0][0][3]);
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

/* ****GT_18**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 3005

ll r, c, k;
// map<pii,ll> m;
ll dp[N][N][4];
ll vals[N][N];

ll solve(ll i, ll j, ll left) {
  if (i < 1 or j < 1)
    return 0;
  if (dp[i][j][left] != -1)
    return dp[i][j][left];
  if (vals[i][j] == 0) {
    return dp[i][j][left] = max(solve(i - 1, j, 3), solve(i, j - 1, left));
  }
  ll ifchoose = 0;
  if (left > 0)
    ifchoose = max(solve(i - 1, j, 3), solve(i, j - 1, left - 1)) + vals[i][j];
  ll ifnotchoose = max(solve(i - 1, j, 3), solve(i, j - 1, left));
  return dp[i][j][left] = max(ifchoose, ifnotchoose);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  rep(i, 0, N) rep(j, 0, N) rep(z, 0, 4) dp[i][j][z] = -1;
  while (TESTS--) {
    cin >> r >> c >> k;
    rep(i, 0, k) {
      ll x, y, z;
      cin >> x >> y >> z;
      // m[{x,y}]=z;
      vals[x][y] = z;
    }
    cout << solve(r, c, 3) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/* ****GT_18**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 3005

ll r, c, k;
map<pii, ll> m;
ll dp[N][N][4];

ll solve(ll i, ll j, ll left) {
  if (i < 1 or j < 1)
    return 0;
  if (dp[i][j][left] != -1)
    return dp[i][j][left];
  if (!m[{i, j}]) {
    return dp[i][j][left] = max(solve(i - 1, j, 3), solve(i, j - 1, left));
  }
  ll ifchoose = 0;
  if (left > 0)
    ifchoose = max(solve(i - 1, j, 3), solve(i, j - 1, left - 1)) + m[{i, j}];
  ll ifnotchoose = max(solve(i - 1, j, 3), solve(i, j - 1, left));
  return dp[i][j][left] = max(ifchoose, ifnotchoose);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  rep(i, 0, N) rep(j, 0, N) rep(z, 0, 4) dp[i][j][z] = -1;
  while (TESTS--) {
    cin >> r >> c >> k;
    rep(i, 0, k) {
      ll x, y, z;
      cin >> x >> y >> z;
      m[{x, y}] = z;
    }
    cout << solve(r, c, 3) << endl;
  }
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

ll g[3002][3002]{};
ll a[3002][3002][4]{};

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    g[r][c] = v;
  }

  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
        a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
        if (k == 3) {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
        } else {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
          a[i][j + 1][k + 1] =
              max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
        }
      }
    }
  }
  ll best = 0;
  for (int i = 0; i <= 3; i++) {
    best = max(best, a[R][C][i]);
  }
  cout << best << "\\n";
  return 0;
}

Error: -11

Fixed submission:

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

ll g[3001][3001]{};
ll a[3001][3001][4]{};

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    g[r][c] = v;
  }

  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
        a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
        if (k == 3) {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
        } else {
          a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
          a[i][j + 1][k + 1] =
              max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
        }
      }
    }
  }
  ll best = 0;
  for (int i = 0; i <= 3; i++) {
    best = max(best, a[R][C][i]);
  }
  cout << best << "\\n";
  return 0;
}