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

def main():
    r,c,k=map(int,input().split())

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

    dp=[[[-10**10]*4 for i in range(c)] for j in range(r)]

    if item[0][0]==0:
        dp[0][0][0]=0
    else:
        dp[0][0][0]=0
        dp[0][0][1]=item[0][0]

    for i in range(r):
        for j in range(c):
            if j!=c-1:
                v=item[i][j+1]
                if v==0:
                    for k in range(4):
                        dp[i][j+1][k]=max(dp[i][j][k],dp[i][j+1][k])
                else:
                    dp[i][j+1][0]=max(dp[i][j][0],dp[i][j+1][0])
                    dp[i][j+1][1]=max(dp[i][j][0]+v,dp[i][j][1],dp[i][j+1][1])
                    dp[i][j + 1][2] = max(dp[i][j][1] + v, dp[i][j][2], dp[i][j + 1][2])
                    dp[i][j + 1][3] = max(dp[i][j][2] + v, dp[i][j][3], dp[i][j + 1][3])
            if i!=r-1:
                v=item[i+1][j]
                if v==0:
                    dp[i+1][j][0]=max(dp[i][j][0],dp[i][j][1],dp[i][j][2],dp[i][j][3],dp[i+1][j][0])
                else:
                    dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3], dp[i + 1][j][0])
                    dp[i + 1][j][1] = max(dp[i][j][0]+v, dp[i][j][1]+v, dp[i][j][2]+v, dp[i][j][3]+v, dp[i + 1][j][1])

    ans=max(dp[r-1][c-1][0],dp[r-1][c-1][1],dp[r-1][c-1][2],dp[r-1][c-1][3])
    print(ans)

if __name__=='__main__':
    main()

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

def main():
    r,c,k=map(int,input().split())

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

    dp0=[[-10**10]*c  for i in range(r)]
    dp1 = [[-10 ** 10] * c for i in range(r)]
    dp2 = [[-10 ** 10] * c for i in range(r)]
    dp3 = [[-10 ** 10] * c for i in range(r)]

    if item[0][0]==0:
        dp0[0][0]=0
    else:
        dp0[0][0]=0
        dp1[0][0]=item[0][0]

    for i in range(r):
        for j in range(c):
            if j!=c-1:
                v=item[i][j+1]
                if v==0:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j + 1] = max(dp1[i][j], dp1[i][j + 1])
                    dp2[i][j + 1] = max(dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp3[i][j], dp3[i][j + 1])
                else:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j+1]=max(dp0[i][j]+v,dp1[i][j],dp1[i][j+1])
                    dp2[i][j + 1] = max(dp1[i][j] + v, dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp2[i][j] + v, dp3[i][j], dp3[i][j + 1])
            if i!=r-1:
                v=item[i+1][j]
                if v==0:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                else:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                    dp1[i + 1][j] = max(dp0[i][j]+v, dp1[i][j]+v, dp2[i][j]+v, dp3[i][j]+v, dp1[i + 1][j])

    ans=max(dp0[r-1][c-1],dp1[r-1][c-1],dp2[r-1][c-1],dp3[r-1][c-1])
    print(ans)

if __name__=='__main__':
    main()

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

r,c,k=map(int,input().split())

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

dp=[[[-10**10]*4 for i in range(c)] for j in range(r)]

if item[0][0]==0:
    dp[0][0][0]=0
else:
    dp[0][0][0]=0
    dp[0][0][1]=item[0][0]

for i in range(r):
    for j in range(c):
        if j!=c-1:
            v=item[i][j+1]
            if v==0:
                for k in range(4):
                    dp[i][j+1][k]=max(dp[i][j][k],dp[i][j+1][k])
            else:
                dp[i][j+1][0]=max(dp[i][j][0],dp[i][j+1][0])
                dp[i][j+1][1]=max(dp[i][j][0]+v,dp[i][j][1],dp[i][j+1][1])
                dp[i][j + 1][2] = max(dp[i][j][1] + v, dp[i][j][2], dp[i][j + 1][2])
                dp[i][j + 1][3] = max(dp[i][j][2] + v, dp[i][j][3], dp[i][j + 1][3])
        if i!=r-1:
            v=item[i+1][j]
            if v==0:
                dp[i+1][j][0]=max(dp[i][j][0],dp[i][j][1],dp[i][j][2],dp[i][j][3],dp[i+1][j][0])
            else:
                dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3], dp[i + 1][j][0])
                dp[i + 1][j][1] = max(dp[i][j][0]+v, dp[i][j][1]+v, dp[i][j][2]+v, dp[i][j][3]+v, dp[i + 1][j][1])

