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

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  auto goods = make_v<ll>(R + 1, C + 1);
  auto dp = make_v<ll>(R + 1, C + 1, 4);
  for (size_t i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    goods[r - 1][c - 1] = v;
  }
  if (goods[0][0] > 0) {
    dp[0][0][1] = goods[0][0];
  }
  ll ans = 0;
  for (ll i = 0; i < R; i++) {
    for (ll j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k > 0 and goods[i][j + 1] > 0) {
          chmax(dp[i][j + 1][k], dp[i][j][k - 1] + goods[i][j + 1]);
        }
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        if (goods[i + 1][j] > 0) {
          chmax(dp[i + 1][j][1], dp[i + 1][j][0] + goods[i + 1][j]);
        }
        chmax(ans, dp[i][j + 1][k]);
        chmax(ans, dp[i + 1][j][k]);
        chmax(ans, dp[i][j][k]);
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  map<pair<int, int>, ll> goods;
  auto dp = make_v<ll>(R + 1, C + 1, 4);
  for (size_t i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    goods[{r - 1, c - 1}] = v;
  }
  if (goods.count({0, 0}) == 1) {
    dp[0][0][1] = goods[{0, 0}];
  }
  ll ans = 0;
  for (ll i = 0; i < R; i++) {
    for (ll j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k > 0 and goods.count({i, j + 1}) == 1) {
          chmax(dp[i][j + 1][k], dp[i][j][k - 1] + goods[{i, j + 1}]);
        }
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        if (goods.count({i + 1, j}) == 1) {
          chmax(dp[i + 1][j][1], dp[i + 1][j][0] + goods[{i + 1, j}]);
        }
        chmax(ans, dp[i][j + 1][k]);
        chmax(ans, dp[i + 1][j][k]);
        chmax(ans, dp[i][j][k]);
      }
    }
  }

  cout << ans << endl;
  return 0;
}