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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// ---------- PBDS -----------------

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
// slow but fast ORZ :)
// ----------------------------------

using namespace std;
using namespace __gnu_pbds;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(a) a.begin(), a.end()
#define MP(a, b) make_pair(a, b)
#define PB(a) push_back(a)
#define EB(a) emplace_back(a) // care when use
#define boost                                                                  \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0),                       \\
      cout << fixed << setprecision(10);
// #define ll long long int
#define int long long int

#define DEBUG

#ifdef DEBUG

#define debug(args...) (Debugger()), args

class Debugger {
public:
  Debugger(const std::string &_separator = " | ")
      : first(true), separator(_separator) {}

  template <typename ObjectType> Debugger &operator,(const ObjectType &v) {
    if (!first)
      std::cerr << separator;
    std::cerr << v;
    first = false;
    return *this;
  }
  ~Debugger() { std::cerr << std::endl; }

private:
  bool first;
  std::string separator;
};

#else
#define debug(args...)
#endif

const int N = 3003;

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

int valid(int x, int y) {
  if (x >= r or y >= c or x < 0 or y < 0) {
    return 0;
  }
  return 1;
}

int fun(int row, int col, int pick) {

  if (row == r - 1 and col == c - 1) {
    if (pick != 3) {
      return a[row][col];
    } else {
      return 0;
    }
  }

  if (dp[row][col][pick] != -1)
    return dp[row][col][pick];

  if (pick == 3) {
    if (valid(row, col + 1))
      dp[row][col][pick] = fun(row, col + 1, pick);
    if (valid(row + 1, col))
      dp[row][col][pick] = max(dp[row][col][pick], fun(row + 1, col, 0));
  } else {
    if (valid(row, col + 1))
      dp[row][col][pick] = max(fun(row, col + 1, pick + 1) + a[row][col],
                               fun(row, col + 1, pick));
    if (valid(row + 1, col))
      dp[row][col][pick] =
          max(dp[row][col][pick], fun(row + 1, col, 0) + a[row][col]);
  }

  return dp[row][col][pick];
}

void solve() {

  cin >> r >> c >> k;

  MEM(a, 0);
  MEM(dp, -1);

  for (int i = 0; i < k; ++i) {
    int rr, cc, vv;
    cin >> rr >> cc >> vv;
    rr--;
    cc--;

    a[rr][cc] = vv;
  }

  cout << fun(0, 0, 0) << "\\n";
}

int32_t main() {

  boost;

  int T;

  // cin >> T;
  T = 1;

  while (T--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0),                       \\
      cout << fixed << setprecision(10);
#define endl '\\n'
#define int long long int

int dp[3005][3005][4];
int R, C, K;
int a[3005][3005];

int func(int r, int c, int p) {
  if (r == R - 1 && c == C - 1 && p == 3)
    return 0;
  else if (r == R - 1 && c == C - 1)
    return a[r][c];

  if (dp[r][c][p] != -1)
    return dp[r][c][p];

  if (p == 3) {
    if (c + 1 <= C) {
      dp[r][c][p] = max(func(r, c + 1, p), dp[r][c][p]);
    }
    if (r + 1 <= R) {
      dp[r][c][p] = max(dp[r][c][p], func(r + 1, c, 0));
    }

  } else {
    if (c + 1 <= C) {
      dp[r][c][p] = max(func(r, c + 1, p), func(r, c + 1, p + 1) + a[r][c]);
    }
    if (r + 1 <= R) {
      dp[r][c][p] = max(dp[r][c][p], func(r + 1, c, 0) + a[r][c]);
    }
  }

  return dp[r][c][p];
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  boost;
  memset(dp, -1, sizeof(dp));
  cin >> R >> C >> K;

  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r - 1][c - 1] = v;
  }
  cout << func(0, 0, 0) << endl;
  return 0;
}