Language: C++

#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 {
#ifndef ONLINE_JUDGE
~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__) << "] "
using ll = long long;
const int nax = 1e4+500;
const int INF = 1e9+100;
ll visited[nax][nax][5];
vector<vector<int>> grid(nax,vector<int> (nax));
int R,C,K;
ll ff(int i, int j, ll sum, int taken) {
	if(i<0 || i>=R || j<0 || j>=C) return 0;
	if(visited[i][j][taken]) return visited[i][j][taken];
	if(i == R-1 && j == C-1) {
		if(taken < 3) return sum+grid[i][j];
		else return grid[i][j];
	}
	ll right = 0, down = 0;
	if(taken != 3) right = ff(i,j+1,sum+grid[i][j], taken+1);
	if(taken!=3) down = ff(i+1,j,sum+grid[i][j], 0);
	ll righte = ff(i,j+1,sum,taken);
	ll downe = ff(i+1,j,sum,0);
	ll ret = max({right,down,downe,righte});
	return visited[i][j][taken] = ret;
	
}
int main() {
	cin >> R >> C >> K;
	for(int i=0;i<K;i++) {
		int x,y,val;
		cin >> x >> y >> val;
		--x,--y;
		grid[x][y] = val;
	}
	memset(visited,-1,sizeof(visited));
	ll ans = ff(0,0,0,0);
	cout<<ans;
}


Compiler output:
/tmp/ccI5Qsca.o: in function `ff(int, int, long long, int)':
a.cc:(.text+0x25): relocation truncated to fit: R_X86_64_PC32 against symbol `R' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x36): relocation truncated to fit: R_X86_64_PC32 against symbol `C' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0xd1): relocation truncated to fit: R_X86_64_PC32 against symbol `R' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0xe3): relocation truncated to fit: R_X86_64_PC32 against symbol `C' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x103): relocation truncated to fit: R_X86_64_PC32 against symbol `grid' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x13e): relocation truncated to fit: R_X86_64_PC32 against symbol `grid' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x193): relocation truncated to fit: R_X86_64_PC32 against symbol `grid' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x1e8): relocation truncated to fit: R_X86_64_PC32 against symbol `grid' defined in .bss section in /tmp/ccI5Qsca.o
/tmp/ccI5Qsca.o: in function `main':
a.cc:(.text+0x340): relocation truncated to fit: R_X86_64_PC32 against symbol `R' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x35c): relocation truncated to fit: R_X86_64_PC32 against symbol `C' defined in .bss section in /tmp/ccI5Qsca.o
a.cc:(.text+0x371): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status