Buggy python submission (verdict: Time Limit Exceeded):
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    M = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        M[r - 1][c - 1] = v

    # dp[i][j][k]=(i,j)にいてその行でk個取得済みの場合の価値の最大値
    dp = [[[-1] * 4 for _ in range(C)] for __ in range(R)]
    dp[0][0][0] = 0
    dp[0][0][1] = M[0][0]
    for i in range(R):
        for j in range(C):
            now = dp[i][j]
            for k in range(4):
                nowk = now[k]
                if nowk < 0: continue
                # 下へ
                if i < R - 1:
                    down = dp[i + 1][j]
                    # 取らない
                    down[0] = max(down[0], nowk)
                    # 取る
                    v = M[i + 1][j]
                    if v > 0:
                        down[1] = max(down[1], nowk + v)
                # 右へ
                if j < C - 1:
                    right = dp[i][j + 1]
                    # 取らない
                    right[k] = max(right[k], nowk)
                    # 取る
                    v = M[i][j + 1]
                    if v > 0 and k < 3:
                        right[k + 1] = max(right[k + 1], nowk + v)
    print(max(dp[R - 1][C - 1]))

if __name__ == '__main__':
    main()


Fixed submission:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    M = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        M[r - 1][c - 1] = v

    # dp[i][j][k]=(i,j)にいてその行でk個取得済みの場合の価値の最大値
    dp = [[-1] * 4 for _ in range(C)]
    dp[0][0] = 0
    dp[0][1] = M[0][0]
    for i in range(R):
        nxt = [[-1] * 4 for _ in range(C)]
        for j in range(C):
            now = dp[j]
            for k in range(4):
                nowk = now[k]
                if nowk < 0: continue
                # 下へ
                if i < R - 1:
                    down = nxt[j]
                    # 取らない
                    down[0] = max(down[0], nowk)
                    # 取る
                    v = M[i + 1][j]
                    if v > 0:
                        down[1] = max(down[1], nowk + v)
                # 右へ
                if j < C - 1:
                    right = dp[j + 1]
                    # 取らない
                    right[k] = max(right[k], nowk)
                    # 取る
                    v = M[i][j + 1]
                    if v > 0 and k < 3:
                        right[k + 1] = max(right[k + 1], nowk + v)
        if i < R - 1: dp = nxt
    print(max(dp[C - 1]))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(R, C, K, M):
    # dp[i][j][k]=(i,j)にいてその行でk個取得済みの場合の価値の最大値
    dp = [[[-1] * 4 for _ in range(C)] for __ in range(R)]
    dp[0][0][0] = 0
    dp[0][0][1] = M[0][0]
    for i in range(R):
        for j in range(C):
            now = dp[i][j]
            for k in range(4):
                if now[k] < 0: continue
                # 下へ
                if i < R - 1:
                    # 取らない
                    dp[i + 1][j][0] = max(dp[i + 1][j][0], now[k])
                    # 取る
                    v = M[i + 1][j]
                    if v > 0:
                        dp[i + 1][j][1] = max(dp[i + 1][j][1], now[k] + v)
                # 右へ
                if j < C - 1:
                    # 取らない
                    dp[i][j + 1][k] = max(dp[i][j + 1][k], now[k])
                    # 取る
                    v = M[i][j + 1]
                    if v > 0 and k < 3:
                        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], now[k] + v)
    print(max(dp[R - 1][C - 1]))
    # for d in dp:
    #    print(d)

if __name__ == '__main__':
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    M = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        M[r - 1][c - 1] = v
    main(R, C, K, M)


Fixed submission:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    M = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        M[r - 1][c - 1] = v

    # dp[i][j][k]=(i,j)にいてその行でk個取得済みの場合の価値の最大値
    dp = [[-1] * 4 for _ in range(C)]
    dp[0][0] = 0
    dp[0][1] = M[0][0]
    for i in range(R):
        nxt = [[-1] * 4 for _ in range(C)]
        for j in range(C):
            now = dp[j]
            for k in range(4):
                nowk = now[k]
                if nowk < 0: continue
                # 下へ
                if i < R - 1:
                    down = nxt[j]
                    # 取らない
                    down[0] = max(down[0], nowk)
                    # 取る
                    v = M[i + 1][j]
                    if v > 0:
                        down[1] = max(down[1], nowk + v)
                # 右へ
                if j < C - 1:
                    right = dp[j + 1]
                    # 取らない
                    right[k] = max(right[k], nowk)
                    # 取る
                    v = M[i][j + 1]
                    if v > 0 and k < 3:
                        right[k + 1] = max(right[k + 1], nowk + v)
        if i < R - 1: dp = nxt
    print(max(dp[C - 1]))

if __name__ == '__main__':
    main()