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

def main():
  h, w, k = map(int, input().split())
  U = h*w
  G = [0]*U
  for _ in range(k):
    r, c, v = map(int, input().split())
    r -= 1
    c -= 1
    G[r*w+c] = v
  dp = [[0]*4 for _ in range(U)]
  dp[0][0] = 0
  dp[0][1] = G[0]
  for i in range(h):
    for j in range(w):
      if j:
        dp[i*w+j][0] = dp[i*w+j-1][0]
        for k in range(1, 4):
          dp[i*w+j][k] = max(dp[i*w+j][k], dp[i*w+j-1][k-1]+G[i*w+j], dp[i*w+j-1][k])
      if i:
        temp = max(dp[(i-1)*w+j])
        dp[i*w+j][0] = max(dp[i*w+j][0], temp)
        dp[i*w+j][1] = max(dp[i*w+j][1], temp+G[i*w+j])
  ans = max(dp[U-1])
  print(ans)
if __name__ == "__main__":
  main()

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

h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*w for i in range(h)] for j in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[0][i][j] = dp[0][i][j-1]
      for k in range(1, 4):
        dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1]+G[i][j], dp[k][i][j-1])
    if i:
      dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j]))
      dp[1][i][j] = max(dp[1][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])+G[i][j])
ans = max(dp[0][h-1][w-1],dp[1][h-1][w-1],dp[2][h-1][w-1],dp[3][h-1][w-1])
print(ans)

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

def main():
  h, w, k = map(int, input().split())
  U = h*w
  G = [0]*U
  for _ in range(k):
    r, c, v = map(int, input().split())
    r -= 1
    c -= 1
    G[r*w+c] = v
  dp = [[0]*4 for _ in range(U)]
  dp[0][0] = 0
  dp[0][1] = G[0]
  for i in range(h):
    for j in range(w):
      if j:
        dp[i*w+j][0] = dp[i*w+j-1][0]
        for k in range(1, 4):
          dp[i*w+j][k] = max(dp[i*w+j][k], dp[i*w+j-1][k-1]+G[i*w+j], dp[i*w+j-1][k])
      if i:
        dp[i*w+j][0] = max(dp[i*w+j][0], max(dp[(i-1)*w+j]))
        dp[i*w+j][1] = max(dp[i*w+j][1], max(dp[(i-1)*w+j])+G[i*w+j])
  ans = max(dp[U-1])
  print(ans)
if __name__ == "__main__":
  main()

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

h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*w for i in range(h)] for j in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[0][i][j] = dp[0][i][j-1]
      for k in range(1, 4):
        dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1]+G[i][j], dp[k][i][j-1])
    if i:
      dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j]))
      dp[1][i][j] = max(dp[1][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])+G[i][j])
ans = max(dp[0][h-1][w-1],dp[1][h-1][w-1],dp[2][h-1][w-1],dp[3][h-1][w-1])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*4 for i in range(w)] for j in range(h)]
dp[0][0][0] = 0
dp[0][0][1] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[i][j][0] = dp[i][j-1][0]
      for k in range(1, 4):
        dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1]+G[i][j], dp[i][j-1][k])
    if i:
      dp[i][j][0] = max(dp[i][j][0], max(dp[i-1][j]))
      dp[i][j][1] = max(dp[i][j][1], max(dp[i-1][j])+G[i][j])
ans = max(dp[h-1][w-1])
print(ans)

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

h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*w for i in range(h)] for j in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[0][i][j] = dp[0][i][j-1]
      for k in range(1, 4):
        dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1]+G[i][j], dp[k][i][j-1])
    if i:
      dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j]))
      dp[1][i][j] = max(dp[1][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])+G[i][j])
ans = max(dp[0][h-1][w-1],dp[1][h-1][w-1],dp[2][h-1][w-1],dp[3][h-1][w-1])
print(ans)