Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[[0]*4 for j in range(c)] for i in range(r)]
for i in range(r):
    for j in range(c):
        if vals[i][j] != 0:
            for k in range(3, 0, -1):
                if dp[i][j][k] < dp[i][j][k-1] + vals[i][j]:
                    dp[i][j][k] = dp[i][j][k-1] + vals[i][j]
        if i < r-1:
            tmp = 0
            for k in range(4):
                if tmp < dp[i][j][k]:
                    tmp = dp[i][j][k]
            dp[i+1][j][0] = tmp
        if j < c-1:
            for k in range(4):
                if dp[i][j+1][k] < dp[i][j][k]:
                    dp[i][j+1][k] = dp[i][j][k]
print(max(dp[r-1][c-1]))


Fixed submission:
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[0]*4 for j in range(c)]
for j in range(c):
    if j > 0:
        for k in range(4):
            dp[j][k] = dp[j-1][k]
    if vals[0][j] > 0:
        for k in reversed(range(3)):
            if dp[j][k+1] < dp[j][k] + vals[0][j]:
                dp[j][k+1] = dp[j][k] + vals[0][j]
for i in range(1, r):
    tmp = [[max(dp[j]), 0, 0, 0] for j in range(c)]
    for j in range(c):
        if j > 0:
            for k in range(4):
                if tmp[j][k] < tmp[j-1][k]:
                    tmp[j][k] = tmp[j-1][k]
        if vals[i][j] > 0:
            for k in reversed(range(3)):
                if tmp[j][k+1] < tmp[j][k] + vals[i][j]:
                    tmp[j][k+1] = tmp[j][k] + vals[i][j]
    dp = tmp
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[[0]*4 for j in range(c)] for i in range(r)]
for i in range(r):
    for j in range(c):
        if i > 0:
            tmp = 0
            for k in range(4):
                if tmp < dp[i-1][j][k]:
                    tmp = dp[i-1][j][k]
            dp[i][j][0] = tmp
        if j > 0:
            for k in range(4):
                if dp[i][j][k] < dp[i][j-1][k]:
                    dp[i][j][k] = dp[i][j-1][k]
        if vals[i][j] != 0:
            for k in range(3, 0, -1):
                if dp[i][j][k] < dp[i][j][k-1] + vals[i][j]:
                    dp[i][j][k] = dp[i][j][k-1] + vals[i][j]
print(max(dp[r-1][c-1]))

Fixed submission:
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[0]*4 for j in range(c)]
for j in range(c):
    if j > 0:
        for k in range(4):
            dp[j][k] = dp[j-1][k]
    if vals[0][j] > 0:
        for k in reversed(range(3)):
            if dp[j][k+1] < dp[j][k] + vals[0][j]:
                dp[j][k+1] = dp[j][k] + vals[0][j]
for i in range(1, r):
    tmp = [[max(dp[j]), 0, 0, 0] for j in range(c)]
    for j in range(c):
        if j > 0:
            for k in range(4):
                if tmp[j][k] < tmp[j-1][k]:
                    tmp[j][k] = tmp[j-1][k]
        if vals[i][j] > 0:
            for k in reversed(range(3)):
                if tmp[j][k+1] < tmp[j][k] + vals[i][j]:
                    tmp[j][k+1] = tmp[j][k] + vals[i][j]
    dp = tmp
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[[0]*4 for j in range(c)] for i in range(r)]
for i in range(r):
    for j in range(c):
        if i > 0:
            tmp = 0
            for k in range(4):
                if tmp < dp[i-1][j][k]:
                    tmp = dp[i-1][j][k]
            dp[i][j][0] = tmp
        if j > 0:
            for k in range(4):
                if dp[i][j][k] < dp[i][j-1][k]:
                    dp[i][j][k] = dp[i][j-1][k]
        for k in range(3, 0, -1):
            if dp[i][j][k] < dp[i][j][k-1] + vals[i][j]:
                dp[i][j][k] = dp[i][j][k-1] + vals[i][j]
print(max(dp[r-1][c-1]))

Fixed submission:
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[0]*4 for j in range(c)]
for j in range(c):
    if j > 0:
        for k in range(4):
            dp[j][k] = dp[j-1][k]
    if vals[0][j] > 0:
        for k in reversed(range(3)):
            if dp[j][k+1] < dp[j][k] + vals[0][j]:
                dp[j][k+1] = dp[j][k] + vals[0][j]
for i in range(1, r):
    tmp = [[max(dp[j]), 0, 0, 0] for j in range(c)]
    for j in range(c):
        if j > 0:
            for k in range(4):
                if tmp[j][k] < tmp[j-1][k]:
                    tmp[j][k] = tmp[j-1][k]
        if vals[i][j] > 0:
            for k in reversed(range(3)):
                if tmp[j][k+1] < tmp[j][k] + vals[i][j]:
                    tmp[j][k+1] = tmp[j][k] + vals[i][j]
    dp = tmp
print(max(dp[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    r, c, k = map(int, input().split())
    vals = [[0]*c for _ in range(r)]
    for _ in range(k):
        x, y, v = map(int, input().split())
        vals[x-1][y-1] = v
    dp = [[[0]*4 for j in range(c)] for i in range(r)]
    for i in range(r):
        for j in range(c):
            if i > 0:
                tmp = 0
                for k in range(4):
                    if tmp < dp[i-1][j][k]:
                        tmp = dp[i-1][j][k]
                dp[i][j][0] = tmp
            if j > 0:
                for k in range(4):
                    if dp[i][j][k] < dp[i][j-1][k]:
                        dp[i][j][k] = dp[i][j-1][k]
            for k in range(3, 0, -1):
                if dp[i][j][k] < dp[i][j][k-1] + vals[i][j]:
                    dp[i][j][k] = dp[i][j][k-1] + vals[i][j]
    print(max(dp[r-1][c-1]))

if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin
input = stdin.readline

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[0]*4 for j in range(c)]
for j in range(c):
    if j > 0:
        for k in range(4):
            dp[j][k] = dp[j-1][k]
    if vals[0][j] > 0:
        for k in reversed(range(3)):
            if dp[j][k+1] < dp[j][k] + vals[0][j]:
                dp[j][k+1] = dp[j][k] + vals[0][j]
for i in range(1, r):
    tmp = [[max(dp[j]), 0, 0, 0] for j in range(c)]
    for j in range(c):
        if j > 0:
            for k in range(4):
                if tmp[j][k] < tmp[j-1][k]:
                    tmp[j][k] = tmp[j-1][k]
        if vals[i][j] > 0:
            for k in reversed(range(3)):
                if tmp[j][k+1] < tmp[j][k] + vals[i][j]:
                    tmp[j][k+1] = tmp[j][k] + vals[i][j]
    dp = tmp
print(max(dp[-1]))