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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;
#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll R;
  cin >> R;
  ll C;
  cin >> C;
  ll K;
  cin >> K;
  vector<ll> r(K, 0);
  vector<ll> c(K, 0);
  vector<ll> v(K, 0);
  for (ll i = 0; i < K; ++i)
    cin >> r[i] >> c[i] >> v[i];
  for (ll i = 0; i < K; ++i) {
    c[i]--;
    r[i]--;
  }
  vector<vector<ll>> val(R, vector<ll>(C, 0));
  for (ll i = 0; i < K; ++i) {
    val[r[i]][c[i]] = v[i];
  }
  vector<vector<vector<ll>>> dp(4, vector<vector<ll>>(R, vector<ll>(C, -INF)));
  for (ll i = 0; i < R; ++i)
    for (ll j = 0; j < C; ++j)
      dp[0][i][j] = 0;
  dp[0][0][0] = 0;
  dp[1][0][0] = val[0][0];
  dp[2][0][0] = 0;
  dp[3][0][0] = 0;
  function<ll(ll, ll, ll)> F = [&](ll t, ll r, ll c) {
    if (dp[t][r][c] != -INF)
      return dp[t][r][c];
    ll ret = 0;
    // とらないほう！
    if (t == 0) {
      if (r > 0 && c > 0) {
        // for(ll i=0;i<4;++i)chmax(ret,F(i,r-1,c-1));
      }
      if (r > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c));
      }
    }

    if (c > 0) {
      chmax(ret, F(t, r, c - 1));
    }
    // とるほう！
    if (t == 1) {
      if (r > 0 && c > 0) {
        // for(ll i=0;i<4;++i)chmax(ret,F(i,r-1,c-1)+val[r][c]);
      }
      if (r > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c) + val[r][c]);
      }
    }
    if (c > 0 && t != 0) {
      chmax(ret, F(t - 1, r, c - 1) + val[r][c]);
    }
    return dp[t][r][c] = ret;
  };
  prt(max({F(0, R - 1, C - 1), F(1, R - 1, C - 1), F(2, R - 1, C - 1),
           F(3, R - 1, C - 1)}));

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;
#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll R;
  cin >> R;
  ll C;
  cin >> C;
  ll K;
  cin >> K;
  vector<ll> r(K, 0);
  vector<ll> c(K, 0);
  vector<ll> v(K, 0);
  for (ll i = 0; i < K; ++i)
    cin >> r[i] >> c[i] >> v[i];
  for (ll i = 0; i < K; ++i) {
    c[i]--;
    r[i]--;
  }
  vector<vector<ll>> val(R, vector<ll>(C, 0));
  for (ll i = 0; i < K; ++i) {
    val[r[i]][c[i]] = v[i];
  }
  vector<vector<vector<ll>>> dp(4, vector<vector<ll>>(R, vector<ll>(C, -INF)));
  for (ll i = 0; i < R; ++i)
    for (ll j = 0; j < C; ++j)
      dp[0][i][j] = 0;
  dp[0][0][0] = 0;
  dp[1][0][0] = val[0][0];
  dp[2][0][0] = 0;
  dp[3][0][0] = 0;
  function<ll(ll, ll, ll)> F = [&](ll t, ll r, ll c) {
    if (dp[t][r][c] != -INF)
      return dp[t][r][c];
    ll ret = 0;
    // とらないほう！
    if (t == 0) {
      if (r > 0 && c > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c - 1));
      }
      if (r > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c));
      }
    }

    if (c > 0) {
      chmax(ret, F(t, r, c - 1));
    }
    // とるほう！
    if (t == 1) {
      if (r > 0 && c > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c - 1) + val[r][c]);
      }
      if (r > 0) {
        for (ll i = 0; i < 4; ++i)
          chmax(ret, F(i, r - 1, c) + val[r][c]);
      }
    }
    if (c > 0 && t != 0) {
      chmax(ret, F(t - 1, r, c - 1) + val[r][c]);
    }
    return dp[t][r][c] = ret;
  };
  prt(max({F(0, R - 1, C - 1), F(1, R - 1, C - 1), F(2, R - 1, C - 1),
           F(3, R - 1, C - 1)}));

  return 0;
}