ans=max(dp[r-1][c-1][0],dp[r-1][c-1][1],dp[r-1][c-1][2],dp[r-1][c-1][3])
print(ans)

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

def main():
    r,c,k=map(int,input().split())

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

    dp0=[[-10**10]*c  for i in range(r)]
    dp1 = [[-10 ** 10] * c for i in range(r)]
    dp2 = [[-10 ** 10] * c for i in range(r)]
    dp3 = [[-10 ** 10] * c for i in range(r)]

    if item[0][0]==0:
        dp0[0][0]=0
    else:
        dp0[0][0]=0
        dp1[0][0]=item[0][0]

    for i in range(r):
        for j in range(c):
            if j!=c-1:
                v=item[i][j+1]
                if v==0:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j + 1] = max(dp1[i][j], dp1[i][j + 1])
                    dp2[i][j + 1] = max(dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp3[i][j], dp3[i][j + 1])
                else:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j+1]=max(dp0[i][j]+v,dp1[i][j],dp1[i][j+1])
                    dp2[i][j + 1] = max(dp1[i][j] + v, dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp2[i][j] + v, dp3[i][j], dp3[i][j + 1])
            if i!=r-1:
                v=item[i+1][j]
                if v==0:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                else:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                    dp1[i + 1][j] = max(dp0[i][j]+v, dp1[i][j]+v, dp2[i][j]+v, dp3[i][j]+v, dp1[i + 1][j])

    ans=max(dp0[r-1][c-1],dp1[r-1][c-1],dp2[r-1][c-1],dp3[r-1][c-1])
    print(ans)

if __name__=='__main__':
    main()

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

r,c,k=map(int,input().split())

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

dp=[[[-10**10]*4 for i in range(c)] for j in range(r)]

if item[0][0]==0:
    dp[0][0][0]=0
else:
    dp[0][0][0]=0
    dp[0][0][1]=item[0][0]

for i in range(r):
    for j in range(c):
        if j!=c-1:
            v=item[i][j+1]
            if v==0:
                for k in range(4):
                    dp[i][j+1][k]=max(dp[i][j][k],dp[i][j+1][k])
            else:
                dp[i][j+1][0]=max(dp[i][j][0],dp[i][j+1][0])
                dp[i][j+1][1]=max(dp[i][j][0]+v,dp[i][j][1],dp[i][j+1][1])
                dp[i][j + 1][2] = max(dp[i][j][1] + v, dp[i][j][2], dp[i][j + 1][2])
                dp[i][j + 1][3] = max(dp[i][j][2] + v, dp[i][j][3], dp[i][j + 1][3])
        if i!=r-1:
            v=item[i+1][j]
            if v==0:
                dp[i+1][j][0]=max(dp[i][j][0],dp[i][j][1],dp[i][j][2],dp[i][j][3],dp[i+1][j][0])
            else:
                dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3], dp[i + 1][j][0])
                dp[i + 1][j][1] = max(dp[i][j][0]+v, dp[i][j][1]+v, dp[i][j][2]+v, dp[i][j][3]+v, dp[i + 1][j][1])

ans=max(dp[r-1][c-1][0],dp[r-1][c-1][1],dp[r-1][c-1][2],dp[r-1][c-1][3])
print(ans)

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

