Buggy python submission (verdict: Time Limit Exceeded):
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
dp=[[[0] * R for i in range(C)] for j in range(4)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp[1][0][0]=l[0][0]
for x in range(1,R+C-1):
  for y in range(R):
    i=y
    j=x-y
    if j<0 or C<=j:
      continue
    for k in range(4):
      A=0
      if 0<j:
        A=max(A,dp[k][j-1][i])
        if 0<k:
          A=max(A,dp[k-1][j-1][i]+l[i][j])
      if 0<i and k<2:
        for q in range(4):
          A=max(A,dp[q][j][i-1]+k*l[i][j])
      dp[k][j][i]=A
X=dp[3][C-1][R-1]
for k in range(3):
  X=max(X,dp[k][C-1][R-1])
print(X)

Fixed submission:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  kdp[0]=dp[0]
  kdp[1]=l[i][0]+dp[0]
  ndp[0]=kdp[1]
  for j in range(1,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  for j in range(0,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])


Fixed submission:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  kdp[0]=dp[0]
  kdp[1]=l[i][0]+dp[0]
  ndp[0]=kdp[1]
  for j in range(1,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
dp=[[[0] * 4 for i in range(C)] for j in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp[0][0][1]=l[0][0]
for x in range(1,R+C-1):
  for y in range(R):
    i=y
    j=x-y
    if j<0 or C<=j:
      continue
    for k in range(4):
      A=0
      if 0<j:
        A=max(A,dp[i][j-1][k])
        if 0<k:
          A=max(A,dp[i][j-1][k-1]+l[i][j])
      if 0<i and k<2:
        for q in range(4):
          A=max(A,dp[i-1][j][q]+k*l[i][j])
      dp[i][j][k]=A
X=dp[R-1][C-1][3]
for k in range(3):
  X=max(X,dp[R-1][C-1][k])
print(X)
    

Fixed submission:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  kdp[0]=dp[0]
  kdp[1]=l[i][0]+dp[0]
  ndp[0]=kdp[1]
  for j in range(1,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])

Buggy python submission (verdict: Time Limit Exceeded):
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
dp=[[[0] * 4 for i in range(C)] for j in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp[0][0][1]=l[0][0]
for x in range(1,R+C-1):
  for y in range(R):
    i=y
    j=x-y
    if j<0 or C<=j:
      continue
    for k in range(4):
      A=0
      if 0<j:
        A=max(A,dp[i][j-1][k])
        if 0<k:
          A=max(A,dp[i][j-1][k-1]+l[i][j])
      if 0<i and k<2:
        for q in range(4):
          A=max(A,dp[i-1][j][q]+k*l[i][j])
      dp[i][j][k]=A
X=dp[R-1][C-1][3]
for k in range(3):
  X=max(X,dp[R-1][C-1][k])
print(X)
    

Fixed submission:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  kdp[0]=dp[0]
  kdp[1]=l[i][0]+dp[0]
  ndp[0]=kdp[1]
  for j in range(1,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])