Buggy python submission (verdict: Time Limit Exceeded):
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
dp[1][1][1] = rcv[1][1]
#print(dp[1][1][1])
for r in range(1,R+1):
  for c in range(1,C+1):
    dp[r][c][1] = max(dp[r][c][1],dp[r][c-1][0]+rcv[r][c],dp[r][c-1][1])
    dp[r][c][2] = max(dp[r][c-1][1]+rcv[r][c],dp[r][c-1][2])
    dp[r][c][3] = max(dp[r][c-1][2]+rcv[r][c],dp[r][c-1][3])
    
    if r < R:
      dp[r+1][c][0] = max(dp[r][c])
      dp[r+1][c][1] = max(dp[r][c])+rcv[r+1][c]
print(max(dp[R][C]))
#print(*dp)

Fixed submission:
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp0=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp2=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp3=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1[1][1] = rcv[1][1]
for r in range(1,R+1):
  for c in range(1,C+1):
    dp1[r][c] = max(dp0[r][c]+rcv[r][c],dp0[r][c-1]+rcv[r][c],dp1[r][c-1])
    dp2[r][c] = max(dp1[r][c-1]+rcv[r][c],dp2[r][c-1])
    dp3[r][c] = max(dp2[r][c-1]+rcv[r][c],dp3[r][c-1])
    
    if r < R:
      dp0[r+1][c] = max(dp0[r][c],dp1[r][c],dp2[r][c],dp3[r][c])
print(max(dp0[R][C],dp1[R][C],dp2[R][C],dp3[R][C]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
dp[1][1][1] = rcv[1][1]
#print(dp[1][1][1])
for r in range(1,R+1):
  for c in range(1,C+1):
    dp[r][c][1] = max(dp[r][c][1],dp[r][c-1][0]+rcv[r][c],dp[r][c-1][1])
    dp[r][c][2] = max(dp[r][c-1][1]+rcv[r][c],dp[r][c-1][2])
    dp[r][c][3] = max(dp[r][c-1][2]+rcv[r][c],dp[r][c-1][3])
    
    if r < R:
      dp[r+1][c][0] = max(dp[r][c])
      dp[r+1][c][1] = max(dp[r][c])+rcv[r+1][c]
print(max(dp[R][C]))
#print(*dp)

Fixed submission:
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp0=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp2=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp3=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1[1][1] = rcv[1][1]
for r in range(1,R+1):
  for c in range(1,C+1):
    dp1[r][c] = max(dp0[r][c]+rcv[r][c],dp0[r][c-1]+rcv[r][c],dp1[r][c-1])
    dp2[r][c] = max(dp1[r][c-1]+rcv[r][c],dp2[r][c-1])
    dp3[r][c] = max(dp2[r][c-1]+rcv[r][c],dp3[r][c-1])
    
    if r < R:
      dp0[r+1][c] = max(dp0[r][c],dp1[r][c],dp2[r][c],dp3[r][c])
print(max(dp0[R][C],dp1[R][C],dp2[R][C],dp3[R][C]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
if rcv[1][1]>0:
  dp[1][1][1] = rcv[1][1]
#print(dp[1][1][1])
for r in range(1,R+1):
  cntmax=1
  for c in range(1,C+1):
    if rcv[r][c] > 0:
      cntmax += 1
    if r*c == 1:
      continue
    for i in range(min(cntmax,4)):
      candi=list()
      if r > 1 and i==0:  #うえからおりてくる、とらない(i=0)
        candi.extend(dp[r-1][c])
      if r > 1 and i==1 and rcv[r][c]>0:  #うえからおりてくる、とる(i=1)
        candi.extend([dp[r-1][c][j]+rcv[r][c] for j in range(4)])
      if c > 1: # ひだりから、とらない(i維持)
        candi.append(dp[r][c-1][i])
        if 0 < i and rcv[r][c]>0:  # ひだりから、とる(i-1から移行)
          candi.append(dp[r][c-1][i-1]+rcv[r][c])
      if len(candi) == 0:
        continue
      dp[r][c][i] = max(candi)
      #print(r,c,i,dp[r][c][i])
print(max(dp[R][C]))

Fixed submission:
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp0=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp2=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp3=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1[1][1] = rcv[1][1]
for r in range(1,R+1):
  for c in range(1,C+1):
    dp1[r][c] = max(dp0[r][c]+rcv[r][c],dp0[r][c-1]+rcv[r][c],dp1[r][c-1])
    dp2[r][c] = max(dp1[r][c-1]+rcv[r][c],dp2[r][c-1])
    dp3[r][c] = max(dp2[r][c-1]+rcv[r][c],dp3[r][c-1])
    
    if r < R:
      dp0[r+1][c] = max(dp0[r][c],dp1[r][c],dp2[r][c],dp3[r][c])
print(max(dp0[R][C],dp1[R][C],dp2[R][C],dp3[R][C]))

Buggy python submission (verdict: Time Limit Exceeded):
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
"""
    info = dict()
for r,item in rcv.items():
  item.sort(key=lambda x:x[0])
  if len(item) < 2:
    info[r] = [[item[0][0],item[0][0],item[0][1]]]
  elif len(item) < 3:
    info[r] = [[item[0][0],item[1][0],item[0][1]+item[1][1]]]
  else:
    ret=list()
    for i in range(len(item)-2):
      ret.append([item[i][0],item[i+2][0],sum([item[j][1] for j in range(i,i+3)])])

# info: 各行でmax3つのアイテムをとるときの列区間とそのvの合計
"""
# dp: 各場所までの合計の最大値
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
if rcv[1][1]>0:
  dp[1][1][1] = rcv[1][1]
#print(dp[1][1][1])
for r in range(1,R+1):
  for c in range(1,C+1):
    if r*c == 1:
      continue
    for i in range(4):
      candi=list()
      if r > 1 and i==0:  #うえからおりてくる、とらない(i=0)
        candi.extend(dp[r-1][c])
      if r > 1 and i==1 and rcv[r][c]>0:  #うえからおりてくる、とる(i=1)
        candi.extend([dp[r-1][c][j]+rcv[r][c] for j in range(4)])
      if c > 1: # ひだりから、とらない(i維持)
        candi.append(dp[r][c-1][i])
        if 0 < i and rcv[r][c]>0:  # ひだりから、とる(i-1から移行)
          candi.append(dp[r][c-1][i-1]+rcv[r][c])
      if len(candi) == 0:
        continue
      dp[r][c][i] = max(candi)
      #print(r,c,i,dp[r][c][i])
print(max(dp[R][C]))

Fixed submission:
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp0=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp2=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp3=[[0 for _ in range(C+1)] for _ in range(R+1)]
dp1[1][1] = rcv[1][1]
for r in range(1,R+1):
  for c in range(1,C+1):
    dp1[r][c] = max(dp0[r][c]+rcv[r][c],dp0[r][c-1]+rcv[r][c],dp1[r][c-1])
    dp2[r][c] = max(dp1[r][c-1]+rcv[r][c],dp2[r][c-1])
    dp3[r][c] = max(dp2[r][c-1]+rcv[r][c],dp3[r][c-1])
    
    if r < R:
      dp0[r+1][c] = max(dp0[r][c],dp1[r][c],dp2[r][c],dp3[r][c])
print(max(dp0[R][C],dp1[R][C],dp2[R][C],dp3[R][C]))