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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
// const double PI = acos(-1);
const int mod = 1000000007;

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// ----------------------DEBUG TEMPLATE--------------------------
int R, C;
int mat[3001][3001];
int dp[3001][3001][4];

int solve(int x, int y, int curr) {
  if (x > R || y > C) {
    return 0;
  }

  if (dp[x][y][curr] != -1) {
    return dp[x][y][curr];
  }
  if (mat[x][y] == 0) {
    dp[x][y][curr] = max(solve(x, y + 1, curr), solve(x + 1, y, 0));
    return dp[x][y][curr];
  } else {
    if (curr < 3) {
      int op1 = mat[x][y] + solve(x, y + 1, curr + 1);
      int op2 = solve(x, y + 1, curr);
      int op3 = mat[x][y] + solve(x + 1, y, 0);
      int op4 = solve(x + 1, y, 0);
      dp[x][y][curr] = max({op1, op2, op3, op4});
      return dp[x][y][curr];
    } else {
      dp[x][y][curr] = max(solve(x + 1, y, 0), solve(x, y + 1, curr));
      return dp[x][y][curr];
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int K;
  cin >> R >> C >> K;
  memset(mat, 0, sizeof(mat));
  memset(dp, -1, sizeof(dp));
  while (K--) {
    int r, c, val;
    cin >> r >> c >> val;
    mat[r][c] = val;
  }
  cout << solve(1, 1, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
// const double PI = acos(-1);
const int mod = 1000000007;

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// ----------------------DEBUG TEMPLATE--------------------------
int R, C;
int mat[3001][3001];

int solve(int sr, int sc, int dr, int dc, vector<int> freq) {
  if (sr > dr || sc > dc)
    return 0;
  if (sr == dr && sc == dc) {
    if (freq[dr] == 3) {
      return 0;
    }
    return mat[dr][dc];
  }
  if (freq[sr] == 3) {
    return solve(sr + 1, sc, dr, dc, freq);
  } else {
    int op1 =
        max(solve(sr + 1, sc, dr, dc, freq), solve(sr, sc + 1, dr, dc, freq));
    freq[sr]++;
    int op2 = mat[sr][sc] + max(solve(sr + 1, sc, dr, dc, freq),
                                solve(sr, sc + 1, dr, dc, freq));
    return max(op1, op2);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int K;
  cin >> R >> C >> K;
  memset(mat, 0, sizeof(mat));
  while (K--) {
    int r, c, val;
    cin >> r >> c >> val;
    mat[r][c] = val;
  }
  vector<int> freq(R + 1, 0);
  cout << solve(1, 1, R, C, freq) << endl;
}