Buggy python submission (verdict: Runtime Error):
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

Fixed submission:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*4 for j in W] for i in H]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[0][0][1]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[y][x][0]
      dp[y][x][0]=0
      for i in range(4):
        if 0<x:
          if dp[y][x][i]<dp[y][x-1][i]:
            dp[y][x][i]=dp[y][x-1][i]
          if 0<i and v!=0 and dp[y][x][i]<dp[y][x-1][i-1]+v:
            dp[y][x][i]=dp[y][x-1][i-1]+v
        if 0<y:
          if dp[y][x][0]<dp[y-1][x][i]:
            dp[y][x][0]=dp[y-1][x][i]
          if v!=0 and dp[y][x][1]<dp[y-1][x][i]+v:
            dp[y][x][1]=dp[y-1][x][i]+v
  print(max(dp[h-1][w-1]))
main()

Fixed submission:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  dp=[[[0]*4 for j in range(w)] for i in range(h)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[0][0][1]=dp[0][0][0]
    dp[0][0][0]=0
  for y in range(h):
    for x in range(w):
      v=dp[y][x][0]
      dp[y][x][0]=0
      for i in range(4):
        if 0<x:
          if dp[y][x][i]<dp[y][x-1][i]:
            dp[y][x][i]=dp[y][x-1][i]
          if 0<i and v!=0 and dp[y][x][i]<dp[y][x-1][i-1]+v:
            dp[y][x][i]=dp[y][x-1][i-1]+v
        if 0<y:
          if dp[y][x][0]<dp[y-1][x][i]:
            dp[y][x][0]=dp[y-1][x][i]
          if v!=0 and dp[y][x][1]<dp[y-1][x][i]+v:
            dp[y][x][1]=dp[y-1][x][i]+v
  print(max(dp[h-1][w-1]))
main()

Fixed submission:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  p=[[0]*w for _ in range(h)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    p[r-1][c-1]=v
  dp=[[[0]*4 for j in range(w)] for i in range(h)]
  if p[0][0]!=0:
    dp[0][0][1]=p[0][0]
  for y in range(h):
    for x in range(w):
      for i in range(4):
        if x<w-1:
          if dp[y][x+1][i]<dp[y][x][i]:
            dp[y][x+1][i]=dp[y][x][i]
          if i<3 and p[y][x+1]!=0 and dp[y][x+1][i+1]<dp[y][x][i]+p[y][x+1]:
            dp[y][x+1][i+1]=dp[y][x][i]+p[y][x+1]
        if y<h-1:
          if dp[y+1][x][0]<dp[y][x][i]:
            dp[y+1][x][0]=dp[y][x][i]
          if p[y+1][x]!=0 and dp[y+1][x][1]<dp[y][x][i]+p[y+1][x]:
            dp[y+1][x][1]=dp[y][x][i]+p[y+1][x]
  print(max(dp[h-1][w-1]))
main()

Fixed submission:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

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

Fixed submission:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()