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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int R, C, K;
ll maze[3030][3030];
ll ret[3030][3030][4];

int main() {
  cin >> R >> C >> K;
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      maze[i][j] = 0;
    }
  }
  for (int i = 0; i < K; i++) {
    int A, B;
    ll C;
    cin >> A >> B >> C;
    maze[A][B] = C;
  }
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        ret[i][j][k] = -INF;
      }
    }
  }
  for (int i = 0; i <= C; i++) {
    ret[0][i][0] = 0;
  }
  for (int i = 0; i <= R; i++) {
    ret[i][0][0] = 0;
  }
  ret[1][1][0] = 0;
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        chmax(ret[i][j][k], ret[i - 1][j][k]);
        chmax(ret[i][j][k], ret[i][j - 1][k]);
        if (maze[i][j] != 0) {
          chmax(ret[i][j][1], ret[i - 1][j][k] + maze[i][j]);
          if (k != 3) {
            chmax(ret[i][j][k + 1], ret[i][j - 1][k] + maze[i][j]);
          }
        }
      }
    }
  }
  ll ret2 = 0;
  for (int i = 0; i < 4; i++) {
    chmax(ret2, ret[R][C][i]);
  }
  cout << ret2 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll mod(ll val, ll M) {
  ll res = val % M;
  if (res < 0) {
    res += M;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int R, C, K;
ll maze[3030][3030];
ll ret[3030][3030][4];
ll ret2 = 0;

void solve(int a, int b, int count, ll cost) {
  if (a == R - 1 && b == C - 1) {
    if (count != 3) {
      chmax(ret2, cost + maze[a][b]);
    } else {
      chmax(ret2, cost);
    }
    return;
  }
  if (!(a < R && b < C)) {
    return;
  }
  if (ret[a][b][count] >= cost) {
    return;
  }
  ret[a][b][count] = cost;
  if (maze[a][b] != 0) {
    solve(a, b + 1, count, cost);
    solve(a + 1, b, 0, cost);
    if (count != 3) {
      solve(a, b + 1, count + 1, cost + maze[a][b]);
      solve(a + 1, b, 0, cost + maze[a][b]);
    }
  } else {
    solve(a, b + 1, count, cost);
    solve(a + 1, b, 0, cost);
  }
}

int main() {
  cin >> R >> C >> K;
  for (int i = 0; i < 3030; i++) {
    for (int j = 0; j < 3030; j++) {
      maze[i][j] = 0;
    }
  }
  for (int i = 0; i < K; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    maze[A][B] = C;
  }
  for (int i = 0; i < 3030; i++) {
    for (int j = 0; j < 3030; j++) {
      for (int k = 0; k < 4; k++) {
        ret[i][j][k] = -INF;
      }
    }
  }
  solve(0, 0, 0, 0);
  cout << ret2 << endl;
}