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

#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 3005;

int R, C, k, r, c, v, dp[maxn][maxn][4], val[maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    val[r][c] = v;
  }
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++) {
        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]);
        if (k < 3) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
        }
      }
  int ans = 0;
  for (int k = 0; k < 4; k++)
    ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
  cout << ans << "\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 3005;

int R, C, k, r, c, v, dp[4][maxn][maxn], val[maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C >> k;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    val[r][c] = v;
  }
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < 4; k++) {
        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]);
        if (k < 3) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
        }
      }
  int ans = 0;
  for (int k = 0; k < 4; k++)
    ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
  cout << ans << "\\n";
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

ll a[3010][3010], dp[2][3010][4];

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < t; i++) {
    int x, y, v;
    cin >> y >> x >> v;
    a[y - 1][x - 1] = v;
  }

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = -1e18;
    }
  dp[0][0][0] = 0;
  if (a[0][0])
    dp[0][0][1] = a[0][0];
  int p = 0, q = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i == 0 && j == 0)
        continue;
      if (j) {
        for (int k = 0; k < 4; k++)
          dp[p][j][k] = max(dp[p][j][k], dp[p][j - 1][k]);
        if (a[i][j])
          for (int k = 1; k < 4; k++)
            dp[p][j][k] = max(dp[p][j][k], dp[p][j - 1][k - 1] + a[i][j]);
      }
      if (i) {
        for (int k = 0; k < 4; k++)
          dp[p][j][0] = max(dp[p][j][0], dp[q][j][k]);
        if (a[i][j])
          for (int k = 0; k < 4; k++)
            dp[p][j][1] = max(dp[p][j][1], dp[q][j][k] + a[i][j]);
      }
    }
    for (int j = 0; j < m; j++)
      for (int k = 0; k < 4; k++)
        dp[(i + 1) % 2][j][k] = -1e18;
    swap(p, q);
  }

  cout << max(dp[q][m - 1][0],
              max(dp[q][m - 1][1], max(dp[q][m - 1][2], dp[q][m - 1][3])))
       << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

ll a[3010][3010], dp[3010][3010][4];

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < t; i++) {
    int x, y, v;
    cin >> y >> x >> v;
    a[y - 1][x - 1] = v;
  }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < t; k++)
        dp[i][j][k] = -1e18;
    }
  dp[0][0][0] = 0;
  if (a[0][0])
    dp[0][0][1] = a[0][0];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i == 0 && j == 0)
        continue;
      if (j) {
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (a[i][j])
          for (int k = 1; k < 4; k++)
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
      }
      if (i) {
        for (int k = 0; k < 4; k++)
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (a[i][j])
          for (int k = 0; k < 4; k++)
            dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a[i][j]);
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;

ll r, c, k = 0;
ll mp[3005][3005] = {};

ll dp[3005][3005][4];
bool v[3005][3005][4] = {};

ll solve(ll x, ll y, ll items) {
  if (v[x][y][items]) {
    return dp[x][y][items];
  }
  if (x > c || y > r)
    return 0;

  ll ret = 0;
  ret = max(ret, solve(x + 1, y, items));
  ret = max(ret, solve(x, y + 1, 0));
  if (items < 3 && mp[x][y] != 0) {
    ret = max(ret, solve(x + 1, y, items + 1) + mp[x][y]);
    ret = max(ret, solve(x, y + 1, 0) + mp[x][y]);
  }
  v[x][y][items] = true;
  return dp[x][y][items] = ret;
}

