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

/*input
4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \\
                                                    |     |   )   \\
                                                    |_____|_______)

*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define out(container)                                                         \\
  for (auto it : container)                                                    \\
    cout << it << " ";                                                         \\
  cout << endl;

template <typename T> T GCD(T a, T b) {
  ll t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }

const int N = 3e3 + 5;

int arr[N][N];
int n, m, k;

ll dp[N][N][4];

ll fun(int r, int c, int cur) {
  if (r > n || c > m)
    return -LL_INF;
  if (r == n && c == m) {
    if (cur < 3)
      return arr[r][c];
    return 0;
  }
  if (~dp[r][c][cur])
    return dp[r][c][cur];
  ll ans = 0;
  if (arr[r][c]) {
    if (cur < 3) {
      ans = max(ans, arr[r][c] + fun(r, c + 1, cur + 1));
      ans = max(ans, arr[r][c] + fun(r + 1, c, 0));
    }
    ans = max(ans, fun(r, c + 1, cur));
    ans = max(ans, fun(r + 1, c, 0));
  } else {
    ans = max(ans, fun(r, c + 1, cur));
    ans = max(ans, fun(r + 1, c, 0));
  }
  return dp[r][c][cur] = ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m >> k;
  int r, c, v;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    arr[r][c] = v;
  }

  memset(dp, -1, sizeof(dp));

  ll ans = fun(1, 1, 0);

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

/*input
4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \\
                                                    |     |   )   \\
                                                    |_____|_______)

*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define out(container)                                                         \\
  for (auto it : container)                                                    \\
    cout << it << " ";                                                         \\
  cout << endl;

template <typename T> T GCD(T a, T b) {
  ll t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }

const int N = 3e3 + 5;

int arr[N][N];
int n, m, k;

ll dp[N][N][4];

ll fun(int r, int c, int cur) {
  if (r > n || c > m)
    return -LL_INF;
  if (r == n && c == m) {
    if (cur < 3)
      return arr[r][c];
    return 0;
  }
  ll &ans = dp[r][c][cur];

  if (~ans)
    return ans;

  for (int j = c; j <= m; j++) {
    if (cur < 3) {
      ans = max(ans, arr[r][j] + fun(r, j + 1, cur + 1));
      ans = max(ans, arr[r][j] + fun(r + 1, j, 0));
    }
    ans = max(ans, fun(r, j + 1, cur));
    ans = max(ans, fun(r + 1, j, 0));
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m >> k;
  int r, c, v;
  for (int i = 0; i < k; i++) {
    cin >> r >> c >> v;
    arr[r][c] = v;
  }

  memset(dp, -1, sizeof(dp));

  ll ans = fun(1, 1, 0);

  cout << ans << endl;

  return 0;
}