Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = []

for y in range(R):
    nowdp = []
    dp.append(nowdp)
    for x in range(C):
        tmp = []
        uemax = 0
        if y > 0:
            uemax = max(dp[y-1][x])
        if vss[y][x] is None:
            if x > 0:
                tmp.append(max(dp[y][x-1][0], uemax))
                tmp.append(dp[y][x-1][1])
                tmp.append(dp[y][x-1][2])
                tmp.append(dp[y][x-1][3])
            else:
                tmp.append(uemax)
                tmp.append(0)
                tmp.append(0)
                tmp.append(0)
        else:
            v = vss[y][x]
            if x > 0:
                tmp.append(max(dp[y][x-1][0], uemax))
                tmp.append(max(dp[y][x-1][0]+v, dp[y][x-1][1], uemax+v))
                tmp.append(max(dp[y][x-1][1]+v, dp[y][x-1][2]))
                tmp.append(max(dp[y][x-1][2]+v, dp[y][x-1][3]))
            else:
                tmp.append(uemax)
                tmp.append(uemax + v)
                tmp.append(0)
                tmp.append(0)
        nowdp.append(tmp)

print(max(dp[R-1][C-1]))


Fixed submission:
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = []

for y in range(R):
    ndp = []
    for x in range(C):
        tmp = []
        uemax = 0
        if y > 0:
            uemax = max(dp[x])
        if vss[y][x] is None:
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(left[1])
                tmp.append(left[2])
                tmp.append(left[3])
            else:
                tmp.append(uemax)
                tmp.append(0)
                tmp.append(0)
                tmp.append(0)
        else:
            v = vss[y][x]
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(max(left[0]+v, left[1], uemax+v))
                tmp.append(left[1]+v if left[1]+v > left[2] else left[2])
                tmp.append(left[2]+v if left[2]+v > left[3] else left[3])
            else:
                tmp.append(uemax)
                tmp.append(uemax + v)
                tmp.append(0)
                tmp.append(0)
        ndp.append(tmp)
    dp = ndp

print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = [[[0]*4 for __ in range(C)] for _ in range(R)]

for y in range(R):
    for x in range(C):
        uemax = 0
        if y > 0:
            uemax = max(dp[y-1][x])
        if vss[y][x] is None:
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = dp[y][x-1][1]
                dp[y][x][2] = dp[y][x-1][2]
                dp[y][x][3] = dp[y][x-1][3]
            else:
                dp[y][x][0] = uemax
                dp[y][x][1] = 0
                dp[y][x][2] = 0
                dp[y][x][3] = 0
        else:
            v = vss[y][x]
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = max(dp[y][x-1][0]+v, dp[y][x-1][1], uemax+v)
                dp[y][x][2] = max(dp[y][x-1][1]+v, dp[y][x-1][2])
                dp[y][x][3] = max(dp[y][x-1][2]+v, dp[y][x-1][3])
            else:
                dp[y][x][0] = uemax
                dp[y][x][1] = uemax + v
                dp[y][x][2] = 0
                dp[y][x][3] = 0

print(max(dp[R-1][C-1]))


Fixed submission:
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = []

for y in range(R):
    ndp = []
    for x in range(C):
        tmp = []
        uemax = 0
        if y > 0:
            uemax = max(dp[x])
        if vss[y][x] is None:
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(left[1])
                tmp.append(left[2])
                tmp.append(left[3])
            else:
                tmp.append(uemax)
                tmp.append(0)
                tmp.append(0)
                tmp.append(0)
        else:
            v = vss[y][x]
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(max(left[0]+v, left[1], uemax+v))
                tmp.append(left[1]+v if left[1]+v > left[2] else left[2])
                tmp.append(left[2]+v if left[2]+v > left[3] else left[3])
            else:
                tmp.append(uemax)
                tmp.append(uemax + v)
                tmp.append(0)
                tmp.append(0)
        ndp.append(tmp)
    dp = ndp

print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = [[[0]*4 for __ in range(C)] for _ in range(R)]

for y in range(R):
    for x in range(C):
        uemax = 0
        if y > 0:
            uemax = max(dp[y-1][x])
        if vss[y][x] is None:
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = dp[y][x-1][1]
                dp[y][x][2] = dp[y][x-1][2]
                dp[y][x][3] = dp[y][x-1][3]
            else:
                dp[y][x][0] = uemax
                dp[y][x][1] = 0
                dp[y][x][2] = 0
                dp[y][x][3] = 0
        else:
            v = vss[y][x]
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = max(dp[y][x-1][0]+v, dp[y][x-1][1], uemax+v)
                dp[y][x][2] = max(dp[y][x-1][1]+v, dp[y][x-1][2])
                dp[y][x][3] = max(dp[y][x-1][2]+v, dp[y][x-1][3])
            else:
                dp[y][x][0] = uemax
                dp[y][x][1] = uemax + v
                dp[y][x][2] = 0
                dp[y][x][3] = 0

print(max(dp[R-1][C-1]))


Fixed submission:
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = []

for y in range(R):
    ndp = []
    for x in range(C):
        tmp = []
        uemax = 0
        if y > 0:
            uemax = max(dp[x])
        if vss[y][x] is None:
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(left[1])
                tmp.append(left[2])
                tmp.append(left[3])
            else:
                tmp.append(uemax)
                tmp.append(0)
                tmp.append(0)
                tmp.append(0)
        else:
            v = vss[y][x]
            if x > 0:
                left = ndp[-1]
                tmp.append(left[0] if left[0] > uemax else uemax)
                tmp.append(max(left[0]+v, left[1], uemax+v))
                tmp.append(left[1]+v if left[1]+v > left[2] else left[2])
                tmp.append(left[2]+v if left[2]+v > left[3] else left[3])
            else:
                tmp.append(uemax)
                tmp.append(uemax + v)
                tmp.append(0)
                tmp.append(0)
        ndp.append(tmp)
    dp = ndp

print(max(dp[-1]))