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

/*
Dont BLUNDER
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimization("unroll-loops")
// for n2 loops
#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


 */
// #define d long double
#define int long long
#define pii pair<int, int>
#define pb push_back
#define endl '\\n'
#define pi acos(-1)

#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
int const mod = 998244353;
int const mod1 = 1000000007;
#define l first
#define r second
int toint(const string &s) {
  stringstream ss;
  ss << s;
  int x;
  ss >> x;
  return x;
}
string tostring(int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
#define out(x)                                                                 \\
  cout << #x << " = "                                                          \\
       << " " << x << "\\n";
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define haha                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

int modexp(int x, int n) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2) % mod);
  }
}

int modinv(int n) { return modexp(n, mod - 2); }

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
bool comp(pii a, pii b) { return a.second < b.second; }
struct hash_pair {

  size_t operator()(const pair<int, int> &p) const {

    return ((p.second ^ p.first) << 32);
  }
};
int r, c, k;
int arr[3001][3001];
int dp[3001][3001][4];

int go(int x, int y, int rowtaken) {
  // cout << x << " " << y << " " << rowtaken << endl;
  if (rowtaken > 3) {
    return -100000000000000;
  }
  if (x > r || y > c)
    return 0;
  if (dp[x][y][rowtaken] >= 0)
    return dp[x][y][rowtaken];

  if (arr[x][y] > 0 && rowtaken < 3) {

    int op1 = arr[x][y] + go(x, y + 1, rowtaken + 1);

    int op2 = arr[x][y] + go(x + 1, y, 0);
    int op3 = go(x, y + 1, rowtaken);
    int op4 = go(x + 1, y, 0);
    dp[x][y][rowtaken] = max(max(op1, op2), max(op3, op4));
    return dp[x][y][rowtaken];

  } else {

    int op3 = go(x, y + 1, rowtaken);
    int op4 = go(x + 1, y, 0);
    dp[x][y][rowtaken] = max(op3, op4);
    return dp[x][y][rowtaken];
  }
}

signed main() {
  haha
          // #ifndef ONLINE_JUDGE

          // 	freopen("inp.txt", "r", stdin);

          // 	freopen("out1.txt", "w", stdout);
          // #endif
          cin >>
      r >> c >> k;
  init(arr, 0);
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[a][b] = c;
  }
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = -1;
      }
    }
  }

  cout << go(1, 1, 0);
}

Error: TLE

Fixed submission:

/*
Dont BLUNDER
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimization("unroll-loops")
// for n2 loops
#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


 */
// #define d long double
#define int long long
#define pii pair<int, int>
#define pb push_back
#define endl '\\n'
#define pi acos(-1)

#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
int const mod = 998244353;
int const mod1 = 1000000007;
#define l first
#define r second
int toint(const string &s) {
  stringstream ss;
  ss << s;
  int x;
  ss >> x;
  return x;
}
string tostring(int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
#define out(x)                                                                 \\
  cout << #x << " = "                                                          \\
       << " " << x << "\\n";
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define haha                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

int modexp(int x, int n) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2) % mod);
  }
}

int modinv(int n) { return modexp(n, mod - 2); }

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
bool comp(pii a, pii b) { return a.second < b.second; }
struct hash_pair {

  size_t operator()(const pair<int, int> &p) const {

    return ((p.second ^ p.first) << 32);
  }
};
int r, c, k;
int arr[3001][3001];
int dp[3001][3001][4];

int go(int x, int y, int rowtaken) {
  // cout << x << " " << y << " " << rowtaken << endl;
  if (rowtaken > 3) {
    return -100000000000000;
  }
  if (x > r || y > c)
    return 0;
  if (dp[x][y][rowtaken] > 0)
    return dp[x][y][rowtaken];

  if (arr[x][y] > 0) {
    int op1 = 0, op2 = 0;
    if (rowtaken < 3)
      op1 = arr[x][y] + go(x, y + 1, rowtaken + 1);
    if (rowtaken < 3)
      op2 = arr[x][y] + go(x + 1, y, 0);
    int op3 = go(x, y + 1, rowtaken);
    int op4 = go(x + 1, y, 0);
    dp[x][y][rowtaken] = max(max(op1, op2), max(op3, op4));
    return dp[x][y][rowtaken];

  } else {

    int op3 = go(x, y + 1, rowtaken);
    int op4 = go(x + 1, y, 0);
    dp[x][y][rowtaken] = max(op3, op4);
    return dp[x][y][rowtaken];
  }
}

signed main() {
  haha

          cin >>
      r >> c >> k;
  init(arr, 0);
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[a][b] = c;
  }
  init(dp, 0);

  cout << go(1, 1, 0);
}