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

dp = [[[-1]*4 for i in range(C+1)] for j in range(R+1)]
dp[0][0][0] = 0
if G[0][0] != None: dp[0][0][1] = G[0][0]

for r in range(R):
    for c in range(C):
        for k in range(4):
            if dp[r][c][k] == -1: continue

            if k < 3: # can go right and pick up
                if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                    dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
            if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
            if dp[r+1][c][0] < dp[r][c][k]:
                dp[r+1][c][0] = dp[r][c][k]
            if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]

print(max(dp[r][c]))


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)

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

dp = [[[-1]*4 for i in range(C)] for j in range(R)]
dp[0][0][0] = 0
if G[0][0] != None: dp[0][0][1] = G[0][0]

for r in range(R):
    for c in range(C):
        for k in range(4):
            if dp[r][c][k] == -1: continue

            if c+1<C and G[r][c+1] != None and k < 3: # can go right and pick up
                if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                    dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
            if c+1<C: # moving right without picking up
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
            if r+1<R: # moving downward without picking up
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
            if r+1<R and G[r+1][c] != None: # can go down and pick up
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]

print(max(dp[r][c]))


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)

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

dp = [[[-1]*4 for i in range(C)] for j in range(R)]
dp[0][0][0] = 0
if G[0][0] != None: dp[0][0][1] = G[0][0]

for r in range(R):
    for c in range(C):
        for k in range(4):
            if dp[r][c][k] == -1: continue

            if c+1<C and G[r][c+1] != None and k < 3: # can go right and pick up
                if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                    dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
            if c+1<C: # moving right without picking up
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
            if r+1<R: # moving downward without picking up
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
            if r+1<R and G[r+1][c] != None: # can go down and pick up
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]

print(max(dp[r][c]))


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)

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

dp = [[[-1]*4 for i in range(C)] for j in range(R)]
dp[0][0][0] = 0
if G[0][0] != None:
    dp[0][0][1] = G[0][0]

for r in range(R):
    for c in range(C):
        for k in range(4):
            if dp[r][c][k] == -1: continue

            if c+1<C and G[r][c+1] != None and k < 3: # can go right and pick up
                dp[r][c+1][k+1] = max(dp[r][c+1][k+1], dp[r][c][k] + G[r][c+1])
            if c+1<C: # moving right without picking up
                dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
            if r+1<R: # moving downward without picking up
                dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
            if r+1<R and G[r+1][c] != None: # can go right and pick up
                dp[r+1][c][1] = max(dp[r+1][c][1], dp[r][c][k] + G[r+1][c])

print(max(dp[r][c]))


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)

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

dp = [[[-1]*4 for i in range(C)] for j in range(R)]
dp[0][0][0] = 0
if G[0][0] != None:
    dp[0][0][1] = G[0][0]

for r in range(R):
    for c in range(C):
        for k in range(4):
            if dp[r][c][k] == -1: continue

            if c+1<C and G[r][c+1] != None and k < 3: # can go right and pick up
                dp[r][c+1][k+1] = max(dp[r][c+1][k+1], dp[r][c][k] + G[r][c+1])
            if c+1<C: # moving right without picking up
                dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
            if r+1<R: # moving downward without picking up
                dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
            if r+1<R and G[r+1][c] != None: # can go right and pick up
                dp[r+1][c][1] = max(dp[r+1][c][1], dp[r][c][k] + G[r+1][c])

print(max(dp[r][c]))


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)

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

dp = [[[0]*4 for i in range(C)] for j in range(R)]
path = [[[False]*4 for i in range(C)] for j in range(R)]
dp[0][0][0] = 0
path[0][0][0] = True
if G[0][0] != None:
    dp[0][0][1] = G[0][0]
    path[0][0][1] = True

for r in range(R):
    for c in range(C):
        for k in range(4):
            if path[r][c][k] == False: continue

            if c+1<C and G[r][c+1] != None and k < 3: # can go right and pick up
                dp[r][c+1][k+1] = max(dp[r][c+1][k+1], dp[r][c][k] + G[r][c+1])
                path[r][c+1][k+1] = True
            if c+1<C: # moving right without picking up
                dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
                path[r][c+1][k] = True
            if r+1<R: # moving downward without picking up
                dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
                path[r+1][c][0] = True
            if r+1<R and G[r+1][c] != None: # can go right and pick up
                dp[r+1][c][1] = max(dp[r+1][c][1], dp[r][c][k] + G[r+1][c])
                path[r+1][c][1] = True

print(max(dp[r][c]))
            


Fixed submission:
import numpy as np
from numba import jit

R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
    [r, c, v] = list(map(int, input().split()))
    G[r-1][c-1] = v

@jit
def main(G, R, C):
    dp = np.full((R+1, C+1, 4), -1)
    dp[0][0][0] = 0
    if G[0][0] != None: dp[0][0][1] = G[0][0]
    for r in range(R):
        for c in range(C):
            for k in range(4):
                if dp[r][c][k] == -1: continue
                if k < 3: # can go right and pick up
                    if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
                        dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
                if dp[r][c+1][k] < dp[r][c][k]:
                    dp[r][c+1][k] = dp[r][c][k]
                if dp[r+1][c][0] < dp[r][c][k]:
                    dp[r+1][c][0] = dp[r][c][k]
                if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
                    dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
    return(max(dp[r][c]))

ret = main(G, R, C)
print(ret)