Problem p02586 (python)

Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

r, c, k = map(int, input().split())
itm = [[0]*(c) for _ in range(r)]
for i in range(k):
    ri, ci, vi = map(int, input().split())
    itm[ri-1][ci-1] = vi

dp0 = [[0]*4 for c_ in range(3005)]
dp1 = [[0]*4 for c_ in range(3005)]

for i in range(r):
    for j in range(c):
        nowv = itm[i][j]
        dp0[j][3] = max(dp0[j][3], dp0[j][2] + nowv)
        dp0[j][2] = max(dp0[j][2], dp0[j][1] + nowv)
        dp0[j][1] = max(dp0[j][1], dp0[j][0] + nowv)

        dp0[j+1][0] = max(dp0[j+1][0], dp0[j][0])
        dp0[j+1][1] = max(dp0[j+1][1], dp0[j][1])
        dp0[j+1][2] = max(dp0[j+1][2], dp0[j][2])
        dp0[j+1][3] = max(dp0[j+1][3], dp0[j][3])

        dp1[j][0] = max(dp1[j][0], max(dp0[j]))
    dp0 = dp1.copy()

print(max(dp1[c-1]))

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

r, c, k = map(int, input().split())
itm = [[0]*(c) for _ in range(r)]
for i in range(k):
    ri, ci, vi = map(int, input().split())
    itm[ri-1][ci-1] = vi

dp = np.zeros((3005, 3005, 4), dtype=np.int64)

for i in range(r):
    for j in range(c):
        nowv = itm[i][j]
        dp[i][j][3] = max(dp[i][j][3], dp[i][j][2] + nowv)
        dp[i][j][2] = max(dp[i][j][2], dp[i][j][1] + nowv)
        dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + nowv)

        dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
        dp[i][j+1][0] = max(dp[i][j+1][0], dp[i][j][0])
        dp[i][j+1][1] = max(dp[i][j+1][1], dp[i][j][1])
        dp[i][j+1][2] = max(dp[i][j+1][2], dp[i][j][2])
        dp[i][j+1][3] = max(dp[i][j+1][2], dp[i][j][3])

print(max(dp[r-1][c-1]))

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

import numpy as np

r, c, k = map(int, input().split())
itm = [[0]*(c) for _ in range(r)]
for i in range(k):
    ri, ci, vi = map(int, input().split())
    itm[ri-1][ci-1] = vi

dp = np.zeros((3005, 3005, 4), dtype=np.int64)

for i in range(r):
    for j in range(c):
        nowv = itm[i][j]
        dp[i][j][3] = max(dp[i][j][3], dp[i][j][2] + nowv)
        dp[i][j][2] = max(dp[i][j][2], dp[i][j][1] + nowv)
        dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + nowv)

        dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
        dp[i][j+1][0] = max(dp[i][j+1][0], dp[i][j][0])
        dp[i][j+1][1] = max(dp[i][j+1][1], dp[i][j][1])
        dp[i][j+1][2] = max(dp[i][j+1][2], dp[i][j][2])
        dp[i][j+1][3] = max(dp[i][j+1][2], dp[i][j][3])

print(max(dp[r-1][c-1]))

Buggy submission (verdict: Runtime Error):
import heapq
r, c, k = map(int, input().split())
itm = [[0]*c for _ in range(r)]
for i in range(k):
  ri, ci, vi = map(int, input().split())
  itm[ri-1][ci-1] = vi
dp = [0]*c
for i in range(r):
  que = []
  heapq.heapify(que)
  nowv = itm[i][0]
  if nowv > 0:
    heapq.heappush(que, nowv)
    dp[0] += nowv
  for j in range(1, c):
    nowv = itm[i][j]
    if nowv > 0:
      heapq.heappush(que, nowv)
      if len(que)>3:
        prev = heapq.heappop(que)
      else:
        prev = 0
    sumv1 = dp[j] + nowv#上から
    sumv2 = dp[j-1] + nowv - prev#左から
    if sumv1 >= sumv2:
      dp[j] = sumv1
      que = []
      heapq.heapify(que)
      if nowv > 0:
        heapq.heappush(que, nowv)
    else:
      dp[j] = sumv2
print(dp[-1])