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

// bismillahir rahmanir rahim            //Author:Fayed Anik

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

// #define         ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define lf double
#define pb(x) push_back(x)
#define ull unsigned long long
#define sfl(a) scanf("%lld", &a)
#define sf(a) scanf("%d", &a)
#define pf(a) printf("%d\\n", a)
#define pfl(a) printf("%lld\\n", a)
#define FOR(x, n) for (ll x = 1; x <= n; ++x)
#define pii pair<ll, ll>
#define mp(a, b) make_pair(a, b)
#define UNIQUE(v) v.resize(distance(v.begin(), unique(v.begin(), v.end())))
#define mod 1000000007
#define INF 2e18
#define EPS 1e-15
#define f1 first
#define f2 second
#define all(v) v.begin(), v.end()
#define PI acos(-1)
#define printminusone printf("-1\\n")
#define bug printf("bug")
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)

// ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
// ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
// bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
// priority_queue <ll, vector<ll>, greater<ll> > pq;
// int dx[]={0,0,1,-1,1,1,-1,-1};
// int dy[]={1,-1,0,0,1,-1,1,-1};

#define lim 3005

ll dp[lim][lim][4], r, c, k, v[200005], MAP[lim][lim];

ll F(ll x, ll y, ll now) {
  if (x > r or y > c)
    return 0;
  ll &ret = dp[x][y][now];
  if (~ret)
    return ret;
  ret = 0;
  ll val = MAP[x][y];
  if (val and now < 3) {
    ret = val + F(x, y + 1, now + 1);
    ret = max(ret, val + F(x + 1, y, 0));
  }
  ret = max(ret, F(x, y + 1, now));
  ret = max(ret, F(x + 1, y, 0));
  return dp[x][y][now] = ret;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll x, y, z;
  sfl(r), sfl(c), sfl(k);
  FOR(i, k) {
    sfl(x), sfl(y), sfl(z);
    MAP[x][y] = z;
  }
  memset(dp, -1, sizeof dp);
  ll ans = F(1, 1, 0);
  pfl(ans);
  return 0;
}

Error: TLE

Fixed submission:

// bismillahir rahmanir rahim            //Author:Fayed Anik

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

// #define         ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define lf double
#define pb(x) push_back(x)
#define ull unsigned long long
#define sfl(a) scanf("%lld", &a)
#define sf(a) scanf("%d", &a)
#define pf(a) printf("%d\\n", a)
#define pfl(a) printf("%lld\\n", a)
#define FOR(x, n) for (ll x = 1; x <= n; ++x)
#define pii pair<ll, ll>
#define mp(a, b) make_pair(a, b)
#define UNIQUE(v) v.resize(distance(v.begin(), unique(v.begin(), v.end())))
#define mod 1000000007
#define INF 2e18
#define EPS 1e-15
#define f1 first
#define f2 second
#define all(v) v.begin(), v.end()
#define PI acos(-1)
#define printminusone printf("-1\\n")
#define bug printf("bug")
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)

// ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
// ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
// bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
// priority_queue <ll, vector<ll>, greater<ll> > pq;
// int dx[]={0,0,1,-1,1,1,-1,-1};
// int dy[]={1,-1,0,0,1,-1,1,-1};

#define lim 3005

ll dp[lim][lim][4], r, c, k, v[200005];
map<pii, ll> MAP;

ll F(ll x, ll y, ll now) {
  if (x > r or y > c)
    return 0;
  ll &ret = dp[x][y][now];
  if (~ret)
    return ret;
  ret = 0;
  ll val = 0;
  if (MAP.find(mp(x, y)) != MAP.end()) {
    val = v[MAP[mp(x, y)]];
  }
  if (val and now < 3) {
    ret = val + F(x, y + 1, now + 1);
    ret = max(ret, val + F(x + 1, y, 0));
  }
  ret = max(ret, F(x, y + 1, now));
  ret = max(ret, F(x + 1, y, 0));
  return dp[x][y][now] = ret;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll x, y, z;
  sfl(r), sfl(c), sfl(k);
  FOR(i, k) {
    sfl(x), sfl(y), sfl(z);
    MAP[mp(x, y)] = i;
    v[i] = z;
  }
  memset(dp, -1, sizeof dp);
  ll ans = F(1, 1, 0);
  pfl(ans);
  return 0;
}