qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, K = map(int, input().split())
p_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))


cycle = {}
max_score = 0
for i in range(N):
  score = 0
  step = 0
  path = {}
  cumulative_score = []
  current_p = i
  cycle_passed = False
  while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and current_p in path.keys():
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True
       
        
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    max_score = max(max_score, score)
    
    current_p = next_p
    step += 1

print(max_score)

Fixed code:
N, K = map(int, input().split())
p_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

max_score = 0
first = True
for i in range(N):
  score = 0
  step = 0
  path = {}
  cumulative_score = []
  current_p = i
  cycle_passed = False
  while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
    current_p = next_p
    step += 1

print(max_score)
Buggy code:
from itertools import accumulate

n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

check = [False] * n
loops = []

for i in range(n):
    if check[i]:
        continue
    check[i] = True
    next = P[i]
    tmp = [i]
    while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
    loops.append(tmp)

ans = -1e9

for loop in loops:

    num = len(loop)
    div = k // num
    r = k % num
    if r == 0:
        r = num
        div -= 1
    L = [C[p] for p in loop]
    ans_tmp = 0
    sumL = sum(L)
    by_loop = max(sumL, 0) * div
    if k > num and sumL <= 0:
        r = num
    L += L[:r]
    ACC = [0] + list(accumulate(L))

    for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

    ans = max(ans, ans_tmp)
print(ans)

Fixed code:
from itertools import accumulate

n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

check = [False] * n
loops = []

for i in range(n):
    if check[i]:
        continue
    check[i] = True
    next = P[i]
    tmp = [i]
    while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
    loops.append(tmp)

ans = int(-1e9)

for loop in loops:

    num = len(loop)
    div = k // num
    r = k % num
    if r == 0:
        r = num
        div -= 1
    L = [C[p] for p in loop]
    ans_tmp = int(-1e9)
    sumL = sum(L)
    by_loop = max(sumL, 0) * div
    if k > num and sumL <= 0:
        r = num
    L += L[:r]
    ACC = [0] + list(accumulate(L))

    for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

    ans = max(ans, ans_tmp)
print(ans)
Buggy code:
n, k = map(int, input().split())
*p, = map(int, input().split())
p = [v - 1 for v in p]
*c, = map(int, input().split())
minc = float('-inf')

ans = minc


def get_loop(i):
    pos = i
    cnt = 0
    cur_score = 0
    while cnt == 0 or pos != i:
        pos = p[pos]
        cur_score = cur_score + c[pos]
        cnt += 1
    return cnt, cur_score

# print(get_loop(2))

