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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i <= n; i++)
#define FORI(i, n) for (int i = n - 1; i >= 0; i--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, (T)x);
}
template <class T, class U, typename... W>
void init(vector<T> &v, U x, size_t n, W... m) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m...);
}

vector<vector<vector<ll>>> dp;
vector<vector<int>> vs;
int r, c, k;

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int T = 1;
  //    cin >> T;
  FOR1(tt, T) {
    cin >> r >> c >> k;
    init(vs, 0, r + 1, c + 1);
    FOR(i, k) {
      int ri, ci, vi;
      cin >> ri >> ci >> vi;
      vs[ri][ci] = vi;
    }

    init(dp, 0, r + 1, c + 1, 4);
    dp[r][c][1] = vs[r][c]; // at pos r, c taken t from row r
    for (int rr = r; rr > 0; rr--) {
      for (int cc = c; cc > 0; cc--) {
        // just go right
        if (cc < c) {
          FOR(i, 4) { umax(dp[rr][cc][i], dp[rr][cc + 1][i]); }
        }
        // just go down
        if (rr < r) {
          FOR(i, 4) { umax(dp[rr][cc][i], dp[rr + 1][cc][i]); }
        }
        if (vs[rr][cc]) {
          // take and go right
          if (cc + 1 <= c) {
            FOR1(i, 3) {
              umax(dp[rr][cc][i], dp[rr][cc + 1][i]);
              umax(dp[rr][cc][i], dp[rr][cc + 1][i - 1] + vs[rr][cc]);
            }
          }
          // take and go down
          if (rr + 1 <= r) {
            FOR(i, 4)
            umax(dp[rr][cc][1], dp[rr + 1][cc][i] + vs[rr][cc]);
          }
        }
      }
    }
    ll sol = 0;
    FOR(i, 4) umax(sol, dp[1][1][i]);
    cout << sol << endl;
  }

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i <= n; i++)
#define FORI(i, n) for (int i = n - 1; i >= 0; i--)
template <class T, class U> void umin(T &x, const U &y) { x = min(x, (T)y); }
template <class T, class U> void umax(T &x, const U &y) { x = max(x, (T)y); }
template <class T, class U> void init(vector<T> &v, U x, size_t n) {
  v = vector<T>(n, (T)x);
}
template <class T, class U, typename... W>
void init(vector<T> &v, U x, size_t n, W... m) {
  v = vector<T>(n);
  for (auto &a : v)
    init(a, x, m...);
}

vector<vector<ll>> dp;
vector<vector<int>> vs;
int r, c, k;
ll solve(int rr, int cc) {
  if (dp[rr][cc] != -1)
    return dp[rr][cc];
  ll ret = 0;
  if (rr < r)
    umax(ret, solve(rr + 1, cc));
  set<pii> S;
  for (int j = cc; j <= c; j++) {
    if (vs[rr][j]) {
      S.insert({vs[rr][j], j});
      if (S.size() > 3)
        S.erase(S.begin());

      ll s = 0;
      for (auto it : S)
        s += it.first;

      if (rr < r)
        umax(ret, s + solve(rr + 1, j));
      else
        umax(ret, s);
    }
  }
  return dp[rr][cc] = ret;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int T = 1;
  //    cin >> T;
  FOR1(tt, T) {
    cin >> r >> c >> k;
    init(vs, 0, r + 1, c + 1);
    FOR(i, k) {
      int ri, ci, vi;
      cin >> ri >> ci >> vi;
      vs[ri][ci] = vi;
    }

    init(dp, -1, r + 1, c + 1);
    dp[r][c] = vs[r][c];
    ll sol = solve(1, 1);
    cout << sol << endl;
  }

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}