Problem p02586 (python)

Fixed submission:
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

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

for i in range(R):
    DPi = [None] * C
    for j in range(C):
        DPi[j] = [0,0,0,0]
        DPij = DPi[j]
        DPijs1 = DPi[j-1]
        if i != 0 and j != 0:
            DPij[0] = max(DPLast[j][0],DPLast[j][1],DPLast[j][2],DPLast[j][3],DPijs1[0])
            DPij[1] = DPijs1[1]
            DPij[2] = DPijs1[2]
            DPij[3] = DPijs1[3]
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        elif i == 0 and j != 0:
            DPij[0] = DPijs1[0]
            DPij[1] = DPijs1[1]
            DPij[2] = DPijs1[2]
            DPij[3] = DPijs1[3]
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        elif j == 0 and i != 0:
            DPij[0] = max(DPLast[j][0],DPLast[j][1],DPLast[j][2],DPLast[j][3])
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        else:
            DPij[1] = Gift[i][j]
    DPLast = DPi

print(max(DPi[C-1]))

Buggy submission (verdict: Runtime Error):
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

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

for i in range(R):
    DPi = [None] * C
    for j in range(C):
        DPi[j] = [0,0,0,0]
        DPij = DPi[j]
        DPijs1 = DPi[j-1]
        if i != 0 and j != 0:
            DPij[0] = max(DPLast[j][0],DPLast[j][1],DPLast[j][2],DPLast[j][3],DPijs1[0])
            DPij[1] = DPijs1[1]
            DPij[2] = DPijs1[2]
            DPij[3] = DPijs1[3]
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        elif i == 0:
            DPij[0] = DPijs1[0]
            DPij[1] = DPijs1[1]
            DPij[2] = DPijs1[2]
            DPij[3] = DPijs1[3]
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        elif j == 0:
            DPij[0] = max(DPLast[j][0],DPLast[j][1],DPLast[j][2],DPLast[j][3])
            if Gift[i][j]:
                DPij[3] = max(DPij[3],DPij[2] + Gift[i][j])
                DPij[2] = max(DPij[2],DPij[1] + Gift[i][j])
                DPij[1] = max(DPij[1],DPij[0] + Gift[i][j])
        else:
            DPij[1] = Gift[i][j]
    DPLast = DPi

print(max(DPi[C-1]))