def solve(pos, k):
    score = minc
    cur_score = 0
    loop_length, loop_score = get_loop(pos)
    if k >= loop_length and loop_score > 0:
        # print("%s: %s // %s, %s, %s" % (i, k, loop_length, cur_score, max_loop_score))
        cur_score = loop_score * ((k - loop_length) // loop_length)
        score = max(cur_score, score)
        k = k % loop_length + 1
    else:
        k = min(k, loop_length)
    # print('rem k', k, "cur_score", cur_score)
    for _ in range(k):
        pos = p[pos]
        cur_score += c[pos]
        score = max(cur_score, score)
    # print("%s: %s" % (i, score))
    return score

for i in range(n):
    ans = max(ans, solve(i, k))
print(ans)



Fixed code:
n, k = map(int, input().split())
*p, = map(int, input().split())
p = [v - 1 for v in p]
*c, = map(int, input().split())
minc = float('-inf')

ans = minc


def get_loop(i):
    pos = i
    cnt = 0
    cur_score = 0
    while cnt == 0 or pos != i:
        pos = p[pos]
        cur_score = cur_score + c[pos]
        cnt += 1
    return cnt, cur_score

# print(get_loop(2))

def solve(pos, k):
    score = minc
    cur_score = 0
    loop_length, loop_score = get_loop(pos)
    if k >= 2 * loop_length and loop_score > 0:
        # print("%s: %s // %s, %s, %s" % (i, k, loop_length, cur_score, max_loop_score))
        cur_score = loop_score * ((k - loop_length) // loop_length)
        score = max(cur_score, score)
        k = k % loop_length + loop_length
    else:
        k = min(k, 2 * loop_length)
    for _ in range(k):
        pos = p[pos]
        cur_score += c[pos]
        score = max(cur_score, score)
    # print("%s: %s" % (i, score))
    return score

for i in range(n):
    ans = max(ans, solve(i, k))
print(ans)
Buggy code:
h,w,k = map(int, input().split())
item = [[0 for _ in range(w)] for _ in range(h)]
for i in range(k):
    y,x,v = list(map(int,input().split()))
    item[y-1][x-1]=v

dp = [[[0 for _ in range(3)] for _ in range(w)] for _ in range(h)]

for y in range(h):
    for x in range(w):
        for i in range(3):
            if y<h-1:
                dp[y+1][x][0]=max(dp[y+1][x][0],dp[y][x][i])
            if x<w-1:
                dp[y][x+1][i]=max(dp[y][x+1][i],dp[y][x][i])

        if item[y][x]>0:
            for i in range(2):
                if y<h-1:
                    dp[y+1][x][0]=max(dp[y+1][x][0],dp[y][x][i] + item[y][x])
                if x<w-1:
                    dp[y][x+1][i+1]=max(dp[y][x+1][i+1],dp[y][x][i] + item[y][x])


if item[-1][-1] != 0:
    for i in range(2):
        dp[-1][-1][i] += item[-1][-1]

ans=max(dp[-1][-1])

print(ans)

Fixed code:
h,w,k = map(int, input().split())
item = [[0 for _ in range(w)] for _ in range(h)]
for i in range(k):
    y,x,v = list(map(int,input().split()))
    item[y-1][x-1]=v

dp = [[[0 for _ in range(w)] for _ in range(h)] for _ in range(4)]

for y in range(h):
    for x in range(w):
        for i in range(4):
            if y<h-1:
                dp[0][y+1][x]=max(dp[0][y+1][x],dp[i][y][x])
            if x<w-1:
                dp[i][y][x+1]=max(dp[i][y][x+1],dp[i][y][x])

        if item[y][x]>0:
            for i in range(3):
                if y<h-1:
                    dp[0][y+1][x]=max(dp[0][y+1][x],dp[i][y][x] + item[y][x])
                if x<w-1:
                    dp[i+1][y][x+1]=max(dp[i+1][y][x+1],dp[i][y][x] + item[y][x])

ans=[dp[0][-1][-1] + item[-1][-1],
     dp[1][-1][-1] + item[-1][-1],
     dp[2][-1][-1] + item[-1][-1],
     dp[3][-1][-1]]
print(max(ans))
Buggy code:
R,C,K = map(int, input().split())

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

for i in range(R):
    for j in range(C):
        
        for k in range(2, -1, -1):
            dp[k+1][i][j] = max(dp[k+1][i][j], dp[k][i][j] + maze[i][j])

        for k in range(4):
            dp[0][i+1][j] = max(dp[0][i+1][j], dp[k][i][j])
            dp[k][i][j+1] = max(dp[k][i][j+1], dp[k][i][j])  

ans = 0

print(dp)
for k in range(4):
    ans = max(ans, dp[k][R-1][C-1])       

print(ans)

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

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

for i in range(R):
    for j in range(C):
        
        for k in range(2, -1, -1):
            dp[k+1][i][j] = max(dp[k+1][i][j], dp[k][i][j] + maze[i][j])

        for k in range(4):
            dp[0][i+1][j] = max(dp[0][i+1][j], dp[k][i][j])
            dp[k][i][j+1] = max(dp[k][i][j+1], dp[k][i][j])  

ans = 0

for k in range(4):
    ans = max(ans, dp[k][R-1][C-1])       

print(ans)
Buggy code:
a = [[0] * 3005 for i in range(3005)]
dp = [[[0] * 4 for i in range(3005)] for j in range(3005)]
R,C,K = map(int, input().split())

for i in range(K):
  r,c,v = map(int, input().split())
  a[r-1][c-1] = v

for i in range(R):
  for j in range(C):
    for k in range(2, -1, -1):
      dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
    for k in range(4):
      dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
      dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
    
ans = 0
for k in range(4):
  ans = max(ans, dp[R-1][C-1][k])
print(ans)


Fixed code:
R,C,K = map(int, input().split())
a = [[0] * (C+1) for i in range(R+1)]
dp = [[[0] * (C+1) for i in range(R+1)] for j in range(4)]
for i in range(K):
  r,c,v = map(int, input().split())
  a[r-1][c-1] = v

for i in range(R):
  for j in range(C):
    for k in range(2, -1, -1):
      dp[k+1][i][j] = max(dp[k+1][i][j], dp[k][i][j]+a[i][j])
    for k in range(4):
      dp[0][i+1][j] = max(dp[0][i+1][j], dp[k][i][j])
      dp[k][i][j+1] = max(dp[k][i][j+1], dp[k][i][j])

    
ans = 0
for k in range(4):
  ans = max(ans, dp[k][R-1][C-1])
print(ans)
Buggy code:
R, C, K = map(int, input().split())
a = [[0]*C for _ in range(R)]
dp = [[[0]*4 for _ in range(C)] for _ in range(R)]
for i in range(K):
    r, c, v = map(int, input().split())
    a[r-1][c-1] = v

for i in range(R):
    for j in range(C):
        for k in range(2, -1, -1):
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
        for k in range(4):
            dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])

