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

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

ll d[3010][3010];
ll dp[3010][3010][4];

int main() {
  clr(d, 0);
  ll h, w, k;
  cin >> h >> w >> k;
  map<pair<ll, ll>, ll> ma;
  rep(i, 0, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    d[a][b] = c;
  }
  dp[0][0][1] = d[0][0];
  rep(i, 0, h) {
    rep(j, 0, w) {
      ll a = d[i][j];
      rep(k, 0, 4) {
        if (j - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (i - 1 >= 0)
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (i - 1 >= 0)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a);
        if (j - 1 >= 0 && k + 1 <= 3)
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j - 1][k] + a);
      }
    }
  }
  ll mx = 0;
  rep(i, 0, 4) { mx = max(mx, dp[h - 1][w - 1][i]); }
  cout << mx << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

ll dp[3010][3010][4];

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  map<pair<ll, ll>, ll> ma;
  rep(i, 0, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ma[mp(a, b)] = c;
  }
  dp[0][0][1] = ma[mp(0, 0)];
  rep(i, 0, h) {
    rep(j, 0, w) {
      ll a = ma[mp(i, j)];
      rep(k, 0, 4) {
        if (j - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (i - 1 >= 0)
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
        if (i - 1 >= 0)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + a);
        if (j - 1 >= 0 && k + 1 <= 3)
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j - 1][k] + a);
      }
    }
  }
  ll mx = 0;
  rep(i, 0, 4) { mx = max(mx, dp[h - 1][w - 1][i]); }
  cout << mx << endl;
}