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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265358979323846264338327950L
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\\n'
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 3e3 + 5;
ll dp[L][L][6];
ll val[L][L];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;
  for (ll i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    val[a][b] = c;
  }
  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      for (ll k = 0; k < 4; k++)
        dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][4]);
      if (val[i][j] != 0) {
        ll v = val[i][j];
        for (ll k = 1; k < 4; k++) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v);
        }
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][4] + v);
      }
      ll temp = 0;
      for (ll k = 0; k < 4; k++)
        temp = max(temp, dp[i][j][k]);
      dp[i][j][4] = temp;
    }
  }
  cout << dp[r][c][4] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265358979323846264338327950L
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\\n'
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 3e3 + 5;
ll dp[L][L][6];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;
  map<pll, ll> val;
  for (ll i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    val[{a, b}] = c;
  }
  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      for (ll k = 0; k < 4; k++)
        dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][4]);
      if (val[{i, j}] != 0) {
        ll v = val[{i, j}];
        for (ll k = 1; k < 4; k++) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v);
        }
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][4] + v);
      }
      ll temp = 0;
      for (ll k = 0; k < 4; k++)
        temp = max(temp, dp[i][j][k]);
      dp[i][j][4] = temp;
      // if (i == 1) {
      // 	debug(dp[i][j]);
      // }
    }
  }
  cout << dp[r][c][4] << endl;
  return 0;
}