ans = 0
for i in range(4):
    ans = max(ans, dp[R-1][C-1][i])

print(ans)



Fixed code:
R, C, k = map(int, input().split())
xs = [[0] * C for _ in range(R)]
for _ in range(k):
    r, c, v = map(int, input().split())
    xs[r - 1][c - 1] = v

dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
for i in range(R):
    for j in range(C):
        for k in range(2, -1, -1):
            dp[k + 1][i][j] = max(dp[k + 1][i][j], dp[k][i][j] + xs[i][j])
        for k in range(4):
            dp[k][i][j + 1] = max(dp[k][i][j + 1], dp[k][i][j])
            dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[k][i][j])
ans = 0
for k in range(4):
    ans = max(ans, dp[k][R - 1][C - 1])
print(ans)
Buggy code:
import sys

readline = sys.stdin.readline

def solve():
    R, C, K = map(int, readline().split())
    items = [[0] * C for i in range(R)]
    for i in range(K):
        r,c,k = map(int, readline().split())
        items[r - 1][c - 1] = k
    
    dp = [[[0] * C for i in range(R)] for j in range(4)]

    for i in range(R):
        for j in range(C):
            for k in range(4):
                if i == 0 and j == 0:
                    dp[k][i][j] = 0 if k == 0 else items[i][j]
                elif i == 0:
                    dp[k][i][j] = dp[k][i][j - 1] if k == 0 else dp[k - 1][i][j - 1] + items[i][j]
                elif j == 0:
                    dp[k][i][j] = dp[3][i - 1][j] if k == 0 else dp[3][i - 1][j] + items[i][j]
                else:
                    if k == 0:
                        dp[k][i][j] = max(dp[3][i - 1][j], dp[k][i][j - 1])
                    else:
                        dp[k][i][j] = max(
                            dp[k - 1][i][j - 1] + items[i][j],
                            dp[3][i - 1][j])

    print(dp[-1][-1][-1])

solve()

Fixed code:
import sys

readline = sys.stdin.readline

def solve():
    R, C, K = map(int, readline().split())
    items = [[0] * C for i in range(R)]
    for i in range(K):
        r,c,k = map(int, readline().split())
        items[r - 1][c - 1] = k
    
    dp = [[[0] * C for i in range(R)] for j in range(4)]

    for i in range(R):
        for j in range(C):
            for k in range(4):
                if i == 0 and j == 0:
                    if k == 0:
                        dp[k][i][j] = 0
                    else:
                        dp[k][i][j] = items[i][j]
                elif i == 0:
                    if k == 0:
                        dp[k][i][j] = dp[k][i][j - 1]
                    else:
                        dp[k][i][j] = max(dp[k - 1][i][j - 1] + items[i][j], dp[k][i][j - 1])
                elif j == 0:
                    if k == 0:
                        dp[k][i][j] = dp[3][i - 1][j]
                    else:
                        dp[k][i][j] = dp[3][i - 1][j] + items[i][j]
                else:
                    if k == 0:
                        dp[k][i][j] = max(dp[3][i - 1][j], dp[k][i][j - 1])
                    else:
                        dp[k][i][j] = max(
                            dp[3][i - 1][j] + items[i][j],
                            dp[k - 1][i][j - 1] + items[i][j],
                            dp[k][i][j - 1]
                        )

    print(dp[-1][-1][-1])

solve()
Buggy code:
r,c,k=map(int,input().split())
lst=[[0]*(c+1) for i in range(r+1)]
for i in range(k):
  s,t,u=map(int,input().split())
  lst[s][t]=u


dp1=[[0]*(c+1) for i in range(r+1)]
dp2=[[0]*(c+1) for i in range(r+1)]
dp3=[[0]*(c+1) for i in range(r+1)]

for i in range(1,r+1):
  for j in range(1,c+1):
    dp1[i][j]=max(dp1[i][j-1],dp1[i-1][j]+lst[i][j],dp2[i-1][j]+lst[i][j],dp3[i-1][j]+lst[i][j])
    dp2[i][j]=max(dp2[i][j-1],dp1[i][j-1]+lst[i][j])
    dp3[i][j]=max(dp3[i][j-1],dp2[i][j-1]+lst[i][j])

print(max(dp1[-1][-1],dp2[-1][-1],dp3[-1][-1]),0)


Fixed code:
r,c,k=map(int,input().split())
lst=[[0]*(c+1) for i in range(r+1)]
for i in range(k):
  s,t,u=map(int,input().split())
  lst[s][t]=u