def main():
    r,c,k=map(int,input().split())

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

    dp0=[[-10**10]*c  for i in range(r)]
    dp1 = [[-10 ** 10] * c for i in range(r)]
    dp2 = [[-10 ** 10] * c for i in range(r)]
    dp3 = [[-10 ** 10] * c for i in range(r)]

    if item[0][0]==0:
        dp0[0][0]=0
    else:
        dp0[0][0]=0
        dp1[0][0]=item[0][0]

    for i in range(r):
        for j in range(c):
            if j!=c-1:
                v=item[i][j+1]
                if v==0:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j + 1] = max(dp1[i][j], dp1[i][j + 1])
                    dp2[i][j + 1] = max(dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp3[i][j], dp3[i][j + 1])
                else:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j+1]=max(dp0[i][j]+v,dp1[i][j],dp1[i][j+1])
                    dp2[i][j + 1] = max(dp1[i][j] + v, dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp2[i][j] + v, dp3[i][j], dp3[i][j + 1])
            if i!=r-1:
                v=item[i+1][j]
                if v==0:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                else:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                    dp1[i + 1][j] = max(dp0[i][j]+v, dp1[i][j]+v, dp2[i][j]+v, dp3[i][j]+v, dp1[i + 1][j])

    ans=max(dp0[r-1][c-1],dp1[r-1][c-1],dp2[r-1][c-1],dp3[r-1][c-1])
    print(ans)

if __name__=='__main__':
    main()

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

r,c,k=map(int,input().split())

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

dp=[[[-10**16]*4 for i in range(c)] for j in range(r)]

if item[0][0]==0:
    dp[0][0][0]=0
else:
    dp[0][0][0]=0
    dp[0][0][1]=item[0][0]

for i in range(r):
    for j in range(c):
        if j!=c-1:
            v=item[i][j+1]
            if v==0:
                for k in range(3):
                    dp[i][j+1][k]=max(dp[i][j][k],dp[i][j+1][k])
            else:
                dp[i][j+1][0]=max(dp[i][j][0],dp[i][j+1][0])
                dp[i][j+1][1]=max(dp[i][j][0]+v,dp[i][j][1],dp[i][j+1][1])
                dp[i][j + 1][2] = max(dp[i][j][1] + v, dp[i][j][2], dp[i][j + 1][2])
                dp[i][j + 1][3] = max(dp[i][j][2] + v, dp[i][j][3], dp[i][j + 1][3])
        if i!=r-1:
            v=item[i+1][j]
            if v==0:
                dp[i+1][j][0]=max(dp[i][j][0],dp[i][j][1],dp[i][j][2],dp[i][j][3],dp[i+1][j][0])
            else:
                dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3], dp[i + 1][j][0])
                dp[i + 1][j][1] = max(dp[i][j][0]+v, dp[i][j][1]+v, dp[i][j][2]+v, dp[i][j][3]+v, dp[i + 1][j][1])

ans=max(dp[r-1][c-1][0],dp[r-1][c-1][1],dp[r-1][c-1][2],dp[r-1][c-1][3])
print(ans)

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

def main():
    r,c,k=map(int,input().split())

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

    dp0=[[-10**10]*c  for i in range(r)]
    dp1 = [[-10 ** 10] * c for i in range(r)]
    dp2 = [[-10 ** 10] * c for i in range(r)]
    dp3 = [[-10 ** 10] * c for i in range(r)]

    if item[0][0]==0:
        dp0[0][0]=0
    else:
        dp0[0][0]=0
        dp1[0][0]=item[0][0]

    for i in range(r):
        for j in range(c):
            if j!=c-1:
                v=item[i][j+1]
                if v==0:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j + 1] = max(dp1[i][j], dp1[i][j + 1])
                    dp2[i][j + 1] = max(dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp3[i][j], dp3[i][j + 1])
                else:
                    dp0[i][j+1]=max(dp0[i][j],dp0[i][j+1])
                    dp1[i][j+1]=max(dp0[i][j]+v,dp1[i][j],dp1[i][j+1])
                    dp2[i][j + 1] = max(dp1[i][j] + v, dp2[i][j], dp2[i][j + 1])
                    dp3[i][j + 1] = max(dp2[i][j] + v, dp3[i][j], dp3[i][j + 1])
            if i!=r-1:
                v=item[i+1][j]
                if v==0:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                else:
                    dp0[i+1][j]=max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j],dp0[i+1][j])
                    dp1[i + 1][j] = max(dp0[i][j]+v, dp1[i][j]+v, dp2[i][j]+v, dp3[i][j]+v, dp1[i + 1][j])

    ans=max(dp0[r-1][c-1],dp1[r-1][c-1],dp2[r-1][c-1],dp3[r-1][c-1])
    print(ans)

if __name__=='__main__':
    main()