Problem:


Score : 500 points

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

Find the maximum possible sum of the values of items he picks up.

Constraints

1 \leq R, C \leq 3000

1 \leq K \leq \min(2 \times 10^5, R \times C)

1 \leq r_i \leq R

1 \leq c_i \leq C

(r_i, c_i) \neq (r_j, c_j) (i \neq j)

1 \leq v_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

Output

Print the maximum possible sum of the values of items Takahashi picks up.

Sample Input 1

2 2 3
1 1 3
2 1 4
1 2 5

Sample Output 1

8

He has two ways to get to the goal:

Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.

Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.

Thus, the maximum possible sum of the values of items he picks up is 8.

Sample Input 2

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2

Sample Output 2

29

We have four items in the 1-st row. The optimal choices are as follows:

Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.

Sample Input 3

4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

Sample Output 3

142



Sample input:
2 2 3
1 1 3
2 1 4
1 2 5


Sample output:
8

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
g = [[-float('inf')]*C for _ in range(R)]
for i in range(K):
    r, c, k = map(int, input().split())
    r -= 1
    c -= 1
    g[r][c] = k

dp = [[[0]*4 for _ in range(C+1)] for _ in range(R+1)]
for r in range(R):
    for c in range(C):
        # k[0..3] = 0個拾っている状態から3個拾っている状態
        # indexが大きいほど、小さいアイテムとなるので、小さいものから更新していく
        for k in range(3):
            dp[r][c][k+1] = max(
                dp[r][c][k+1],
                dp[r][c][k]+g[r][c]
            )

        for k in range(4):
            dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
            dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
