Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define int long long int
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define modp 998244353
#define inf (ll)1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int r, c, k;
int dp[3001][3001][4];
int cost[3001][3001];

int solve(int i, int j, int item_row) {
  if (i == r - 1 && j == c - 1) {
    if (item_row < 3 && cost[i][j])
      return cost[i][j];
    return 0;
  }
  if (i >= r)
    return INT_MIN;
  if (j >= c)
    return INT_MIN;
  if (dp[i][j][item_row] != -1)
    return dp[i][j][item_row];
  int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN;
  if (cost[i][j] && item_row < 3) {
    x = cost[i][j] + solve(i, j + 1, item_row + 1);
    y = cost[i][j] + solve(i + 1, j, 0);
    z = solve(i, j + 1, item_row);
    w = solve(i + 1, j, 0);
  } else {
    x = solve(i, j + 1, item_row);
    y = solve(i + 1, j, 0);
  }
  return dp[i][j][item_row] = max({x, y, z, w});
}

void solve() {
  cin >> r >> c >> k;
  memset(cost, 0, r * c);
  for (int i = 0; i < k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    cost[x][y] = z;
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0, 0) << endl;
}

int32_t main() {
  FIO
  // w(t)
  {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
// #define int             long long int
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define modp 998244353
#define inf (ll)1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int r, c, k;
map<pair<int, int>, int> mp;
int dp[3001][3001][4];

int solve(int i, int j, int item_row) {
  if (i == r - 1 && j == c - 1) {
    if (item_row < 3)
      return mp[{i, j}];
    return 0;
  }
  if (i >= r)
    return INT_MIN;
  if (j >= c)
    return INT_MIN;
  if (dp[i][j][item_row] != -1)
    return dp[i][j][item_row];
  int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN;
  if (mp[{i, j}] >= 1 && item_row < 3) {
    x = mp[{i, j}] + solve(i, j + 1, item_row + 1);
    y = mp[{i, j}] + solve(i + 1, j, 0);
    z = solve(i, j + 1, item_row);
    w = solve(i + 1, j, 0);
  } else {
    x = solve(i, j + 1, item_row);
    y = solve(i + 1, j, 0);
  }
  return dp[i][j][item_row] = max({x, y, z, w});
}

void solve() {
  cin >> r >> c >> k;
  for (int i = 0; i < k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    mp[{x, y}] = z;
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0, 0) << endl;
}

int32_t main() {
  FIO
  // w(t)
  {
    solve();
  }
  return 0;
}