Problem p02586 (python)

Fixed submission:
import sys
import copy
R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)]  # dp配列と合わせるために, 0行目, 0列目を追加している.
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    r -= 1
    c -= 1
    item[r][c] = v
    
dp0 = [[0] * (C+1) for r in range(R+1)]
dp1 = [[0] * (C+1) for r in range(R+1)]
dp2 = [[0] * (C+1) for r in range(R+1)]
dp3 = [[0] * (C+1) for r in range(R+1)]
 
for r in range(R):
   for c in range(C):
      dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
      dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
      dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
      dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
      dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
      dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
      dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c])
ans = max(dp0[R-1][C-1], dp1[R-1][C-1])
ans = max(ans, dp3[R-1][C-1])
ans = max(ans, dp2[R-1][C-1])
print(ans)

Buggy submission (verdict: Runtime Error):
import copy
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(4):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = copy.deepcopy(dp[0])
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]
dp[1][0][0] = location[0][0]
if R > 1:
   for r in range(R-1):
      dp[1][r+1][0] = location[r][0] + dp[1][r][0]
      dp[0][r+1][0] = max(dp[1][r][0],dp[0][r][0])
if C > 1:
   for c in range(C-1):
         dp[1][0][c+1] = max(location[0][c+1] + dp[0][0][c], dp[1][0][c])
         dp[2][0][c+1] = max(location[0][c+1] + dp[1][0][c], dp[2][0][c])
         dp[3][0][c+1] = max(location[0][c+1] + dp[2][0][c], dp[3][0][c])
   if R > 1:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1], dp[2][r][c+1], dp[3][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c+1] + location[r+1][c+1], dp[1][r+1][c])
            dp[2][r+1][c+1] = max(dp[1][r+1][c] + location[r+1][c+1], dp[2][r+1][c])
            dp[3][r+1][c+1] = max(dp[2][r+1][c] + location[r+1][c+1], dp[3][r+1][c])
if R==1 and C==1:
   ans = i[2]
else:
   ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1], dp[3][R-1][C-1])
print(ans)

Buggy submission (verdict: Runtime Error):
import copy
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(4):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = copy.deepcopy(dp[0])
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]
dp[1][0][0] = location[0][0]
if R > 1:
   for r in range(R-1):
      dp[1][r+1][0] = location[r][0] + max(dp[1][r][0],dp[0][r][0])
      dp[0][r+1][0] = max(dp[1][r][0],dp[0][r][0])
if C > 1:
   for c in range(C-1):
         dp[1][0][c+1] = max(location[0][c+1] + dp[0][0][c], dp[1][0][c])
         dp[2][0][c+1] = max(location[0][c+1] + dp[1][0][c], dp[2][0][c])
         dp[3][0][c+1] = max(location[0][c+1] + dp[2][0][c], dp[3][0][c])
   if R > 1:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1], dp[2][r][c+1], dp[3][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c+1] + location[r+1][c+1], dp[1][r+1][c])
            dp[2][r+1][c+1] = max(dp[1][r+1][c] + location[r+1][c+1], dp[2][r+1][c])
            dp[3][r+1][c+1] = max(dp[2][r+1][c] + location[r+1][c+1], dp[3][r+1][c])
if R==1 and C==1:
   ans = i[2]
else:
   ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1], dp[3][R-1][C-1])
print(ans)