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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll r, c, k, a, b, vv;
ll grid[3005][3005];
ll memo[3005][3005][3];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> r >> c >> k;
  REP(i, 0, k) {
    cin >> a >> b >> vv;
    grid[a - 1][b - 1] = vv;
    // v[a - 1].PB(b - 1, vv);
  }
  for (ll y = c - 1; y >= 0; y--) {
    for (ll x = r - 1; x >= 0; x--) {
      for (ll z = 0; z <= 3; z++) {
        if (x == r - 1 && y == c - 1) {
          if (z == 0) {
            memo[x][y][z] = 0;
            continue;
          }
          if (grid[r - 1][c - 1] == 0) {
            memo[x][y][z] = 0;
            continue;
          }
          memo[x][y][z] = grid[r - 1][c - 1];
          continue;
        }
        ll ans = 0;
        if (z == 0 || grid[x][y] == 0) {
          if (y + 1 < c)
            ans = max(ans, memo[x][y + 1][z]);
          if (x + 1 < r)
            ans = max(ans, memo[x + 1][y][3]);
          memo[x][y][z] = ans;
          continue;
        }

        if (grid[x][y] != 0 && z > 0) {
          if (x + 1 < r)
            ans = max(ans, grid[x][y] + memo[x + 1][y][3]);
          if (y + 1 < c)
            ans = max(ans, grid[x][y] + memo[x][y + 1][z - 1]);
          if (y + 1 < c)
            ans = max(ans, memo[x][y + 1][z]);
          if (x + 1 < r)
            ans = max(ans, memo[x + 1][y][3]);
        }
        memo[x][y][z] = ans;
        continue;
      }
    }
  }
  cout << memo[0][0][3] << endl;
}

// cout.flush()
// cout << fixed << setprecision(9) << x << endl;

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll r, c, k, a, b, vv;
ll grid[3005][3005];
ll memo[3005][3005][3];

ll dp(ll x, ll y, ll z) {
  if (x == r - 1 && y == c - 1) {
    if (z == 0)
      return 0;
    if (grid[r - 1][c - 1] == 0)
      return 0;
    return memo[x][y][z] = grid[r - 1][c - 1];
  }
  ll ans = 0;
  if (z == 0 || grid[x][y] == 0) {
    if (y + 1 < c)
      ans = max(ans, dp(x, y + 1, z));
    if (x + 1 < r)
      ans = max(ans, dp(x + 1, y, 3));
    return memo[x][y][z] = ans;
  }

  if (grid[x][y] != 0 && z > 0) {
    if (x + 1 < r)
      ans = max(ans, grid[x][y] + dp(x + 1, y, 3));
    if (y + 1 < c)
      ans = max(ans, grid[x][y] + dp(x, y + 1, z - 1));
    if (y + 1 < c)
      ans = max(ans, dp(x, y + 1, z));
    if (x + 1 < r)
      ans = max(ans, dp(x + 1, y, 3));
  }
  return memo[x][y][z] = ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> r >> c >> k;
  REP(i, 0, k) {
    cin >> a >> b >> vv;
    grid[a - 1][b - 1] = vv;
    // v[a - 1].PB(b - 1, vv);
  }
  cout << dp(0, 0, 3) << endl;
}

// cout.flush()
// cout << fixed << setprecision(9) << x << endl;