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

#include <bits/stdc++.h>
using namespace std;
#define optimizar_io                                                           \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

typedef long long int ll;
typedef pair<int, int> ii;

int n, m, k, val[3010][3010];
ll memo[3010][3010][4];

ll dp(int i, int j, int taken) {
  if (i == n - 1 and j == m - 1) {
    if (taken == 3)
      return 0;
    return val[n - 1][m - 1];
  }
  if (memo[i][j][taken] != -1)
    return memo[i][j][taken];
  if (i == n - 1 and j != m - 1) {
    if (taken == 3)
      return memo[i][j][taken] = dp(i, j + 1, taken);
    ll v = 0;
    if (val[i][j] != 0)
      v = val[i][j];
    return memo[i][j][taken] =
               max(dp(i, j + 1, taken), dp(i, j + 1, taken + 1) + v);
  }
  if (i != n - 1 and j == m - 1) {
    ll v = 0;
    if (val[i][j] != 0)
      v = val[i][j];
    return memo[i][j][taken] = dp(i + 1, j, 0) + v;
  }
  if (taken == 3)
    return memo[i][j][taken] = max(dp(i + 1, j, 0), dp(i, j + 1, taken));
  else {
    if (val[i][j] != 0) {
      ll v = val[i][j];
      return memo[i][j][taken] = max({dp(i + 1, j, 0) + v, dp(i, j + 1, taken),
                                      dp(i, j + 1, taken + 1) + v});
    } else {
      return memo[i][j][taken] = max(dp(i + 1, j, 0), dp(i, j + 1, taken));
    }
  }
}

int main() {
  optimizar_io int r, c, v;
  cin >> n >> m >> k;
  while (k--) {
    cin >> r >> c >> v;
    val[r - 1][c - 1] = v;
  }
  memset(memo, -1, sizeof(memo));
  cout << dp(0, 0, 0) << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define optimizar_io                                                           \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

typedef long long int ll;
typedef pair<int, int> ii;

int n, m, k;
ll memo[3010][3010][4];
map<ii, ll> val;

ll dp(int i, int j, int taken) {
  if (i == n - 1 and j == m - 1) {
    if (taken == 3)
      return 0;
    return val[ii(n - 1, m - 1)];
  }
  if (memo[i][j][taken] != -1)
    return memo[i][j][taken];
  if (i == n - 1 and j != m - 1) {
    if (taken == 3)
      return memo[i][j][taken] = dp(i, j + 1, taken);
    ll v = 0;
    if (val.count(ii(i, j)))
      v = val[ii(i, j)];
    return memo[i][j][taken] =
               max(dp(i, j + 1, taken), dp(i, j + 1, taken + 1) + v);
  }
  if (i != n - 1 and j == m - 1) {
    ll v = 0;
    if (val.count(ii(i, j)))
      v = val[ii(i, j)];
    return memo[i][j][taken] = dp(i + 1, j, 0) + v;
  }
  if (taken == 3)
    return memo[i][j][taken] = max(dp(i + 1, j, 0), dp(i, j + 1, taken));
  else {
    if (val.count(ii(i, j))) {
      ll v = val[ii(i, j)];
      return memo[i][j][taken] = max({dp(i + 1, j, 0) + v, dp(i, j + 1, taken),
                                      dp(i, j + 1, taken + 1) + v});
    } else {
      return memo[i][j][taken] = max(dp(i + 1, j, 0), dp(i, j + 1, taken));
    }
  }
}

int main() {
  optimizar_io int r, c, v;
  cin >> n >> m >> k;
  while (k--) {
    cin >> r >> c >> v;
    val[ii(r - 1, c - 1)] = v;
  }
  memset(memo, -1, sizeof(memo));
  cout << dp(0, 0, 0) << "\\n";
  return 0;
}