int main() {
  cin >> r >> c >> k;
  ll ri, ci, vi;
  rep(i, k) {
    cin >> ri >> ci >> vi;
    mp[ci][ri] = vi;
  }
  cout << solve(1, 1, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;

ll r, c, k = 0;
ll mp[3005][3005] = {};

ll dp[3005][3005][4];
bool v[3005][3005][4] = {};

ll solve(ll x, ll y, ll items) {
  if (v[x][y][items]) {
    return dp[x][y][items];
  }
  if (x > c || y > r)
    return 0;

  vector<ll> transition;
  transition.push_back(solve(x + 1, y, items));
  transition.push_back(solve(x, y + 1, 0));
  if (items < 3 && mp[x][y] != 0) {
    transition.push_back(solve(x + 1, y, items + 1) + mp[x][y]);
    transition.push_back(solve(x, y + 1, 0) + mp[x][y]);
  }
  v[x][y][items] = true;
  return dp[x][y][items] = *max_element(transition.begin(), transition.end());
}

int main() {
  cin >> r >> c >> k;
  ll ri, ci, vi;
  rep(i, k) {
    cin >> ri >> ci >> vi;
    mp[ci][ri] = vi;
  }
  cout << solve(1, 1, 0) << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

ll dp[3005][3005][5];

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

  vec<vec<int>> mp(R + 1, vec<int>(C + 1, 0));
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    mp[r][c] = v;
  }

  for (int i = 1; i <= R; ++i) {
    for (int j = 1; j <= C; ++j) {
      dp[i][j][0] = max({dp[i][j][0], dp[i][j - 1][0]});
      dp[i][j][1] = max({dp[i][j][1], dp[i][j - 1][1]});
      dp[i][j][2] = max({dp[i][j][2], dp[i][j - 1][2]});
      dp[i][j][3] = max({dp[i][j][3], dp[i][j - 1][3]});

      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]});
        dp[i][j][2] = max({dp[i][j][2], dp[i - 1][j][k]});
        dp[i][j][3] = max({dp[i][j][3], dp[i - 1][j][k]});
      }

      if (mp[i][j] != 0) {
        dp[i][j][3] = max({dp[i][j][3], dp[i][j][2] + mp[i][j]});
        dp[i][j][2] = max({dp[i][j][2], dp[i][j][1] + mp[i][j]});
        dp[i][j][1] = max({dp[i][j][1], dp[i][j][0] + mp[i][j]});
      }
    }
  }

  cout << max({dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3]}) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

ll dp[3005][3005][5];

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

  map<pair<ll, ll>, ll> mp;
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    mp[{r, c}] = v;
  }

  for (int i = 1; i <= R; ++i) {
    for (int j = 1; j <= C; ++j) {
      dp[i][j][0] = max({dp[i][j][0], dp[i][j - 1][0]});
      dp[i][j][1] = max({dp[i][j][1], dp[i][j - 1][1]});
      dp[i][j][2] = max({dp[i][j][2], dp[i][j - 1][2]});
      dp[i][j][3] = max({dp[i][j][3], dp[i][j - 1][3]});

      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]});
        dp[i][j][2] = max({dp[i][j][2], dp[i - 1][j][k]});
        dp[i][j][3] = max({dp[i][j][3], dp[i - 1][j][k]});
      }

      if (mp[{i, j}] != 0) {
        dp[i][j][3] = max({dp[i][j][3], dp[i][j][2] + mp[{i, j}]});
        dp[i][j][2] = max({dp[i][j][2], dp[i][j][1] + mp[{i, j}]});
        dp[i][j][1] = max({dp[i][j][1], dp[i][j][0] + mp[{i, j}]});
      }
    }
  }

  cout << max({dp[R][C][0], 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>
using namespace std;

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

// code starts
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<int> h(k);
  vector<int> w(k);
  vector<int> v(k);
  int i, j, l;
  rep(i, k) cin >> h[i] >> w[i] >> v[i];
  vector<vector<int>> val(r, vector<int>(c, 0));
  rep(i, k) { val[h[i] - 1][w[i] - 1] = v[i]; }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  rep(i, r) rep(j, c) rep(l, 4) {
    if (l + 1 < 4) {
      if (j + 1 < c)
        dp[i][j + 1][l + 1] = max(dp[i][j + 1][l + 1], dp[i][j][l] + val[i][j]);
      if (i + 1 < r)
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l] + val[i][j]);
    }
    if (j + 1 < c)
      dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
    if (i + 1 < r)
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
  }
  ll ans = 0;
  rep(l, 4) {
    if (l < 3)
      ans = max(ans, dp[r - 1][c - 1][l] + val[r - 1][c - 1]);
    else
      ans = max(ans, dp[r - 1][c - 1][l]);
  }
  /*rep(i,r)
  {
    rep(j,c)
    {
      cout << '(';
      rep(l,3)
      {
        cout << dp[i][j][l] << ' ';
      }
      cout << ") ";
    }
    cout << endl;
  }*/
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