print(max(dp[R-1][C-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp0 = dp[0]; dp1 = dp[1]; dp2 = dp[2]; dp3 = dp[3]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        # No use down
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        # No use right
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp0 = dp[0]; dp1 = dp[1]; dp2 = dp[2]; dp3 = dp[3]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        for k in range(4):
            ij = i * C + j
            ij1 = ij + 1
            i1j = ij + C
            item = field[i][j]
            # Use
            if item:
                dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
                dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
                dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
                dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
                dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
                dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
            # No use down
            dp0[i1j] = max(dp0[i1j], dp0[ij]) 
            dp0[i1j] = max(dp0[i1j], dp1[ij]) 
            dp0[i1j] = max(dp0[i1j], dp2[ij]) 
            # No use right
            dp0[ij1] = max(dp0[ij1], dp0[ij]) 
            dp1[ij1] = max(dp1[ij1], dp1[ij]) 
            dp2[ij1] = max(dp2[ij1], dp2[ij]) 

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

Submission status: Wrong Answer

Solution code:
# WA

# R, C: H, W

H, W, K = map(int, input().split())
# YXV = [list(map(int, input().split())) for _ in range(K)]
# H, W, K = 3000, 3000, 2*10**5
# YXV = [(i//H, i%W, i) for i in range(K)]

item = [[0 for _ in range(W+2)] for _ in range(H+2)]
for _ in range(K):
    y, x, v = map(int, input().split())
    item[y][x] = v

# [print(item_i) for item_i in item]

dp = [[[0]*4 for _ in range(W+2)] for _ in range(H+2)]
dp[1][1][1] = item[1][1] 
for ty in range(1, H+1):
    for tx in range(1, W+1):
        # アイテムを拾わない
        # print(f"pos: ({tx}, {ty})", end="\r")
        for k in range(4):
            dp[ty+1][tx][0] = max(
                dp[ty+1][tx][0],
                dp[ty][tx][k]
            )
            dp[ty][tx+1][k] = max(
                dp[ty][tx+1][k],
                dp[ty][tx][k]
            )
        # print((x, y), item[y][x])
        if item[ty][tx] == 0:
            continue
        # アイテムを拾う
        for k in range(4):
            dp[ty+1][tx][0] = max(
                dp[ty+1][tx][0],
                dp[ty][tx][k] + item[ty+1][tx]
            )
        for k in range(3):
            dp[ty][tx+1][k+1] = max(
                dp[ty][tx+1][k+1],
                dp[ty][tx][k] + item[ty][tx+1]
            )

# for dp_i in dp:
#     print(dp_i)

print(max(dp[H][W]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
p = []
for k in range(K):
  p.append(list(map(int, input().split())))

maps = [[0 for _ in range(C)] for _ in range(R)]
for k in range(K):
  maps[p[k][0]-1][p[k][1]-1] += p[k][2]

point = [[[0, 0, 0] for _ in range(C)] for _ in range(R)]

point[0][0][0] = maps[0][0]

for k in range(R + C):
  for r in range(max(0, k - C + 1), min(k+1, R)):
    c = k - r
    a, b, d = point[r][c]
    if r < R - 1:
      point[r+1][c][0] = maps[r+1][c] + max(a, b, d)      
    if c < C - 1:
      x = maps[r][c+1]
      if x > 0:
        y =  point[r][c+1]
        y[0] = max(y[0], x, a)
        y[1] = max(a + x, b)
        y[2] = max(b + x, d)
        point[r][c+1] = y
      else:
        point[r][c+1] = point[r][c]


print(max(point[-1][-1][0:3]))

Submission status: Wrong Answer

Solution code:
r, c, k = map(int, input().split())
item = []
for _ in range(r):
    item.append([0]*c)
for _ in range(k):
    x, y, v = map(int, input().split())
    item[x-1][y-1] = v
score = []
for _ in range(r):
    score.append([0]*c)
gotItem = []
for _ in range(r):
    gotItem.append([])
score[0][0] = item[0][0]
gotItem[0].append(item[0][0])
for i in range(r):
    for j in range(c):
        if i > 0 and j == 0:
            score[i][j] = score[i-1][j] + item[i][j]
            gotItem[i].append(item[i][j])
        if i == 0 and j > 0:
            score[i][j] = score[i][j-1] + item[i][j]
            gotItem[i].append(item[i][j])
            if len(gotItem[i]) > 3:
                score[i][j] -= min(gotItem[i])
                gotItem[i].remove(min(gotItem[i]))
        if i > 0 and j > 0 and score[i-1][j] >= score[i][j-1]:
            score[i][j] = score[i-1][j] + item[i][j]
            gotItem[i] = [item[i][j]]
        if i > 0 and j > 0 and score[i-1][j] < score[i][j-1]:
            score[i][j] = score[i][j-1] + item[i][j]
            gotItem[i].append(item[i][j])
            if len(gotItem[i]) > 3:
                score[i][j] -= min(gotItem[i])
                gotItem[i].remove(min(gotItem[i]))
print(score[r-1][c-1])

Submission status: Wrong Answer

Solution code:
import sys
import time
s1 = time.time()
*data, = map(int, sys.stdin.read().split()[::-1])

def inp():
    global data
    return data.pop()
R, C, k = inp(), inp(), inp()
grid = [[0] * C for _ in range(R)]

for _ in range(k):
    ri, ci, v = inp(), inp(), inp()
    grid[ri - 1][ci - 1] = v


dp = []
for _ in range(4):
    dp.append([[0] * (C + 1) for _ in range(R + 1)])

# print(time.time() - s1)
mx = 0
for row in range(1, R + 1):
    for col in range(1, C + 1):
        level = 0
        while level < 4:
            dp[1][row][col] = max(dp[1][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            dp[0][row][col] = max(dp[0][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            level += 1
        level = 1
        while level < 4:
            dp[level][row][col] = max(
                dp[level][row][col],
                dp[level - 1][row][col - 1] + grid[row - 1][col - 1],
                dp[level][row][col - 1]
            )
            mx = max(mx, dp[level][row][col])
            level += 1

# print(time.time() - s1)
print(mx)

Submission status: Wrong Answer

Solution code:
import sys
import time
s1 = time.time()
*data, = map(int, sys.stdin.read().split()[::-1])

def inp():
    global data
    return data.pop()
R, C, k = inp(), inp(), inp()
grid = [[0] * C for _ in range(R)]

for _ in range(k):
    ri, ci, v = inp(), inp(), inp()
    grid[ri - 1][ci - 1] = v


dp = []
for _ in range(4):
    dp.append([[0] * (C + 1) for _ in range(R + 1)])

# print(time.time() - s1)
mx = 0
for row in range(1, R + 1):
    for col in range(1, C + 1):
        level = 0
        while level < 4:
            dp[1][row][col] = max(dp[1][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            dp[0][row][col] = max(dp[1][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            level += 1
        level = 1
        while level < 4:
            dp[level][row][col] = max(
                dp[level][row][col],
                dp[level - 1][row][col - 1] + grid[row - 1][col - 1],
                dp[level][row][col - 1]
            )
            mx = max(mx, dp[level][row][col])
            level += 1

# print(time.time() - s1)
print(mx)

Submission status: Wrong Answer

Solution code:
R,C,K = map(int,input().split())
dp0 = [[0 for _ in range(C+1)] for f in range(R+1)]
dp1 = [[0 for _ in range(C+1)] for f in range(R+1)]
dp2 = [[0 for _ in range(C+1)] for f in range(R+1)]
dp3 = [[0 for _ in range(C+1)] for f in range(R+1)]
vl = [[0 for _ in range(C+1)] for f in range(R+1)]

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

for i in range(1, R+1):
	for j in range(1, C+1):
		dp0[i][j] = max(dp0[i][j], dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])
		dp3[i][j] = max(dp3[i][j], dp2[i][j-1]+vl[i][j], dp2[i][j]+vl[i][j])
		dp2[i][j] = max(dp2[i][j], dp1[i][j-1]+vl[i][j], dp1[i][j]+vl[i][j])
		dp1[i][j] = max(dp1[i][j], dp0[i][j-1]+vl[i][j], dp0[i][j]+vl[i][j])

print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))

Submission status: Wrong Answer

Solution code:
import sys
import time
s1 = time.time()
*data, = map(int, sys.stdin.read().split()[::-1])

def inp():
    global data
    return data.pop()
R, C, k = inp(), inp(), inp()
grid = [[0] * C for _ in range(R)]

for _ in range(k):
    ri, ci, v = inp(), inp(), inp()
    grid[ri - 1][ci - 1] = v


dp = []
for _ in range(4):
    dp.append([[0] * (C + 1) for _ in range(R + 1)])

print(time.time() - s1)
mx = 0
for row in range(1, R + 1):
    for col in range(1, C + 1):
        level = 0
        while level < 4:
            dp[1][row][col] = max(dp[1][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            dp[0][row][col] = max(dp[1][row][col], dp[level][row - 1][col] + grid[row - 1][col - 1])
            level += 1
        level = 1
        while level < 4:
            dp[level][row][col] = max(
                dp[level][row][col],
                dp[level - 1][row][col - 1] + grid[row - 1][col - 1],
                dp[level][row][col - 1]
            )
            mx = max(mx, dp[level][row][col])
            level += 1

print(time.time() - s1)
print(mx)

Submission status: Wrong Answer

Solution code:
import sys
import queue

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def rc_to_s(r,c):
	return str(r)+'_'+str(c)
	
def s_to_rc(s):
	r,c = map(int, s.split('_'))
	return r, c
	
class qqq:
	def __init__(self):
		self.l=[]
		self.sum=0
		self.num=0
	
	def update(self, v):
		if self.num==3 and v > self.l[0]:
			self.l[0]=v
			self.l.sort()
			self.sum=sum(self.l)
		elif self.num < 3:
			self.l.append(v)
			self.l.sort()
			self.sum += v
			self.num += 1
			
			
def main():
	#a = int(input())
	r, c, k = tin()
	#s = input()
	dd={}
	for _ in range(k):
		ri, ci, vi = tin()
		dd[rc_to_s(ri-1,ci-1)]=vi
	
	cv=[0]*c

	pq=qqq()
	npq=0
	vpq=0
	for ci in range(c):
		ps = rc_to_s(0, ci)
		if ps in dd:
			tv=dd[ps]
			pq.update(tv)
		cv[ci]=pq.sum
		
	for ri in range(1, r):
		ncv = [0]*c
		for ci in range(c):
			if ci == 0 or ncv[ci-1] <= cv[ci]:
				pq=qqq()
				ovpq=cv[ci]	
			ps = rc_to_s(ri, ci)
			if ps in dd:
				tv=dd[ps]
				pq.update(tv)

			ncv[ci]=ovpq + pq.sum
		cv=ncv
		#pa(cv)
	return cv[-1]
		
	
	
	

		
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
# pypy
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from collections import defaultdict
import gc

R, C, K = map(int, readline().split())
rcv = list(map(int, read().split()))
rr = rcv[::3]
cc = rcv[1::3]
vv = rcv[2::3]
rc2v = defaultdict(int)
for _r, _c, _v in zip(rr, cc, vv):
    rc2v[(_r-1, _c-1)] = _v

del rcv, rr, cc, vv
gc.collect()


dp = [[0] * C*4 for r in range(2)]
dp[0][1] = rc2v[(0, 0)]
for c in range(1, C):
  idc1 = c * 4
  idc2 = idc1 - 4
  for k in range(1, 4):
    idx1 = idc1 + k
    idx2 = idc2 + k
    idx3 = idx2 - 1
    dp[0][idx1] = max(dp[0][idx2], dp[0][idx3]+rc2v[(0, c)])

for r in range(1, R):
  rr = r % 2
  dp[rr][0] = max(dp[rr-1][:4])
  if (r, 0) in rc2v:
    dp[rr][1] = dp[rr][0] + rc2v[(rr, 0)]
  
  for c in range(1, C):
    idc1 = c * 4
    idc2 = idc1 - 4
    dp[rr][idc1] = max(dp[rr][idc2], max(dp[rr-1][idc1:idc1+4]))
    for k in range(1, 4):
      idx1 = idc1 + k
      idx2 = idc2 + k
      idx3 = idx2 - 1
      dp[rr][idx1] = max(dp[rr][idx2], dp[rr][idx3] + rc2v[(r, c)])
    dp[rr][idc1+1] = max(dp[rr][idc1+1], dp[rr][idc1] + rc2v[(r, c)])

if R % 2:
  print(max(dp[0][-4:]))
else:
  print(max(dp[-1][-4:]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
R,C,K = map(int,input().split())
goods = [[0]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = map(int,input().split())
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if goods[1][1]:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1]:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1],DP[1][j+1][0])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1],DP[1][j+1][1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1],DP[1][j+1][2])
    else:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j+1][0])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j+1][1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j+1][2])
for i in range(1,R):
    DP[i+1][1][0] = DP[i][1][3]
    if goods[i+1][1]:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(DP[i-1][j][3],DP[i][j-1][0])
        if goods[i][j]:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1],DP[i][j][0])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j],DP[i][j][1])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j],DP[i][j][2])
        else:
            DP[i][j][1] = max(DP[i][j-1][1],DP[i][j][0])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j][1])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j][2])
