Problem p02586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

constexpr int N = 3005;

int r, c, k;
int arr[N][N];
int dp[N][N][4];

int recur(int r, int c, int pick) {
  if (r < 0 || c < 0 || pick <= 0) {
    return 0;
  }
  if (dp[r][c][pick] != -1)
    return dp[r][c][pick];

  int a = recur(r - 1, c, 3) + arr[r][c];
  int b = recur(r, c - 1, pick - 1) + arr[r][c];
  int cc = recur(r, c - 1, pick);
  int z = max(a, max(b, cc));

  return z;
}

void solve() {
  mem(dp, -1);
  cin >> r >> c >> k;
  rep(i, 0, k) {
    int a, b, v;
    cin >> a >> b >> v;
    a--, b--;
    arr[a][b] = v;
  }
  cout << recur(r - 1, c - 1, 3) << endl;
  return;
}

signed main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // sieve();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

constexpr int N = 3005;

int r, c, k;
int arr[N][N];
int dp[N][N][4];

int recur(int r, int c, int pick) {
  if (r < 0 || c < 0 || pick <= 0) {
    return 0;
  }
  if (dp[r][c][pick] != -1)
    return dp[r][c][pick];

  int a = recur(r - 1, c, 3) + arr[r][c];
  int b = recur(r, c - 1, pick - 1) + arr[r][c];
  int cc = recur(r, c - 1, pick);
  int z = max(a, max(b, cc));

  return dp[r][c][pick] = z;
}

void solve() {
  mem(dp, -1);
  cin >> r >> c >> k;
  rep(i, 0, k) {
    int a, b, v;
    cin >> a >> b >> v;
    a--, b--;
    arr[a][b] = v;
  }
  cout << recur(r - 1, c - 1, 3) << endl;
  return;
}

signed main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // sieve();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}