Language: C++

#include <bits/stdc++.h>
using namespace  std;
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

	
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define re(n) for (int i = 0; i < n; ++i)
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define eb emplace_back

//OutPut
#define ps(x) cout << x << endl;
#define ps_end(x) {ps(x); return;}


const int md = 1e9 + 7; //998244353 ;
const int dir[4][2] = {{-1,0}, {1, 0}, {0, -1}, {0, 1}};
template<class T> bool ckmin(T& a, const T& b) { 
	return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { 
	return a < b ? a = b, 1 : 0; }
ll pw(auto x, auto y) {	 ll s = 1; while(y) { if (y & 1) s = s * x % md; x= 1LL * x * x % md; y /= 2; } return s;}

const int maxn = 5e3 + 10;

int n, m, nq, nk;
string str;
int a[maxn][maxn];
ll dp[maxn][maxn][4];
void task() {
	int r, c, k;
	cin >> r >> c >> k;
	re(k) {
		int x, y, z;
		cin >> x >> y >> z;
		x--; y--;
		a[x][y] = z;
	};
	dp[0][0][0] = 0;
	dp[0][0][1] = a[0][0];
	for (int i = 0; i < r; ++i) {
		for (int j = 0; j < c; ++j) 
		  for (int k = 0; k < 4; ++k) {
				if (j < c - 1)  ckmax(dp[i][j+1][k], dp[i][j][k]);
				if (j < c - 1 && k < 3)  ckmax(dp[i][j+1][k+1], dp[i][j][k]+a[i][j+1]);				
				if (i < r - 1) ckmax(dp[i+1][j][0], dp[i][j][k]);
			}
	}
	int ans = 0;
	re(4) ckmax(ans, dp[r-1][c-1][i]);
	ps(ans);
	//Read the stuff at the bottom
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout<<fixed<<setprecision(20);
  int T = 1;
  //cin >> T;
  for (int ti = 1; ti <= T; ++ti) {
    task();
  }
  return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* write down
*/


Compiler output:
a.cc:58:7: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   58 | ll pw(auto x, auto y) {  ll s = 1; while(y) { if (y & 1) s = s * x % md; x= 1LL * x * x % md; y /= 2; } return s;}
      |       ^~~~
a.cc:58:15: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   58 | ll pw(auto x, auto y) {  ll s = 1; while(y) { if (y & 1) s = s * x % md; x= 1LL * x * x % md; y /= 2; } return s;}
      |               ^~~~
a.cc: In function 'void task()':
a.cc:86:20: error: no matching function for call to 'ckmax(int&, ll&)'
   86 |         re(4) ckmax(ans, dp[r-1][c-1][i]);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:56:24: note: candidate: 'template<class T> bool ckmax(T&, const T&)'
   56 | template<class T> bool ckmax(T& a, const T& b) {
      |                        ^~~~~
a.cc:56:24: note:   template argument deduction/substitution failed:
a.cc:86:20: note:   deduced conflicting types for parameter 'const T' ('int' and 'll' {aka 'long long int'})
   86 |         re(4) ckmax(ans, dp[r-1][c-1][i]);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~