Language: C++

#include <bits/stdc++.h>
using namespace std;

using lint = long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define repu(i, a, b, s) for(int i = int(a); i < int(b); i += int(s))
#define repd(i, a, b, s) for(int i = int(a); i > int(b); i += int(s))
#define endl "\n"
constexpr lint INF = 2e18;
constexpr lint MOD = 1e9 + 7;

lint grid[100000][100000];
lint dp[3001][3001][4];

void solve() {
    lint R, C, K;
    cin >> R >> C >> K;

    rep(i, R) {
        rep(j, C) { grid[i][j] = -1; }
    }

    rep(i, K) {
        lint r, c, v;
        cin >> r >> c >> v;
        r--;
        c--;
        grid[r][c] = v;
    }

    rep(y, R) {
        rep(x, C) {
            rep(i, 4) { dp[y][x][i] = 0; }
        }
    }

    if(grid[0][0] != -1)
        dp[0][0][1] = grid[0][0];

    rep(y, R) {
        rep(x, C) {

            // 選ばない
            if(0 <= y - 1) {
                rep(i, 4) { dp[y][x][0] = max(dp[y][x][0], dp[y - 1][x][i]); }
            }
            if(0 <= x - 1) {
                rep(i, 4) { dp[y][x][i] = max(dp[y][x][i], dp[y][x - 1][i]); }
            }

            // 選ぶ
            if(grid[y][x] != -1) {
                if(0 <= y - 1) {
                    rep(i, 4) {
                        dp[y][x][1] =
                            max(dp[y][x][1], dp[y - 1][x][i] + grid[y][x]);
                    }
                }
            }
            if(0 <= x - 1) {
                repu(i, 1, 4, 1) {
                    dp[y][x][i] =
                        max(dp[y][x][i], dp[y][x - 1][i - 1] + grid[y][x]);
                }
            }
        }
    }

    lint ans = -INF;
    rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }
    cout << ans << endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
    solve();
    return 0;
}


Compiler output:
/tmp/cciVqgP7.o: in function `solve()':
a.cc:(.text+0x177): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x1be): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x220): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x246): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x27b): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x2d7): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x30b): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x34e): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x3dc): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x433): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/cciVqgP7.o
a.cc:(.text+0x46c): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status