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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(I, J, K) for (ll I = J; I < K; I++)

ll dp[3001][3001][4];
int item[3001][3001];
int main() {

  ll r, c, k;
  cin >> r >> c >> k;

  rep(i, 0, k) {
    ll a, b, x;
    cin >> a >> b >> x;
    item[a][b] = x;
  }

  rep(i, 0, c + 1) {   // 列
    rep(j, 0, r + 1) { // 行
      if (j) {
        dp[j][i][0] = max({dp[j - 1][i][0], dp[j - 1][i][1], dp[j - 1][i][2],
                           dp[j - 1][i][3]});
        dp[j][i][1] = max({dp[j - 1][i][0], dp[j - 1][i][1], dp[j - 1][i][2],
                           dp[j - 1][i][3]}) +
                      item[j][i];
      }
      if (i) {
        dp[j][i][0] = max(dp[j][i][0], dp[j][i - 1][0]);
        dp[j][i][1] =
            max({dp[j][i][1], dp[j][i - 1][0] + item[j][i], dp[j][i - 1][1]});
        dp[j][i][2] =
            max({dp[j][i][2], dp[j][i - 1][1] + item[j][i], dp[j][i - 1][2]});
        dp[j][i][3] =
            max({dp[j][i][3], dp[j][i - 1][2] + item[j][i], dp[j][i - 1][3]});
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[r][c][i], ans);
  cout << ans << endl;
}


Error: -11

Fixed submission:

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

int main() {

  ll r, c, k;
  cin >> r >> c >> k;

  ll dp[3010][3010][4];
  int item[3010][3010];
  rep(i, 0, 3010) rep(j, 0, 3010) rep(x, 0, 4) dp[i][j][x] = 0;
  rep(i, 0, 3010) rep(j, 0, 3010) item[i][j] = 0;

  rep(i, 0, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    item[a][b] = c;
  }

  rep(i, 0, c + 1) {   // 列
    rep(j, 0, r + 1) { // 行
      if (j) {
        dp[j][i][0] = max({dp[j - 1][i][0], dp[j - 1][i][1], dp[j - 1][i][2],
                           dp[j - 1][i][3]});
        dp[j][i][1] = max({dp[j - 1][i][0], dp[j - 1][i][1], dp[j - 1][i][2],
                           dp[j - 1][i][3]}) +
                      item[j][i];
      }
      if (i) {
        dp[j][i][0] = max(dp[j][i][0], dp[j][i - 1][0]);
        dp[j][i][1] =
            max({dp[j][i][1], dp[j][i - 1][0] + item[j][i], dp[j][i - 1][1]});
        dp[j][i][2] =
            max({dp[j][i][2], dp[j][i - 1][1] + item[j][i], dp[j][i - 1][2]});
        dp[j][i][3] =
            max({dp[j][i][3], dp[j][i - 1][2] + item[j][i], dp[j][i - 1][3]});
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[r][c][i], ans);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define ll long long
ll arr[3005][3005];
ll dp[3005][3005][4];
ll n;
ll m;
ll k;
ll solve(ll i, ll j, ll p) {
  if (dp[i][j][p] != -1)
    return dp[i][j][p];
  if (i > n || j > m)
    dp[i][j][p] = 0;
  else if (p == 3)
    dp[i][j][p] = solve(i + 1, j, 0);
  else if (j == m && i == n)
    dp[i][j][p] = arr[i][j];
  else if (j == m)
    dp[i][j][p] = arr[i][j] + solve(i + 1, j, 0);
  else if (i == n)
    dp[i][j][p] = max(arr[i][j] + solve(i, j + 1, p + 1), solve(i, j + 1, p));
  else {
    ll a, b, c, d;
    a = arr[i][j] + solve(i + 1, j, 0);
    b = arr[i][j] + solve(i, j + 1, p + 1);
    c = solve(i, j + 1, p);
    dp[i][j][p] = max(a, max(b, c));
  }
  return dp[i][j][p];
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int p = 0; p <= 3; p++)
        dp[i][j][p] = -1;
  while (k--) {
    ll i, j, v;
    cin >> i >> j >> v;
    arr[i][j] = v;
  }
  cout << solve(1, 1, 0) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define ll long long
ll arr[3001][3001];
ll dp[3001][3001][4];
ll n;
ll m;
ll k;
ll solve(ll i, ll j, ll p) {
  if (dp[i][j][p] != 0)
    return dp[i][j][p];
  if (i > n || j > m)
    return 0;
  else if (p == 3)
    dp[i][j][p] = solve(i + 1, j, 0);
  else if (j == m && i == n)
    dp[i][j][p] = arr[i][j];
  else if (j == m)
    dp[i][j][p] = arr[i][j] + solve(i + 1, j, 0);
  else if (i == n)
    dp[i][j][p] = max(arr[i][j] + solve(i, j + 1, p + 1), solve(i, j + 1, p));
  else {
    ll a, b, c, d;
    a = arr[i][j] + solve(i + 1, j, 0);
    b = arr[i][j] + solve(i, j + 1, p + 1);
    c = solve(i, j + 1, p);
    dp[i][j][p] = max(a, max(b, c));
  }
  return dp[i][j][p];
}
int main() {

  cin >> n >> m >> k;
  while (k--) {
    ll i, j, v;
    cin >> i >> j >> v;
    arr[i][j] = v;
  }
  cout << solve(1, 1, 0) << 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;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)

int a[3005][3005];
ll dp[3005][3005][4];

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

  rep(i, R) rep(j, C) {
    for (int k = 2; k >= 0; k--) {
      chmax(dp[i][j][k + 1], dp[i][j][k] + a[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;
  return 0;
}


Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3000
#define max(a, b) ((a > b) ? (a) : (b))

typedef long long unsigned int int64;

int main() {
  int r, c, k;
  int n, i, j;
  int rn, cn;
  int v;
  int64 ans = 0;
  int value[SIZE + 10][SIZE + 10] = {0};
  int64 dp[SIZE + 10][SIZE + 10][4] = {0};

  scanf("%d %d %d", &r, &c, &k);

  for (i = 0; i < k; i++) {
    scanf("%d %d %d", &rn, &cn, &v);
    value[rn][cn] = v;
  }

  for (i = 0; i <= r; i++) {
    for (j = 0; j <= c; j++) {
      for (n = 2; n >= 0; n--) // 拾う場合の遷移
      {
        dp[i][j][n + 1] =
            max(dp[i][j][n + 1], dp[i][j][n] + (int64)value[i][j]);
      }
      for (n = 0; n < 4; n++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][n]);
        dp[i][j + 1][n] = max(dp[i][j + 1][n], dp[i][j][n]);
      }
    }
  }
  for (n = 0; n < 4; n++) {
    if (ans < dp[r][c][n])
      ans = dp[r][c][n];
  }

  printf("%lld", ans);
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005
ll a[3100][3100];
ll dp[3100][3100][4];
int main() {
  int h, w, K;
  cin >> h >> w >> K;

  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, K) {
    int x, y, v;
    cin >> x >> y >> v;
    x--;
    y--;
    a[x][y] = v;
  }

  rep(i, h) rep(j, w) rep(k, 4) dp[i][j][k] = 0;
  dp[0][0][1] = a[0][0];
  rep(i, h) {
    rep(j, w) {
      if (i > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
      if (j > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[h - 1][w - 1][k]);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005

int main() {
  int h, w, K;
  cin >> h >> w >> K;
  ll a[h][w];
  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, K) {
    int x, y, v;
    cin >> x >> y >> v;
    x--;
    y--;
    a[x][y] = v;
  }
  ll dp[h][w][4];
  rep(i, h) rep(j, w) rep(k, 4) dp[i][j][k] = 0;
  dp[0][0][1] = a[0][0];
  rep(i, h) {
    rep(j, w) {
      if (i > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
      if (j > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[h - 1][w - 1][k]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005
ll dp[3100][3100][4];
int main() {
  ll a[3100][3100];

  int h, w, K;
  cin >> h >> w >> K;

  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, K) {
    int x, y, v;
    cin >> x >> y >> v;
    x--;
    y--;
    a[x][y] = v;
  }

  rep(i, h) rep(j, w) rep(k, 4) dp[i][j][k] = 0;
  dp[0][0][1] = a[0][0];
  rep(i, h) {
    rep(j, w) {
      if (i > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
      if (j > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[h - 1][w - 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)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005

int main() {
  ll a[3100][3100];
  ll dp[3100][3100][4];
  int h, w, K;
  cin >> h >> w >> K;

  rep(i, h) rep(j, w) a[i][j] = 0;
  rep(i, K) {
    int x, y, v;
    cin >> x >> y >> v;
    x--;
    y--;
    a[x][y] = v;
  }

  rep(i, h) rep(j, w) rep(k, 4) dp[i][j][k] = 0;
  dp[0][0][1] = a[0][0];
  rep(i, h) {
    rep(j, w) {
      if (i > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
      if (j > 0) {
        rep(k, 4) {
          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] + a[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[h - 1][w - 1][k]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int R, C, K;
int arr[3300][3300];
ll dp[3300][3300][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> K;
  for (int i = 0; i < K; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[a][b] = c;
  }

  for (int i = 1; i <= R; i++)
    for (int j = 1; j <= C; j++)
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (arr[i][j] == 0) {
          if (k == 0) {
            for (int l = 0; l < 4; l++)
              dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][l]);
          }
        } else {
          if (k > 0)
            dp[i][j][k] = max(dp[i][j][k], arr[i][j] + dp[i][j - 1][k - 1]);
          if (k == 1) {
            for (int l = 0; l < 4; l++)
              dp[i][j][k] = max(dp[i][j][k], arr[i][j] + dp[i - 1][j][l]);
          }
        }
      }

  ll ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[R][C][i]);
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int R, C, K;
vector<pll> v[3300];
vector<int> valid_row;

ll cache[3300][3300][3];

ll solve(int row, int col, int cnt) {
  if (row == valid_row.size())
    return 0;
  ll &ret = cache[row][col][cnt];
  if (ret != -1)
    return ret;

  ret = solve(row + 1, col, 0);
  int idx = lower_bound(all(v[valid_row[row]]), pll(col, 0)) -
            v[valid_row[row]].begin();
  for (int i = idx; i < v[valid_row[row]].size(); i++) {
    if (v[valid_row[row]][i].fr >= col) {
      if (cnt < 2)
        ret = max(ret, v[valid_row[row]][i].sc +
                           solve(row, v[valid_row[row]][i].fr + 1, cnt + 1));
      ret = max(ret, v[valid_row[row]][i].sc +
                         solve(row + 1, v[valid_row[row]][i].fr, 0));
    }
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> K;
  for (int i = 0; i < K; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a].push_back({b, c});
  }
  for (int i = 0; i <= 3000; i++) {
    sort(all(v[i]));
    if (v[i].size())
      valid_row.push_back(i);
  }

  memset(cache, -1, sizeof(cache));
  cout << solve(0, 0, 0) << '\\n';
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using i64 = int64_t;
#define mp make_pair

i64 dp[3005][3005][4];
i64 v[3005][3005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int r, c, k;
  cin >> r >> c >> k;
  map<pair<int, int>, i64> item;
  for (int i = 0; i < k; i++) {
    int rr, cc;
    i64 vv;
    cin >> rr >> cc >> vv;
    v[rr][cc] = vv;
  }

  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        for (int l = 1; l <= 3; l++)
          dp[i + 1][j][l] = max(dp[i + 1][j][l], dp[i][j][k] + v[i + 1][j]);
        for (int l = k + 1; l <= 3; l++)
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][k] + v[i][j + 1]);
      }
    }
  }
  // for (int k = 0; k <= 3; k++)
  // {
  //     for (int i = 0; i <= r; i++)
  //     {
  //         for (int j = 0; j <= c; j++)
  //         {
  //             cerr << dp[i][j][k] << " ";
  //         }
  //         cerr << endl;
  //     }
  //     cerr << "---" << endl;
  // }
  i64 ans = 0;
  for (int i = 0; i <= 3; i++)
    ans = max(ans, dp[r][c][i]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using i64 = int64_t;
#define mp make_pair

i64 dp[3005][3005][4];
i64 v[3005][3005];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  map<pair<int, int>, i64> item;
  for (int i = 0; i < k; i++) {
    int rr, cc;
    i64 vv;
    cin >> rr >> cc >> vv;
    v[rr][cc] = vv;
  }

  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        for (int l = 1; l <= 3; l++)
          dp[i + 1][j][l] = max(dp[i + 1][j][l], dp[i][j][k] + v[i + 1][j]);
        for (int l = k + 1; l <= 3; l++)
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][k] + v[i][j + 1]);
      }
    }
  }
  for (int k = 0; k <= 3; k++) {
    for (int i = 0; i <= r; i++) {
      for (int j = 0; j <= c; j++) {
        cerr << dp[i][j][k] << " ";
      }
      cerr << endl;
    }
    cerr << "---" << endl;
  }
  i64 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: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, k;
ll mp[3005][3005];
ll dp[3005][3005][4];
ll dfs(int i, int j, int t) {
  if (i < 1 || i > n || j < 1 || j > m || t > 3)
    return -1e18;
  if (dp[i][j][t] != -1)
    return dp[i][j][t];
  // printf("//%d %d %d\\n",i,j,t);
  if (i == n && j == m) {
    if (t < 3)
      return mp[i][j];
    return 0;
  }
  ll ans = 0;
  ans = max(ans, max(dfs(i + 1, j, 0), dfs(i, j + 1, t)));
  ans = max(ans, mp[i][j] + dfs(i, j + 1, t + 1));
  if (t < 3)
    ans = max(ans, mp[i][j] + dfs(i + 1, j, 0));

  return dp[i][j][t] = ans;
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d%d", &n, &m, &k);
  while (k--) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    mp[x][y] = v;
  }
  printf("%lld\\n", dfs(1, 1, 0));
  //    for(int k=0;k<=3;k++)
  //    {
  //        printf("k=%d\\n",k);
  //        for(int i=1;i<=n;i++)
  //        {
  //            for(int j=1;j<=m;j++)
  //                printf("%d ",dp[i][j][k]);
  //                puts("");
  //
  //        }
  //
  //            puts("");
  //
  //    }
  return 0;
}
/*
1 4 4
1 1 1
1 2 2
1 3 3
1 4 4
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, k;
map<pair<int, int>, ll> mp;
ll dp[3005][3005][4];
ll dfs(int i, int j, int t) {
  if (i < 1 || i > n || j < 1 || j > m || t > 3)
    return -1e18;
  if (dp[i][j][t] != -1)
    return dp[i][j][t];
  // printf("//%d %d %d\\n",i,j,t);
  if (i == n && j == m) {
    if (t < 3)
      return mp[make_pair(i, j)];
    return 0;
  }
  ll ans = 0;
  ans = max(ans, max(dfs(i + 1, j, 0), dfs(i, j + 1, t)));
  if (mp.count(make_pair(i, j))) {
    ans = max(ans, mp[make_pair(i, j)] + dfs(i, j + 1, t + 1));
    if (t < 3)
      ans = max(ans, mp[make_pair(i, j)] + dfs(i + 1, j, 0));
  }
  return dp[i][j][t] = ans;
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d%d", &n, &m, &k);
  while (k--) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    mp[make_pair(x, y)] = v;
  }
  printf("%lld\\n", dfs(1, 1, 0));
  //    for(int k=0;k<=3;k++)
  //    {
  //        printf("k=%d\\n",k);
  //        for(int i=1;i<=n;i++)
  //        {
  //            for(int j=1;j<=m;j++)
  //                printf("%d ",dp[i][j][k]);
  //                puts("");
  //
  //        }
  //
  //            puts("");
  //
  //    }
  return 0;
}
/*
1 4 4
1 1 1
1 2 2
1 3 3
1 4 4
*/
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

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

#include <iostream>

using namespace std;
using ll = long long;
const ll LINF = 1e18;

template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};
ll dp[3000][3000][4];
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll a[r][c];
  fill(a[0], a[r], 0);
  for (int i = 0; i < k; i++) {
    ll f, g, h;
    cin >> f >> g >> h;
    f--;
    g--;
    a[f][g] = h;
  }

  fill(dp[0][0], dp[r][0], -LINF);
  dp[0][0][0] = 0;
  dp[0][0][1] = a[0][0];

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++) {
        if (i + 1 < r) {
          chmax(dp[i + 1][j][0], dp[i][j][k]); // 下の行に移る
          chmax(dp[i + 1][j][1], dp[i][j][k] + a[i + 1][j]); // 下の行に移る
        }
        if (j + 1 < c) {
          chmax(dp[i][j + 1][k], dp[i][j][k]); // 右に移る
          if (k + 1 < 4) {
            chmax(dp[i][j + 1][k + 1], dp[i][j][k] + a[i][j + 1]);
          }
        }
      }
    }
  }

  ll an = 0;
  for (int i = 0; i < 4; i++) {
    an = max(an, dp[r - 1][c - 1][i]);
  }
  cout << an << endl;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;
using ll = long long;
const ll LINF = 1e18;

template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll a[r][c];
  fill(a[0], a[r], 0);
  for (int i = 0; i < k; i++) {
    ll f, g, h;
    cin >> f >> g >> h;
    f--;
    g--;
    a[f][g] = h;
  }

  ll dp[r][c][4];
  fill(dp[0][0], dp[r][0], -LINF);
  dp[0][0][0] = 0;
  dp[0][0][1] = a[0][0];

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++) {
        if (i + 1 < r) {
          chmax(dp[i + 1][j][0], dp[i][j][k]); // 下の行に移る
          chmax(dp[i + 1][j][1], dp[i][j][k] + a[i + 1][j]); // 下の行に移る
        }
        if (j + 1 < c) {
          chmax(dp[i][j + 1][k], dp[i][j][k]); // 右に移る
          if (k + 1 < 4) {
            chmax(dp[i][j + 1][k + 1], dp[i][j][k] + a[i][j + 1]);
          }
        }
      }
    }
  }

  ll an = 0;
  for (int i = 0; i < 4; i++) {
    an = max(an, dp[r - 1][c - 1][i]);
  }
  cout << an << endl;
}