// code starts
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<int> h(k);
  vector<int> w(k);
  vector<int> v(k);
  int i, j, l;
  rep(i, k) cin >> h[i] >> w[i] >> v[i];
  vector<vector<int>> val(r, vector<int>(c, 0));
  rep(i, k) { val[h[i] - 1][w[i] - 1] = v[i]; }
  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) rep(l, 4) {
    if (l < 3) {
      if (j + 1 < c)
        dp[i][j + 1][l + 1] = max(dp[i][j + 1][l + 1], dp[i][j][l] + val[i][j]);
      if (i + 1 < r)
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l] + val[i][j]);
    }
    if (j + 1 < c)
      dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
    if (i + 1 < r)
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
  }
  ll ans = 0;
  rep(l, 4) {
    if (l < 3)
      ans = max(ans, dp[r - 1][c - 1][l] + val[r - 1][c - 1]);
    else
      ans = max(ans, dp[r - 1][c - 1][l]);
  }
  /*rep(i,r)
  {
    rep(j,c)
    {
      cout << '(';
      rep(l,3)
      {
        cout << dp[i][j][l] << ' ';
      }
      cout << ") ";
    }
    cout << endl;
  }*/
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long int dp[3001][3001][4];
long long int sc[3001][3001];
int main() {
  int R, C, k;
  cin >> R >> C >> k;
  int r, c;
  long long int v;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    sc[r][c] = v;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int m = 0; m < 4; m++) {
        if (sc[i][j] > 0 && m < 3) {
          dp[i][j + 1][m + 1] =
              max(dp[i][j][m] + sc[i][j], dp[i][j + 1][m + 1]);
          dp[i + 1][j][0] = max(dp[i][j][m] + sc[i][j], dp[i + 1][j][0]);
        }
        dp[i][j + 1][m] = max(dp[i][j][m], dp[i][j + 1][m]);
        dp[i + 1][j][0] = max(dp[i][j][m], dp[i + 1][j][0]);
      }
    }
  }
  cout << dp[R][C - 1][0];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int dp[3001][3001][4];
int main() {
  int R, C, k;
  cin >> R >> C >> k;
  int r, c;
  long long int v;
  map<pair<int, int>, long long int> mp;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    mp[make_pair(r, c)] = v;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int m = 0; m < 4; m++) {
        if (mp[make_pair(i, j)] > 0 && m < 3) {
          dp[i][j + 1][m + 1] =
              max(dp[i][j][m] + mp[make_pair(i, j)], dp[i][j + 1][m + 1]);
          dp[i + 1][j][0] =
              max(dp[i][j][m] + mp[make_pair(i, j)], dp[i + 1][j][0]);
        }
        dp[i][j + 1][m] = max(dp[i][j][m], dp[i][j + 1][m]);
        dp[i + 1][j][0] = max(dp[i][j][m], dp[i + 1][j][0]);
      }
    }
  }
  cout << dp[R][C - 1][0];
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int r, c, k;
  cin >> r >> c >> k;

  vector<vector<ll>> item(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 100,
                                vector<vector<ll>>(c + 100, vector<ll>(4, 0)));
  /* ll dp[3100][3100][4];
   for (int i = 0; i < r + 100; i++) {
     for (int j = 0; j < c + 100; j++) {
       for (int ii = 0; ii < 4; ii++) {
         dp[i][j][ii] = 0;
       }
     }
   }*/

  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    item[x][y] = z;
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        if (item[i][j] == 0) {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
        } else {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
          if (ii < 3) {
            dp[i][j + 1][ii + 1] =
                max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
          }
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        ans = max(ans, dp[i][j][ii]);
      }
    }
  }

  cout << ans << '\\n';
}

Error: -11

Fixed submission:

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int r, c, k;
  cin >> r >> c >> k;

  vector<vector<ll>> item(r, vector<ll>(c));
  ll dp[3100][3100][4];
  for (int i = 0; i < r + 100; i++) {
    for (int j = 0; j < c + 100; j++) {
      for (int ii = 0; ii < 4; ii++) {
        dp[i][j][ii] = 0;
      }
    }
  }

  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    item[x][y] = z;
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        if (item[i][j] == 0) {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
        } else {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
          if (ii < 3) {
            dp[i][j + 1][ii + 1] =
                max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
          }
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        ans = max(ans, dp[i][j][ii]);
      }
    }
  }

  cout << ans << '\\n';
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

const ll INF = 1e18;
int r, c, k;
ll dp[3010][3010][5];
vector<vector<ll>> item(3010, vector<ll>(3010, 0));

