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

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;
using P = pair<int, int>;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

  int R, C, K;
  cin >> R >> C >> K;
  auto t = make_vec<int>(R, C);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    t[r][c] = v;
  }

  auto dp = make_vec<int>(R + 1, C + 1, 5);
  fill(dp, 0);
  dp[0][0][1] = t[0][0];

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i + 1 < R) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + t[i + 1][j]);
        }
        if (j + 1 < C) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + t[i][j + 1]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;
using P = pair<int, int>;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

  int R, C, K;
  cin >> R >> C >> K;
  map<P, int> mp;
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--, c--;
    mp[{r, c}] = v;
  }

  auto dp = make_vec<int>(R + 1, C + 1, 5);
  fill(dp, 0);
  dp[0][0][1] = mp[{0, 0}];

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (i + 1 < R) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + mp[{i + 1, j}]);
        }
        if (j + 1 < C) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + mp[{i, j + 1}]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;

  return 0;
}