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

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mx 3002
mt19937 myrand(time(0));

int r, c, k;
ll a[mx][mx];
ll dp[mx][mx][5];

ll call(ll x, ll y, ll taken) {
  if (x > r || y > c)
    return 0;
  ll &ret = dp[x][y][taken];
  if (ret != -1)
    return ret;
  ret = 0;
  ret = max(ret, ((taken < 3) ? a[x][y] : 0) + call(x + 1, y, 0));
  if (taken < 3)
    ret = max(ret, a[x][y] + call(x, y + 1, taken + 1));
  ret = max(ret, call(x, y + 1, taken));
  return ret;
}

int main() {
  scanf("%d %d %d", &r, &c, &k);
  for (int i = 0; i < k; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    a[x][y] = z;
  }
  memset(dp, -1, sizeof dp);
  ll ans = call(1, 1, 0);
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mx 3002
mt19937 myrand(time(0));

int r, c, k;
vector<pair<int, int>> a[mx];
ll dp[mx][mx], idx[mx][mx];

ll call(ll x, ll y) {
  if (x > r)
    return 0;
  ll &ret = dp[x][y];
  if (ret != -1)
    return ret;
  ret = 0;
  ll mx1 = 0, mx2 = 0, mx3 = 0;
  for (int i = idx[x][y]; i < (int)a[x].size(); i++) {
    if (mx1 < a[x][i].ss) {
      mx3 = mx2;
      mx2 = mx1;
      mx1 = a[x][i].ss;
    } else if (mx2 < a[x][i].ss) {
      mx3 = mx2;
      mx2 = a[x][i].ss;
    } else if (mx3 < a[x][i].ss)
      mx3 = a[x][i].ss;
    ret = max(ret, mx1 + mx2 + mx3 + call(x + 1, a[x][i].ff));
  }
  ret = max(ret, call(x + 1, y));
  return ret;
}

int main() {
  scanf("%d %d %d", &r, &c, &k);
  for (int i = 0; i < k; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    a[x].pb({y, z});
  }
  for (int i = 1; i <= r; i++) {
    sort(all(a[i]));
    int j = 1;
    for (int l = 0; l < (int)a[i].size(); l++) {
      while (j <= a[i][l].ff)
        idx[i][j] = l, j++;
    }
    while (j <= c)
      idx[i][j] = a[i].size(), j++;
  }
  memset(dp, -1, sizeof dp);
  ll ans = call(1, 1);
  printf("%lld\\n", ans);
  return 0;
}