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

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for k in range(4):
        for j in range(C):
            if i < R - 1:
                dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
                dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][k] + goods[i+1][j])
        
            if j < C - 1:
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
                if k < 3:
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])

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

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        for k in range(4):
            if i < R - 1:
                dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
                dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][k] + goods[i+1][j])
        
            if j < C - 1:
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
                if k < 3:
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])

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

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i < R - 1:
            dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i+1][j])
        
        if j < C - 1:
            for k in range(4):
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
            for k in range(3):
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])

Buggy python submission (verdict: Runtime Error):
R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i < R - 1:
            dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i+1][j])
        
        if j < C - 1:
            for k in range(4):
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
            for k in range(3):
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

print(max(dp[R-1][C-1]))R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i < R - 1:
            dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i+1][j])
        
        if j < C - 1:
            for k in range(4):
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
            for k in range(3):
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i < R - 1:
            dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i+1][j])
        
        if j < C - 1:
            for k in range(4):
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
            for k in range(3):
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])

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

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i < R - 1:
            dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i+1][j])
        
        if j < C - 1:
            for k in range(4):
                dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
            for k in range(3):
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j+1])

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

Fixed submission:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(C+1)] for j in range(4)] for k in range(R+1)]
 
for _ in range(K):
    r, c, v = map(int,input().split())
    MAP[r-1][c-1] = v
 
for i in range(R):
    for j in range(4):
        for k in range(C):
            DP[i+1][j][k+1] = max(DP[i][3][k+1], DP[i+1][j][k])
            if j == 0:
                continue
            v = MAP[i][k]
            if v == 0:
                continue
            DP[i+1][j][k+1] = max(DP[i+1][j][k+1], max(DP[i][3][k+1], DP[i+1][j-1][k]) + v)

print(DP[R][3][C])