Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP = [[[0,0,0] for _ in range(C+1)] for _ in range(R+1)]

for r in range(1,R+1):
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP[r-1][c][2]+item,DP[r][c-1][0])
    DP2 = max(DP1,DP[r][c-1][1],DP[r][c-1][0]+item)
    DP3 = max(DP2,DP[r][c-1][2],DP[r][c-1][1]+item)
    DP[r][c][0] = DP1
    DP[r][c][1] = DP2
    DP[r][c][2] = DP3
        
print(DP[R][C][2])

Fixed submission:
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP_prev = [0 for _ in range(C+1)]

for r in range(1,R+1):
  DP = [[0 for _ in range(C+1)],[0 for _ in range(C+1)],[0 for _ in range(C+1)]]
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP_prev[c]+item,DP[0][c-1])
    DP2 = max(DP1,DP[1][c-1],DP[0][c-1]+item)
    DP3 = max(DP2,DP[2][c-1],DP[1][c-1]+item)
    DP[0][c] = DP1
    DP[1][c] = DP2
    DP[2][c] = DP3
  DP_prev = DP[2]
        
print(DP_prev[C])

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP = [[[0,0,0] for _ in range(C+1)] for _ in range(R+1)]

for r in range(1,R+1):
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP[r-1][c][2]+item,DP[r][c-1][0])
    DP2 = max(DP1,DP[r][c-1][1],DP[r][c-1][0]+item)
    DP3 = max(DP2,DP[r][c-1][2],DP[r][c-1][1]+item)
    DP[r][c][0] = DP1
    DP[r][c][1] = DP2
    DP[r][c][2] = DP3
        
print(DP[R][C][2])

Fixed submission:
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP_prev = [0 for _ in range(C+1)]

for r in range(1,R+1):
  DP = [[0 for _ in range(C+1)],[0 for _ in range(C+1)],[0 for _ in range(C+1)]]
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP_prev[c]+item,DP[0][c-1])
    DP2 = max(DP1,DP[1][c-1],DP[0][c-1]+item)
    DP3 = max(DP2,DP[2][c-1],DP[1][c-1]+item)
    DP[0][c] = DP1
    DP[1][c] = DP2
    DP[2][c] = DP3
  DP_prev = DP[2]
        
print(DP_prev[C])

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP = [[[0,0,0,0] for _ in range(C+1)] for _ in range(R+1)]

for r in range(1,R+1):
  for c in range(1,C+1):
    DP0 = DP[r-1][c][3]
    DP1 = max(DP0+items[r][c],DP[r][c-1][1])
    DP2 = max(DP1,DP[r][c-1][2],DP[r][c-1][1]+items[r][c])
    DP3 = max(DP2,DP[r][c-1][3],DP[r][c-1][2]+items[r][c])
    DP[r][c][0] = DP0
    DP[r][c][1] = DP1
    DP[r][c][2] = DP2
    DP[r][c][3] = DP3
        
print(DP[R][C][3])

Fixed submission:
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP_prev = [0 for _ in range(C+1)]

for r in range(1,R+1):
  DP = [[0 for _ in range(C+1)],[0 for _ in range(C+1)],[0 for _ in range(C+1)]]
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP_prev[c]+item,DP[0][c-1])
    DP2 = max(DP1,DP[1][c-1],DP[0][c-1]+item)
    DP3 = max(DP2,DP[2][c-1],DP[1][c-1]+item)
    DP[0][c] = DP1
    DP[1][c] = DP2
    DP[2][c] = DP3
  DP_prev = DP[2]
        
print(DP_prev[C])

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP = [[[0,0,0,0] for _ in range(C+1)] for _ in range(R+1)]
for r in range(1,R+1):
  # 0
  for c in range(1,C+1):
    DP[r][c][0] = DP[r-1][c][3]
  # 1
  for c in range(1,C+1):
    DP[r][c][1] = max(DP[r][c][0]+items[r][c],DP[r][c-1][1])
  # 2
  for c in range(1,C+1):
    DP[r][c][2] = max(DP[r][c][1],DP[r][c-1][2],DP[r][c-1][1]+items[r][c])
  # 3
  for c in range(1,C+1):
    DP[r][c][3] = max(DP[r][c][2],DP[r][c-1][3],DP[r][c-1][2]+items[r][c])
        
print(DP[R][C][3])

Fixed submission:
# -*- coding: utf-8 -*-

R,C,K = map(int, input().split())

items = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
  r,c,v = map(int, input().split())
  items[r][c] = v

DP_prev = [0 for _ in range(C+1)]

for r in range(1,R+1):
  DP = [[0 for _ in range(C+1)],[0 for _ in range(C+1)],[0 for _ in range(C+1)]]
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP_prev[c]+item,DP[0][c-1])
    DP2 = max(DP1,DP[1][c-1],DP[0][c-1]+item)
    DP3 = max(DP2,DP[2][c-1],DP[1][c-1]+item)
    DP[0][c] = DP1
    DP[1][c] = DP2
    DP[2][c] = DP3
  DP_prev = DP[2]
        
print(DP_prev[C])