Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__X = H * W
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            if i>0:
                x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3])
                dp[i, j, 1]=max(dp[i, j, 1],x+val)
                dp[i, j, 0]=max(dp[i, j, 0],x)

            if j>0:
                X = dp[i, j-1, 0]
                Y = dp[i, j-1, 1]
                V = dp[i, j-1, 2]
                Z = dp[i, j-1, 3]
                dp[i, j, 0]=max(dp[i, j, 0],X)
                dp[i, j, 1]=max(dp[i, j, 1],X+val,Y)
                dp[i, j, 2]=max(dp[i, j, 2],Y+val,V)
                dp[i, j, 3]=max(dp[i, j, 3],V+val,Z)

    #print(dp)
    ans=0
    for i in range(4): ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            if i>0:
                x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3]) + val
                dp[i, j, 1]=max(dp[i, j, 1], x)
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 0],dp[i-1, j, 1],dp[i-1, j, 2],dp[i-1, j, 3])
            if j>0:
                dp[i, j, 0]=max(dp[i, j, 0],dp[i, j-1, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 0]+val,dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+val,dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+val,dp[i, j-1, 3])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            if i>0:
                x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3]) + val
                dp[i, j, 1]=max(dp[i, j, 1], x)
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 0],dp[i-1, j, 1],dp[i-1, j, 2],dp[i-1, j, 3])
            if j>0:
                dp[i, j, 0]=max(dp[i, j, 0],dp[i, j-1, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 0]+val,dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+val,dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+val,dp[i, j-1, 3])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 0]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 1]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 1])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 2])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 3]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 3])
            if j>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 0]+v[i, j])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+v[i, j])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i, j-1, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 3])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

def main():
    r,c,k=map(int,input().split())
    v=[0] * (c * r)
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[(ri-1)*c + (ci-1)]=a

    dp= [0] * (c * r * 4)

    #print(dp)
    x = c * r
    if v[0]>0: dp[x] = v[0]

    for i in range(r):
        for j in range(c):
            idx = i*c+j
            idx2 = (i-1)*c + j
            idx3 = i*c + j-1

            if i>0:
                a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2])+v[idx]
                dp[x+idx]=max(dp[x+idx], a)
                dp[idx]=max(dp[idx],dp[idx2],dp[x+idx2],dp[2*x+idx2],dp[3*x+idx2])

            if j>0:
                dp[idx]=max(dp[idx],dp[idx3])
                dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx],dp[x + idx3])
                dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx],dp[2*x + idx3])
                dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx],dp[3*x + idx3])

    #print(dp)
    ans=0
    for i in range(4): ans = max(dp[i*x + (r-1)*c + (c-1)],ans)
    return print(ans)

if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 0]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 1]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 1])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 2])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 3]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 3])
            if j>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 0]+v[i, j])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+v[i, j])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i, j-1, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 3])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        if a[0] < 0 or a[1] < 0 or a[0] >= self.__H or a[1] >= self.__W:
            print("Out of Index")
            exit(1)
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        if a[0] < 0 or a[1] < 0 or a[0] >= self.__H or a[1] >= self.__W:
            print("Out of Index")
            exit(1)
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        if a[0] < 0 or a[1] < 0 or a[0] >= self.__H or a[1] >= self.__W or a[2] < 0 or a[2] >= self.__D:
            print("Out of Index")
            exit(1)
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        if a[0] < 0 or a[1] < 0 or a[0] >= self.__H or a[1] >= self.__W or a[2] < 0 or a[2] >= self.__D:
            print("Out of Index")
            exit(1)
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

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

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 0]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 1]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 1])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 2])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i-1, j, 3]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i-1, j, 3])
            if j>0:
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 0]+v[i, j])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+v[i, j])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+v[i, j])
                dp[i, j, 0]=max(dp[i, j, 0],dp[i, j-1, 0])
                dp[i, j, 1]=max(dp[i, j, 1],dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 3])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


Fixed submission:
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()