Buggy python submission (verdict: Time Limit Exceeded):
r, c, k = map(int, input().split())
dp = [[[0] * 4 for _ in range(c+1)] for _ in range(r+1)]

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        p = (y, x)
        prev_y_max = max(dp[y-1][x][:])
        dp[y][x][0] = max(prev_y_max, dp[y][x-1][0])
        if p in v_dict:
            v = v_dict[p]
            dp[y][x][1] = max(prev_y_max + v, dp[y][x-1][0] + v, dp[y][x-1][1])
            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:
            for i in range(1, 4):
                dp[y][x][i] = dp[y][x-1][i]
print(max(dp[r][c]))


Fixed submission:
r, c, k = map(int, input().split())
dp = [[[0] * (c+1) for _ in range(r+1)] for _ in range(4)]

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        prev_y_max = 0
        for i in range(4):
            prev_y_max = max(dp[i][y-1][x], prev_y_max)
        dp[0][y][x] = max(prev_y_max, dp[0][y][x-1])
        if (y, x) in v_dict:
            v = v_dict[(y, x)]
            dp[1][y][x] = max(prev_y_max + v, dp[0][y][x-1] + v, dp[1][y][x-1])
            dp[2][y][x] = max(dp[1][y][x-1] + v, dp[2][y][x-1])
            dp[3][y][x] = max(dp[2][y][x-1] + v, dp[3][y][x-1])
        else:
            for i in range(1, 4):
                dp[i][y][x] = dp[i][y][x-1]
ans = 0
for i in range(4):
    ans = max(dp[i][r][c], ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
r, c, k = map(int, input().split())
dp = [[[0] * 4 for _ in range(c+1)] for _ in range(r+1)]

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        p = (y, x)
        prev_y_max = max(dp[y-1][x][:])
        dp[y][x][0] = max(prev_y_max, dp[y][x-1][0])
        if p in v_dict:
            v = v_dict[p]
            dp[y][x][1] = max(prev_y_max + v, dp[y][x-1][0] + v, dp[y][x-1][1])
            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:
            for i in range(1, 4):
                dp[y][x][i] = dp[y][x-1][i]
print(max(dp[r][c]))


Fixed submission:
r, c, k = map(int, input().split())
dp = [[[0] * (c+1) for _ in range(r+1)] for _ in range(4)]

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        prev_y_max = 0
        for i in range(4):
            prev_y_max = max(dp[i][y-1][x], prev_y_max)
        dp[0][y][x] = max(prev_y_max, dp[0][y][x-1])
        if (y, x) in v_dict:
            v = v_dict[(y, x)]
            dp[1][y][x] = max(prev_y_max + v, dp[0][y][x-1] + v, dp[1][y][x-1])
            dp[2][y][x] = max(dp[1][y][x-1] + v, dp[2][y][x-1])
            dp[3][y][x] = max(dp[2][y][x-1] + v, dp[3][y][x-1])
        else:
            for i in range(1, 4):
                dp[i][y][x] = dp[i][y][x-1]
ans = 0
for i in range(4):
    ans = max(dp[i][r][c], ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
r, c, k = map(int, input().split())
grid = [[0] * c for _ in range(r)]
dp = [[[0] * 4 for _ in range(c+1)] for _ in range(r+1)]

for _ in range(k):
    y, x, v = map(int, input().split())
    grid[y-1][x-1] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        v = grid[y-1][x-1]
        prev_y_max = max(dp[y-1][x][:])
        dp[y][x][0] = max(prev_y_max, dp[y][x-1][0])
        if v != 0:
            dp[y][x][1] = max(prev_y_max + v, dp[y][x-1][0] + v, dp[y][x-1][1])
            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:
            for i in range(1, 4):
                dp[y][x][i] = dp[y][x-1][i]
print(max(dp[r][c]))


Fixed submission:
r, c, k = map(int, input().split())
dp = [[[0] * (c+1) for _ in range(r+1)] for _ in range(4)]

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        prev_y_max = 0
        for i in range(4):
            prev_y_max = max(dp[i][y-1][x], prev_y_max)
        dp[0][y][x] = max(prev_y_max, dp[0][y][x-1])
        if (y, x) in v_dict:
            v = v_dict[(y, x)]
            dp[1][y][x] = max(prev_y_max + v, dp[0][y][x-1] + v, dp[1][y][x-1])
            dp[2][y][x] = max(dp[1][y][x-1] + v, dp[2][y][x-1])
            dp[3][y][x] = max(dp[2][y][x-1] + v, dp[3][y][x-1])
        else:
            for i in range(1, 4):
                dp[i][y][x] = dp[i][y][x-1]
ans = 0
for i in range(4):
    ans = max(dp[i][r][c], ans)
print(ans)