dp1=[[0]*(c+1) for i in range(r+1)]
dp2=[[0]*(c+1) for i in range(r+1)]
dp3=[[0]*(c+1) for i in range(r+1)]

for i in range(1,r+1):
  for j in range(1,c+1):
    dp1[i][j]=max(dp1[i][j-1],dp1[i-1][j]+lst[i][j],dp2[i-1][j]+lst[i][j],dp3[i-1][j]+lst[i][j])
    dp2[i][j]=max(dp2[i][j-1],dp1[i][j-1]+lst[i][j])
    dp3[i][j]=max(dp3[i][j-1],dp2[i][j-1]+lst[i][j])

print(max(dp1[-1][-1],dp2[-1][-1],dp3[-1][-1],0))
Buggy code:
import sys
input=sys.stdin.readline

def main():
    r,c,k=map(int,input().split())
    dp=[[[0 for jj in range(c+1)] for ii in range(r+1)] for kk in range(4)]

    g=[[0 for j in range(c)] for i in range(r)]
    for i in range(k):
        rr,cc,v=map(int,input().split())
        rr-=1
        cc-=1
        g[rr][cc]=v
    for j in range(1,r+1):
        for k in range(1,c+1):
            for i in range(4):
                dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])#,dp[i][j-1][k])
                dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][k])
                if g[i-1][j-1]>0:
                    if k>0:
                        if dp[i][j][k]<dp[i][j-1][k-1]+g[i-1][j-1]:
                            dp[i][j][k]=dp[i][j-1][k-1]+g[i-1][j-1]
                    if dp[i][j][1]<dp[i-1][j][k]+g[i-1][j-1]:
                        dp[i][j][1]=dp[i-1][j][k]+g[i-1][j-1]
    print(max(dp[0][r][c],dp[1][r][c],dp[2][r][c],dp[3][r][c]))
if __name__=='__main__':
    main()



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

def main():
    r,c,k=map(int,input().split())
    dp=[[[0 for jj in range(c+1)] for ii in range(r+1)] for kk in range(4)]

    g=[[0 for j in range(c)] for i in range(r)]
    for i in range(k):
        rr,cc,v=map(int,input().split())
        rr-=1
        cc-=1
        g[rr][cc]=v
    for i in range(1,r+1):
        for j in range(1,c+1):
            for k in range(4):
                dp[k][i][j]=max(dp[k][i][j],dp[k][i][j-1])#,dp[i][j-1][k])
                dp[0][i][j]=max(dp[0][i][j],dp[k][i-1][j])
                if g[i-1][j-1]>0:
                    if k>0:
                        if dp[k][i][j]<dp[k-1][i][j-1]+g[i-1][j-1]:
                            dp[k][i][j]=dp[k-1][i][j-1]+g[i-1][j-1]
                    if dp[1][i][j]<dp[k][i-1][j]+g[i-1][j-1]:
                        dp[1][i][j]=dp[k][i-1][j]+g[i-1][j-1]
    print(max(dp[0][r][c],dp[1][r][c],dp[2][r][c],dp[3][r][c]))
if __name__=='__main__':
    main()
Buggy code:
#!/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 = [[[0]*4 for i in range(3005)] for j in range(3005)]

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]))


Fixed code:
#!/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 code:
R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for j in range(C):
        if goods[i][j] == 0:
            if i < R - 1:
                dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            
            if j < C - 1:
                for k in range(4):
                    dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])

        else:
            if i < R - 1:
                dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
                dp[i+1][j][1] = max(dp[i+1][j][1], max(dp[i][j]) + goods[i][j])
            
            if j < C - 1:
                for k in range(4):
                    dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
                for k in range(3):
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][j])

for i in range(3):
    dp[R-1][C-1][i] += goods[R-1][C-1]
print(max(dp[R-1][C-1]))

Fixed code:
R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

dp = [[[0] * C for _ in range(4)] for _ in range(R)]
dp[0][1][0] = goods[0][0]
for i in range(R):
    for j in range(4):
        for k in range(C):
            if i < R - 1:
                dp[i+1][0][k] = max(dp[i+1][0][k], dp[i][j][k])
                dp[i+1][1][k] = max(dp[i+1][1][k], dp[i][j][k] + goods[i+1][k])
        
            if k < C - 1:
                dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
                if j < 3:
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k] + goods[i][k+1])

res = 0
for i in range(4):
    res = max(res, dp[R-1][i][C-1])
print(res)
Buggy code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()

Fixed code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*w for j in H] for i in range(4)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[0][r-1][c-1]=v
  if dp[0][0][0]!=0:
    dp[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()