print(DP[R][C][3])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        for k in range(3):
            ij = i * C + j
            ij1 = i * C + j + 1
            i1j = (i + 1) * C + j
            # Use
            if field[i][j] and k < 3:
                dp[k+1][ij1] = max(dp[k+1][ij1], dp[k][ij] + field[i][j]) 
                dp[0][i1j] = max(dp[0][i1j], dp[k][ij] + field[i][j]) 
            # No use down
            dp[0][i1j] = max(dp[0][i1j], dp[k][ij]) 
            # No use right
            dp[k][ij1] = max(dp[k][ij1], dp[k][ij]) 

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

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    R, C, K = map(int, input().split())

    field = [[0 for j in range(C)] for i in range(R)]

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

    dp = [[0] * 4 for j in range(C)]

    for i in range(R):
        for j in range(C):
            ct = field[i][j]
            premax = max(dp[j])

            if ct == 0:
                dp[j][0] = max(dp[j - 1][0], premax)

                for k in range(1, 4):
                    if j > 0:
                        dp[j][k] = dp[j - 1][k]
            else:
                dp[j][0] = max(dp[j - 1][0], premax)
                dp[j][1] = max(ct + max(dp[j - 1][0], premax), dp[j - 1][1])
                for k in range(2, 4):
                    if j > 0:
                        dp[j][k] = max(dp[j - 1][k - 1] + ct, dp[j - 1][k])

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    R, C, K = map(int, input().split())
    V = [[0]*C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        V[r-1][c-1] = v
    
    dp = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    dp[0][0][1] = V[0][0]
    for i in range(R):
        for j in range(C):
            for k in range(4):
                dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k], dp[i][j-1][k])
                if V[i][j] == 0:
                    continue
                for n in range(4):
                    if i > 0 and n > 0:
                        dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][n] + V[i][j])
                if j > 0 and k > 0:
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + V[i][j])

    print(max(dp[R-1][C-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# pypy
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

R, C, K = map(int, readline().split())
rcv = list(map(int, read().split()))
rr = rcv[::3]
cc = rcv[1::3]
vv = rcv[2::3]
rc2v = {}
for _r, _c, _v in zip(rr, cc, vv):
    rc2v[(_r-1, _c-1)] = _v

dp = [[0]*4 for c in range(C)]

for r in range(R):
    for c in range(C):
        if(c!=0):
            dp[c][0] = max(dp[c])
        else:
            dp[c][0] = max(dp[c-1][0], max(dp[c]))

        v = 0

        if (r, c) in rc2v:
            v = rc2v[(r, c)]
        for k in range(1,4):
            if(c!=0):
                dp[c][k] = max(dp[c-1][k], dp[c-1][k-1] + v)
            else:
                dp[c][k] = dp[c][0] + v
            

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
r,c,k=[int(x) for x in input().split()]
item=[[0]*(c+1) for i in range(r+1)]
for i in range(k):
  y,x,v=[int(x) for x in input().split()]
  item[y][x]=v
dp=[[0]*(c+1) for i in range(r+1)]

for i in range(1,r+1):
  cnt=0
  m=[]
  for j in range(1,c+1):
    if item[i][j]!=0:
      if dp[i-1][j]>=dp[i][j-1]:
        cnt=1
        m=[item[i][j]]
        dp[i][j]=dp[i-1][j]+item[i][j]
      else:
        if cnt<3:
          cnt+=1
          m.append(item[i][j])
          dp[i][j]=dp[i][j-1]+item[i][j]
        elif cnt>=3:
          if item[i][j]>min(m):
            dp[i][j]=dp[i][j-1]+item[i][j]-min(m)
            m.remove(min(m))
            m.append(item[i][j])
          else:
            dp[i][j]=dp[i][j-1]
    else:
      if dp[i-1][j]>=dp[i][j-1]:
        cnt=0
        m=[]
        dp[i][j]=dp[i-1][j]
      else:
        dp[i][j]=dp[i][j-1]
print(dp[r][c])

Submission status: Wrong Answer

Solution code:
"""
dp[i][j]　=> i行目j列目がゴールの場合の最大値
dp[i][j] => 
"""

R,C,K = map(int,input().split())
items = [[0]*(C) for _ in range(R)]

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

dp = [[0]*C for _ in range(R)]

#0行目を初期化する
memo = [0,0,0]

v = items[0][0]
memo[-1] = v
dp[0][0] = items[0][0]

for j in range(1,C):
    memo.sort()
    v = items[0][j]
    dp[0][j] = dp[0][j-1]
    if v > memo[0]:
        dp[0][j] += v-memo[0]
        memo[0] = v

for i in range(1,R):
    memo = [0,0,0]
    for j in range(C):
        memo.sort()
        v = items[i][j]
        if j != 0:
            #左から入る場合の価値を求める。
            dp[i][j] = dp[i][j-1]
            if v > memo[0]:
                dp[i][j] += v-memo[0]
                memo[0] = v
        
        #上から入る場合の価値を求める。
        tmp2 = dp[i-1][j] + v
        
        if tmp2 > dp[i][j]:
            dp[i][j] = tmp2
            memo = [0,0,v]

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

Submission status: Wrong Answer

Solution code:
import sys
import queue

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def rc_to_s(r,c):
	return str(r)+'_'+str(c)
	
def s_to_rc(s):
	r,c = map(int, s.split('_'))
	return r, c


def main():
	#a = int(input())
	r, c, k = tin()
	#s = input()
	dd={}
	for _ in range(k):
		ri, ci, vi = tin()
		dd[rc_to_s(ri-1,ci-1)]=vi
	
	cv=[0]*c

	pq=queue.PriorityQueue()
	npq=0
	vpq=0
	for ci in range(c):
		ps = rc_to_s(0, ci)
		if ps in dd:
			tv=dd[ps]
			if npq < 3:
				pq.put(tv)
				npq+=1
				vpq+=tv
			else:
				min_v = pq.get()
				pq.put(max(tv, min_v))
				vpq += max(tv, min_v) - min_v
		cv[ci]=vpq
		
	for ri in range(1, r):
		ncv = [0]*c
		for ci in range(c):
			if ci == 0 or ncv[ci-1] <= cv[ci]:
				pq=queue.PriorityQueue()
				npq=0
				vpq=cv[ci]	
			ps = rc_to_s(ri, ci)
			if ps in dd:
				tv=dd[ps]
				if npq < 3:
					pq.put(tv)
					npq+=1
					vpq+=tv
				else:
					min_v = pq.get()
					pq.put(max(tv, min_v))
					vpq += max(tv, min_v) - min_v
			ncv[ci]=vpq
		cv=ncv
		#pa(cv)
	return cv[-1]

			
		
		
	
	
	

		
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
R,C,K = map(int,input().split())
goods = [[0]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = map(int,input().split())
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if not goods[1][1]:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if not goods[1][j+1]:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if not goods[i+1][1]:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if not goods[i][j]:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            DP[i][j][1] = DP[i][j-1][1]
            DP[i][j][2] = DP[i][j-1][2]
            DP[i][j][3] = DP[i][j-1][3]
print(max(DP[R][C]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
R,C,K = map(int,input().split())
goods = [[0]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = map(int,input().split())
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if not goods[1][1]:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1] != -1:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if not goods[i+1][1]:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if not goods[i][j]:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            DP[i][j][1] = DP[i][j-1][1]
            DP[i][j][2] = DP[i][j-1][2]
            DP[i][j][3] = DP[i][j-1][3]
print(max(DP[R][C]))

Submission status: Wrong Answer

Solution code:
import sys
# import heapq, functools, collections
# import math, random
# from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(items,R,C):  # fix inputs here
    console("----- solving ------")
    console(R,C)

    grid = [[0 for _ in range(C)] for _ in range(R)]

    for r,c,v in items:
        grid[r-1][c-1] = v

    # console(np.array(grid))  # PLEASE SILENCE
    console("\n\n")

    dp = [[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]

    if grid[0][0] > 0:
        dp[0][0][1] = dp[0][0][0] + grid[0][0]

    for i in range(1,R):
        dp[i][0][0] = max(dp[i-1][0][0], dp[i-1][0][1])
        if grid[i][0] > 0:
            dp[i][0][1] = dp[i][0][0] + grid[i][0]

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

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp)[:,0,:])  # PLEASE SILENCE
    # console(np.array(dp)[0,:,:])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE
    # console("\n\nPost")

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

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,1])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,2])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,3])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE

    # return a string (i.e. not a list or matrix)
    return max(dp[R-1][C-1][k] for k in range(4))


def console(*args):  # the judge will not read these print statement
    # print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
inn = sys.stdin.readlines()

for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    r,c,nrows = list(map(int,inn[0].split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    grid = []
    for t in range(1,nrows+1):
        grid.append(list(map(int,inn[t].split())))

    res = solve(grid,r,c)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)