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

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
// DEBUG TEMPLATE
#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__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}
int r, c;

int solve2(int x, int y, int row, vector<vector<int>> &grid,
           vector<vector<vector<int>>> &dp) {
  int ret = 0;
  if (x == r or y == c)
    return 0;
  if (dp[x][y][row] != -1)
    return dp[x][y][row];
  if (grid[x][y] == 0) {
    ret = max(ret, solve2(x + 1, y, 3, grid, dp));
    ret = max(ret, solve2(x, y + 1, row, grid, dp));
  } else {
    if (row == 0) {
      ret = max(ret, solve2(x + 1, y, 3, grid, dp));
    } else {
      ret = max(ret, max(solve2(x, y + 1, row, grid, dp),
                         solve2(x, y + 1, row - 1, grid, dp) + grid[x][y]));
      ret = max(ret, solve2(x + 1, y, 3, grid, dp) + grid[x][y]);
    }
  }
  // if (x == 0 and y == 1)
  // debug() << imie(x) imie(y) imie(row) imie(ret);
  dp[x][y][row] = ret;
  return ret;
  ;
}

void solve() {
  int k;
  cin >> r >> c >> k;
  vector<vector<int>> grid(r, vector<int>(c, 0));
  vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(4, -1)));
  for (int i = 0; i < k; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    grid[--x][--y] += v;
  }
  debug() << imie(grid);
  // debug() << imie(solve2(0,3,3,grid));
  int ans = solve2(0, 0, 3, grid, dp);
  cout << ans << endl;
}

/*

*/
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
// DEBUG TEMPLATE
#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__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}
int r, c;
int solve2(int x, int y, int row, vector<vector<int>> &grid) {
  int ret = 0;
  if (x == r or y == c)
    return 0;
  if (grid[x][y] == 0) {
    ret = max(ret, solve2(x + 1, y, 3, grid));
    ret = max(ret, solve2(x, y + 1, row, grid));
  } else {
    if (row == 0) {
      ret = max(ret, solve2(x + 1, y, 3, grid));
    } else {
      ret = max(ret, max(solve2(x, y + 1, row, grid),
                         solve2(x, y + 1, row - 1, grid) + grid[x][y]));
      ret = max(ret, solve2(x + 1, y, 3, grid) + grid[x][y]);
    }
  }
  // if (x == 0 and y == 1)
  // debug() << imie(x) imie(y) imie(row) imie(ret);
  return ret;
  ;
}

void solve() {
  int k;
  cin >> r >> c >> k;
  vector<vector<int>> grid(r, vector<int>(c, 0));
  for (int i = 0; i < k; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    grid[--x][--y] += v;
  }
  debug() << imie(grid);
  // debug() << imie(solve2(0,3,3,grid));
  int ans = solve2(0, 0, 3, grid);
  cout << ans << endl;
}

/*

*/
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(