int main() {
  cin >> r >> c >> k;
  for (int i = 0; i < k; ++i) {
    ll cr, cc, cv;
    cin >> cr >> cc >> cv;
    cr--;
    cc--;
    item[cr][cc] = cv;
  }

  for (int i = 0; i <= r; ++i) {
    for (int j = 0; j <= c; ++j) {
      for (int l = 0; l <= 3; ++l) {
        dp[i][j][l] = -INF;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i <= r; ++i) {
    for (int j = 0; j <= c; ++j) {
      for (int l = 0; l <= 2; ++l) {
        if (item[i][j] != 0) {
          if (i + 1 <= r)
            chmax(dp[i + 1][j][0], dp[i][j][l] + item[i][j]);
          if (j + 1 <= c)
            chmax(dp[i][j + 1][l + 1], dp[i][j][l] + item[i][j]);
        }
        if (i + 1 <= r)
          chmax(dp[i + 1][j][0], dp[i][j][l]);
        if (j + 1 <= c)
          chmax(dp[i][j + 1][l], dp[i][j][l]);
      }
    }
  }

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

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

const ll INF = 1e18;
int r, c, k;

int main() {
  cin >> r >> c >> k;
  vector<vector<ll>> item(3010, vector<ll>(3010, 0));
  for (int i = 0; i < k; ++i) {
    ll cr, cc, cv;
    cin >> cr >> cc >> cv;
    cr--;
    cc--;
    item[cr][cc] = cv;
  }

  ll dp[3010][3010][5];
  for (int i = 0; i <= r; ++i) {
    for (int j = 0; j <= c; ++j) {
      for (int l = 0; l <= 3; ++l) {
        dp[i][j][l] = -INF;
      }
    }
  }

  dp[0][0][0] = 0;
  for (int i = 0; i <= r; ++i) {
    for (int j = 0; j <= c; ++j) {
      for (int l = 0; l <= 2; ++l) {
        if (item[i][j] != 0) {
          if (i + 1 <= r)
            chmax(dp[i + 1][j][0], dp[i][j][l] + item[i][j]);
          if (j + 1 <= c)
            chmax(dp[i][j + 1][l + 1], dp[i][j][l] + item[i][j]);
        }
        if (i + 1 <= r)
          chmax(dp[i + 1][j][0], dp[i][j][l]);
        if (j + 1 <= c)
          chmax(dp[i][j + 1][l], dp[i][j][l]);
      }
    }
  }

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

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

int main() {
  ll R, C, k;
  cin >> R >> C >> k;
  ll r[k], c[k], v[k];
  vector<vector<ll>> m(R + 2, vector<ll>(C + 2));
  rep(i, k) {
    cin >> r[i] >> c[i] >> v[i];
    m[r[i]][c[i]] = v[i];
  }
  vector<vector<vector<ll>>> dp(R + 2,
                                vector<vector<ll>>(C + 2, vector<ll>(4)));
  for (int j = 1; j <= C + 1; j++) {
    for (int i = 1; i <= R + 1; i++) {
      rep(id, 4) {
        if (m[i][j] > 0) {
          if (id < 3) {
            if (i + 1 <= R + 1)
              dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][id] + m[i][j]);
            if (j + 1 <= C + 1)
              dp[i][j + 1][id + 1] =
                  max(dp[i][j + 1][id + 1], dp[i][j][id] + m[i][j]);
          }
        }
        if (i + 1 <= R + 1)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][id]);
        if (j + 1 <= C + 1)
          dp[i][j + 1][id] = max(dp[i][j + 1][id], dp[i][j][id]);
      }
    }
  }
  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>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ll R, C, k;
  cin >> R >> C >> k;
  ll r[k], c[k], v[k];
  map<P, ll> mp;
  rep(i, k) {
    cin >> r[i] >> c[i] >> v[i];
    mp[P(r[i], c[i])] = v[i];
  }
  vector<vector<vector<ll>>> dp(R + 2,
                                vector<vector<ll>>(C + 2, vector<ll>(4)));
  for (int j = 1; j <= C + 1; j++) {
    for (int i = 1; i <= R + 1; i++) {
      rep(id, 4) {
        if (mp[P(i, j)] > 0) {
          if (id < 3) {
            if (i + 1 <= R + 1)
              dp[i + 1][j][0] =
                  max(dp[i + 1][j][0], dp[i][j][id] + mp[P(i, j)]);
            if (j + 1 <= C + 1)
              dp[i][j + 1][id + 1] =
                  max(dp[i][j + 1][id + 1], dp[i][j][id] + mp[P(i, j)]);
          }
        }
        if (i + 1 <= R + 1)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][id]);
        if (j + 1 <= C + 1)
          dp[i][j + 1][id] = max(dp[i][j + 1][id], dp[i][j][id]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[R + 1][C + 1][i]);
  cout << ans << endl;
  return 0;
}