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:
import sys

R, C, K = map(int, sys.stdin.readline().split())

grid = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, sys.stdin.readline().split())
    grid[r-1][c-1] = v

# r行目、c列目にいるときに、その行でアイテムをi個取得している場合の最大価値
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[0][0] = grid[0][0]
# print(dp)
for r in range(R):
    for c in range(C):
        # 取らない
        dp0[r+1][c+1] = max((dp0[r+1][c], dp0[r][c+1], dp1[r][c+1], dp2[r][c+1], dp3[r][c+1]))

        dp1[r+1][c+1] = max(dp1[r+1][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp2[r+1][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp3[r+1][c], dp3[r+1][c+1])

        # 取る
        dp0[r+1][c+1] = max(dp0[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp1[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp2[r][c+1] + grid[r][c], dp0[r+1][c+1])

        dp1[r+1][c+1] = max(dp0[r+1][c] + grid[r][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp1[r+1][c] + grid[r][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp2[r+1][c] + grid[r][c], dp3[r+1][c+1])

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

Submission status: Wrong Answer

Solution code:
import sys

R, C, K = map(int, sys.stdin.readline().split())

grid = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, sys.stdin.readline().split())
    grid[r-1][c-1] = v

# r行目、c列目にいるときに、その行でアイテムをi個取得している場合の最大価値
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)]
# print(dp)
for r in range(R):
    for c in range(C):
        # 取らない
        dp0[r+1][c+1] = max((dp0[r+1][c], dp0[r][c+1], dp1[r][c+1], dp2[r][c+1], dp3[r][c+1]))

        dp1[r+1][c+1] = max(dp1[r+1][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp2[r+1][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp3[r+1][c], dp3[r+1][c+1])

        # 取る
        dp0[r+1][c+1] = max(dp0[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp1[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp2[r][c+1] + grid[r][c], dp0[r+1][c+1])

        dp1[r+1][c+1] = max(dp0[r+1][c] + grid[r][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp1[r+1][c] + grid[r][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp2[r+1][c] + grid[r][c], dp3[r+1][c+1])

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

Submission status: Wrong Answer

Solution code:
import sys

R, C, K = map(int, sys.stdin.readline().split())

grid = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, sys.stdin.readline().split())
    grid[r-1][c-1] = v

# r行目、c列目にいるときに、その行でアイテムをi個取得している場合の最大価値
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)]
# print(dp)
for r in range(R):
    for c in range(C):
        # 取らない
        dp0[r+1][c+1] = max((dp0[r+1][c], dp0[r][c+1], dp1[r][c+1], dp2[r][c+1], dp3[r][c+1]))

        dp1[r+1][c+1] = max(dp1[r+1][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp2[r+1][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp3[r+1][c], dp3[r+1][c+1])

        if grid[r][c] == 0:
            continue
        # 取る
        dp0[r+1][c+1] = max(dp0[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp1[r][c+1] + grid[r][c], dp0[r+1][c+1])
        dp0[r+1][c+1] = max(dp2[r][c+1] + grid[r][c], dp0[r+1][c+1])

        dp1[r+1][c+1] = max(dp0[r+1][c] + grid[r][c], dp1[r+1][c+1])
        dp2[r+1][c+1] = max(dp1[r+1][c] + grid[r][c], dp2[r+1][c+1])
        dp3[r+1][c+1] = max(dp2[r+1][c] + grid[r][c], dp3[r+1][c+1])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split(' '))
dp = [[[0, 1 << C] for i in range(C)] for j in range(R)]
original = [[0 for i in range(C)] for j in range(R)]

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

for i in range(R):
    for j in range(C):
        if i == 0 and j == 0 and dp[i][j][0] > 0:
            dp[i][j][1] = 1 << 0
        elif j == 0:
            dp[i][j][0] += dp[i - 1][j][0]
        else:
            top, left = -1, dp[i][j - 1]
            if i > 0:
                top = dp[i - 1][j][0]
            if top >= left[0]:
                if original[i][j]>0:
                    dp[i][j][1]= 1<<j
                dp[i][j][0] += top
            else:
                chosen = bin(left[1]).count('1')
                if chosen < 3:
                    dp[i][j][0] += left[0]
                    dp[i][j][1] = left[1] | (1 << j)
                    pass
                else:
                    # add if greater than minimal
                    minimal, idx = dp[i][j][0], j
                    for k in range(j):
                        if 1 << k & left[1]:
                            if original[i][k] < minimal:
                                minimal = original[i][k]
                                idx = k
                    if idx!=j:
                        dp[i][j][0]+=left[0]-minimal
                        dp[i][j][1]=(left[1]&~(1<<idx))|(1<<j)
                    else:
                        dp[i][j][0]+=left[0]
                        dp[i][j][1]=left[1]

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

Submission status: Wrong Answer

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

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

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

dp[0][0][1] = rcv[0][0]

for j in range(1, C):
  dp[0][j][0] = dp[0][j-1][0]
  dp[0][j][1] = max(dp[0][j-1][0] + rcv[0][j], dp[0][j-1][1])
  dp[0][j][2] = max(dp[0][j-1][1] + rcv[0][j], dp[0][j-1][2])
  dp[0][j][3] = max(dp[0][j-1][2] + rcv[0][j], dp[0][j-1][3])

for i in range(1, R):
  dp[i][0][0] = dp[i-1][0][0]
  dp[i][0][1] = max(dp[i-1][0][0] + rcv[i][0], dp[i-1][0][1])
  dp[i][0][2] = max(dp[i-1][0][1] + rcv[i][0], dp[i-1][0][2])
  dp[i][0][3] = max(dp[i-1][0][2] + rcv[i][0], dp[i-1][0][3])

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

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
rcv = [[0]*C for _ in range(R)]
dp = [[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]

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

dp[0][0][1] = rcv[0][0]

for j in range(1, C):
  dp[0][j][0] = dp[0][j-1][0]
  dp[0][j][1] = max(dp[0][j-1][0] + rcv[0][j], dp[0][j-1][1])
  dp[0][j][2] = max(dp[0][j-1][1] + rcv[0][j], dp[0][j-1][2])
  dp[0][j][3] = max(dp[0][j-1][2] + rcv[0][j], dp[0][j-1][3])

for i in range(1, R):
  dp[i][0][0] = dp[i-1][0][0]
  dp[i][0][1] = max(dp[i-1][0][0] + rcv[i][0], dp[i-1][0][1])
  dp[i][0][2] = max(dp[i-1][0][1] + rcv[i][0], dp[i-1][0][2])
  dp[i][0][3] = max(dp[i-1][0][2] + rcv[i][0], dp[i-1][0][3])

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[0]*(c+1) for j in range(4)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[i][cc]=max(dp[i][cc-1],dp[3][cc])
			else:
				dp[0][cc]=max(dp[3][cc],dp[0][cc-1])
				dp[1][cc]=max(dp[0][cc]+G[rr][cc],dp[1][cc-1])
				dp[2][cc]=max(dp[1][cc-1]+G[rr][cc],dp[1][cc])
				dp[3][cc]=max(dp[2][cc-1]+G[rr][cc],dp[2][cc])
	#print(dp)
	print(dp[-1][-1])















if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*C for _ in range(R)] for _ in range(4)]
for i in range(R):
    for k in range(C):
        if i >= 1:
            tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        else:
            tmp = 0
        target = grid[i][k]
        if k >= 1:
            dp[0][i][k] = max(dp[0][i][k-1],tmp)
            dp[1][i][k] = dp[1][i][k-1]
            dp[2][i][k] = dp[2][i][k-1]
            dp[3][i][k] = dp[3][i][k-1]
            if target != 0:
                dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
                if dp[1][i][k-1] != 0:
                    dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
                if dp[2][i][k-1] != 0:
                    dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
        else:
            dp[0][i][k] = tmp
            if target != 0:
                dp[1][i][k] = tmp+target
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[1][-1][-1],dp[3][-1][-1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*C for _ in range(R)] for _ in range(4)]
for i in range(R):
    for k in range(C):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i][k]
        if k >= 1:
            dp[0][i][k] = max(dp[0][i][k-1],tmp)
            dp[1][i][k] = dp[1][i][k-1]
            dp[2][i][k] = dp[2][i][k-1]
            dp[3][i][k] = dp[3][i][k-1]
            if target != 0:
                dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
                if dp[1][i][k-1] != 0:
                    dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
                if dp[2][i][k-1] != 0:
                    dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
        else:
            dp[0][i][k] = tmp
            if target != 0:
                dp[1][i][k] = tmp+target
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[1][-1][-1],dp[3][-1][-1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*C for _ in range(R)] for _ in range(4)]
for i in range(R):
    for k in range(C):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i][k]
        if k >= 1:
            dp[0][i][k] = max(dp[0][i][k-1],tmp)
            dp[1][i][k] = dp[1][i][k-1]
            dp[2][i][k] = dp[2][i][k-1]
            dp[3][i][k] = dp[3][i][k-1]
            if target != 0:
                dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
                if dp[1][i][k-1] != 0:
                    dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
                if dp[2][i][k-1] != 0:
                    dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
        else:
            if target != 0:
                dp[1][i][k] += target
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[1][-1][-1],dp[3][-1][-1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*(C+2) for _ in range(R+2)] for _ in range(4)]
for i in range(1,R+1):
    for k in range(1,C+1):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i-1][k-1]
        dp[0][i][k] = max(dp[0][i][k-1],tmp)
        dp[1][i][k] = dp[1][i][k-1]
        dp[2][i][k] = dp[2][i][k-1]
        dp[3][i][k] = dp[3][i][k-1]
        if target != 0:
            dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
            if dp[1][i][k-1] != 0:
                dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
            if dp[2][i][k-1] != 0:
                dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
print(max(dp[0][-2][-2],dp[1][-2][-2],dp[1][-2][-2],dp[3][-2][-2]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]
for i in range(1,R+1):
    for k in range(1,C+1):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i-1][k-1]
        dp[0][i][k] = max(dp[0][i][k-1],tmp)
        dp[1][i][k] = dp[1][i][k-1]
        dp[2][i][k] = dp[2][i][k-1]
        dp[3][i][k] = dp[3][i][k-1]
        if target != 0:
            dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
            if dp[1][i][k-1] != 0:
                dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
            if dp[2][i][k-1] != 0:
                dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[1][-1][-1],dp[3][-1][-1]))

Submission status: Wrong Answer

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

import heapq
dp = [[0]*r for i in range(c)]
dp[0][0] = v[0][0]

get = []

for j in range(r):
    for i in range(c):
        if i==0 and v[i][j]!=0:
            heapq.heappush(get,v[i][j])
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            x = v[i][j]
            m = 0
            if len(get)==3:
                m = heapq.heappop(get)
                x = max(m,x)
            dp[i][j] = dp[i-1][j] + x - m
            if x!=0:heapq.heappush(get,x)
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i][j-1] + v[i][j]
            get = []
            if v[i][j]!=0:
                heapq.heappush(get,v[i][j])
    get = []

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

Submission status: Wrong Answer

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

import heapq
dp = [[0]*r for i in range(c)]
dp[0][0] = v[0][0]

get = []

for j in range(r):
    for i in range(c):
        if i==0 and v[i][0]!=0:
            heapq.heappush(get,v[i][0])
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            x = v[i][j]
            m = 0
            if len(get)==3:
                m = heapq.heappop(get)
                x = max(m,x)
            dp[i][j] = dp[i-1][j] + x - m
            if x!=0:heapq.heappush(get,x)
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i][j-1] + v[i][j]
            get = []
            if v[i][j]!=0:
                heapq.heappush(get,v[i][j])
    get = []

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

Submission status: Wrong Answer

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

import heapq
dp = [[0]*r for i in range(c)]
dp[0][0] = v[0][0]

get = []
if v[0][0]!=0:
    heapq.heappush(get,v[0][0])

for j in range(r):
    for i in range(c):
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            x = v[i][j]
            m = 0
            if len(get)==3:
                m = heapq.heappop(get)
                x = max(m,x)
            dp[i][j] = dp[i-1][j] + x - m
            if x!=0:heapq.heappush(get,x)
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i][j-1] + v[i][j]
            get = []
            if v[i][j]!=0:
                heapq.heappush(get,v[i][j])
    get = []

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

Submission status: Wrong Answer

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

import heapq
dp = [[0]*c for i in range(r)]
dp[0][0] = v[0][0]

get = []
if v[0][0]!=0:
    heapq.heappush(get,v[0][0])

for i in range(r):
    for j in range(c):
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            x = v[i][j]
            m = 0
            if len(get)==3:
                m = heapq.heappop(get)
                x = max(m,x)
            dp[i][j] = dp[i][j-1] + x - m
            if x!=0:heapq.heappush(get,x)
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i-1][j] + v[i][j]
            get = []
            if v[i][j]!=0:
                heapq.heappush(get,v[i][j])
    get = []

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

Submission status: Wrong Answer

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

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

if rcv[0][0]>0:
    dp[0][0][0]=0
    dp[1][0][0]=rcv[0][0]

#左端を縦に
j=0
for i in range(1,R):
    if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に 
i=0
for j in range(1,C):
    if rcv[i][j]==0:
        for r in range(4):
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j])

for i in range(1,R):
    for j in range(1,C):
        dp[0][i][j]=max(dp[0][i-1][j],dp[0][i-1][j])
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:
                dp[r][i][j] = max(dp[r-1][i][j-1], dp[r-1][i][j-1]+rcv[i][j] )
print(max( dp[i][-1][-1] for i in range(0,4)) )

Submission status: Wrong Answer

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

item_dic={}
for _ in range(K):
  r,c,v=map(int,input().split())
  item_dic[(r,c)]=v
#print(item_dic)

dp=[[[[0]*4 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    for k in range(4):
      for l in range(4):
        if (i,j) in item_dic:
          if l>0:
            dp[i][j][1][l]=max(dp[i][j][1][l],dp[i-1][j][k][l-1]+item_dic[(i,j)])
          dp[i][j][0][l]=max(dp[i][j][0][l],dp[i-1][j][k][l])
          if k>0:
            dp[i][j][k][1]=max(dp[i][j][k][1],dp[i][j-1][k-1][l]+item_dic[(i,j)])
          dp[i][j][k][0]=max(dp[i][j][k][0],dp[i][j-1][k][l])
        else:
          dp[i][j][0][l]=max(dp[i][j][0][l],dp[i-1][j][k][l])
          dp[i][j][k][0]=max(dp[i][j][k][0],dp[i][j-1][k][l])
          
#print(dp[R][C])
answer=0
for i in range(4):
  for j in range(4):
    answer=max(answer,dp[R][C][i][j])
    
print(answer)

Submission status: Wrong Answer

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

import heapq
dp = [[0]*c for i in range(r)]
dp[0][0] = v[0][0]

get = [[] for i in range(r)]
if v[0][0]!=0:
    heapq.heappush(get[0],v[0][0])

for i in range(r):
    for j in range(c):
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            x = v[i][j]
            m = 0
            if len(get[i])==3:
                m = heapq.heappop(get[i])
                x = max(m,x)
            dp[i][j] = dp[i][j-1] + x - m
            if x!=0:heapq.heappush(get[i],x)
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i-1][j] + v[i][j]
            get[i] = []
            if v[i][j]!=0:
                heapq.heappush(get[i],v[i][j])

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

Submission status: Wrong Answer

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

import heapq
dp = [[0]*c for i in range(r)]
dp[0][0] = v[0][0]

get = [[] for i in range(r)]
if v[0][0]!=0:
    heapq.heappush(get[0],v[0][0])

for i in range(r):
    for j in range(c):
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            x = v[i][j]
            if len(get[i])==3:
                m = heapq.heappop(get[i])
                x = max(m,x) - m
            dp[i][j] += dp[i][j-1] + x
            if x!=0:heapq.heappush(get[i],x)
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i-1][j] + v[i][j]
            get[i] = []
            if v[i][j]!=0:
                heapq.heappush(get[i],v[i][j])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*C for _ in range(R)] for _ in range(4)]
for i in range(R):
    for k in range(C):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i][k]
        dp[0][i][k] = max(dp[0][i][k-1],tmp)
        dp[1][i][k] = dp[1][i][k-1]
        dp[2][i][k] = dp[2][i][k-1]
        dp[3][i][k] = dp[3][i][k-1]
        if target != 0:
            dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
            if dp[1][i][k-1] != 0:
                dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
            if dp[2][i][k-1] != 0:
                dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[2][-1][-1],dp[3][-1][-1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
from heapq import *

R,C,K,*f = map(int, open(0).read().split())
rcv = [f[i*3:i*3+3] for i in range(K)]
m = np.zeros((R,C),dtype=int)
for r,c,v in rcv:
    m[r-1][c-1] = v
dp = np.zeros((R,C),dtype=int)
for i in range(R):
    temp = []
    heapify(temp)
    for j in range(C):
        if i > 0 and j > 0:
            if dp[i-1][j] >= dp[i][j-1]:
                dp[i][j] = dp[i-1][j]
                temp = []
            else:
                dp[i][j] = dp[i][j-1]
        elif j > 0:
            dp[i][j] = dp[i][j-1]
        elif i > 0:
            dp[i][j] = dp[i-1][j]
        if m[i][j] > 0:
            if len(temp) < 3:
                heappush(temp,m[i][j])
                dp[i][j] += m[i][j]
            else:
                mi = heappop(temp)
                dp[i][j] -= mi
                ma = max(m[i][j],mi)
                dp[i][j] += ma
                heappush(temp,ma)
print(dp.max())

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    R, C, K = map(int, rl().split())
    rcv = [list(map(int, rl().split())) for _ in range(K)]
    
    items = defaultdict(int)
    for r, c, v in rcv:
        r, c = r - 1, c - 1
        items[(r, c)] = v
    
    dp = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    dp[0][0][1] = items[(0, 0)]
    
    for r in range(1, R):
        dp[r][0][0] = dp[r - 1][0][1]
        dp[r][0][1] += dp[r - 1][0][1]
    for c in range(1, C):
        dp[0][c][1] = max(dp[0][c - 1][1], items[(0, c)])
        dp[0][c][2] = max(dp[0][c - 1][2], dp[0][c - 1][1] + items[(0, c)])
        dp[0][c][3] = max(dp[0][c - 1][3], dp[0][c - 1][2] + items[(0, c)])
    
    for r in range(1, R):
        for c in range(1, C):
            dp[r][c][0] = max(max(dp[r - 1][c]), dp[r][c - 1][0])
            dp[r][c][1] = max(max(dp[r - 1][c]) + items[(r, c)], dp[r][c - 1][0] + items[(r, c)], dp[r][c - 1][1])
            dp[r][c][2] = max(dp[r][c - 1][2], dp[r][c - 1][1] + items[(r, c)])
            dp[r][c][3] = max(dp[r][c - 1][3], dp[r][c - 1][2] + items[(r, c)])
    print(max(dp[-1][-1]))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

import heapq
dp = [[0]*c for i in range(r)]
dp[0][0] = v[0][0]

get = [[] for i in range(r)]
if v[0][0]!=0:
    heapq.heappush(get[0],v[0][0])

for i in range(r):
    for j in range(c):
        if j>0 and dp[i][j-1]+v[i][j]>dp[i][j]:
            x = v[i][j]
            if len(get[i])==3:
                m = heapq.heappop(get[i])
                dp[i][j] -= m
                x = max(m,x)
            dp[i][j] += dp[i][j-1] + x
            if x!=0:heapq.heappush(get[i],x)
        if i>0 and dp[i-1][j]+v[i][j]>dp[i][j]:
            dp[i][j] = dp[i-1][j] + v[i][j]
            get[i] = []
            if v[i][j]!=0:
                heapq.heappush(get[i],v[i][j])
print(dp[r-1][c-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    R, C, K = map(int, input().split())
    item_map = [[0]*C for _ in range(R)]

    for _ in range(K):
        ir, ic, iv = map(int, input().split())
        item_map[ir-1][ic-1] = iv
    dp = []
    for row in range(R):
        ls = []
        for col in range(C):
            ls.append([0, 0, 0, 0])
        dp.append(ls)

    for row in range(R):
        for col in range(C):
            item = item_map[row][col]
            if row>=1 and col>=1:
                M = max(
                    dp[row-1][col][0],
                    dp[row-1][col][1],
                    dp[row-1][col][2],
                    dp[row-1][col][3],
                    dp[row][col-1][0],
                )
                dp[row][col][0] = M
                
                dp[row][col][1] = dp[row][col-1][1]
                dp[row][col][2] = dp[row][col-1][2]
                dp[row][col][3] = dp[row][col-1][3]
                if item:
                    dp[row][col][1] = max(
                        M + item,
                        dp[row][col-1][1]
                        )
                    dp[row][col][2] = max(
                        dp[row][col-1][1] + item,
                        dp[row][col-1][2]
                        )
                    dp[row][col][3] = max(
                        dp[row][col-1][2] + item,
                        dp[row][col-1][3]
                        )
            elif row == 0:
                dp[row][col][0] = dp[row][col-1][0]
                dp[row][col][1] = dp[row][col-1][1]
                dp[row][col][2] = dp[row][col-1][2]
                dp[row][col][3] = dp[row][col-1][3]
                if item:
                    dp[row][col][1] = max(dp[row][col-1][1], dp[row][col-1][0] + item)
                    dp[row][col][2] = max(dp[row][col-1][2], dp[row][col-1][1] + item)
                    dp[row][col][3] = max(dp[row][col-1][3], dp[row][col-1][2] + item)
                continue
            elif col == 0:
                dp[row][col][0] = max(
                    dp[row-1][col][0], dp[row-1][col][1], dp[row-1][col][2], dp[row-1][col][3])
                
                if item:
                    dp[row][col][1] = dp[row][col][0] + item
                continue
            elif row == 0 and col == 0:
                if item != 0:
                    dp[row][col][1] = item
                    continue
    print(max(dp[R-1][C-1]))
main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict


def read():
    R, C, K = map(int, input().strip().split())
    # アイテムの価値V[r][c]
    V = [defaultdict(int) for i in range(R+1)]
    for i in range(K):
        r, c, v = map(int, input().strip().split())
        V[r][c] = v
    return R, C, K, V


def solve(R, C, K, V):
    # dp[c][s]: c列目までにs個のアイテムを拾った時の、価値の最大値
    dp = [[0 for s in range(4)] for c in range(C+1)]
    # 次の行に移るときに使うキャッシュ
    tmp = [0 for c in range(C+1)]
    for r in range(1, R+1):
        dp[0][0] = tmp[1]
        for c in range(1, C):
            dp[c][0] = max(dp[c-1][0], tmp[c+1])
        dp[C][0] = dp[C-1][0]
        for c in range(1, C+1):
            for s in range(1, 4):
                dp[c][s] = max(dp[c-1][s], dp[c-1][s-1] + V[r][c])
        # 次の行へ移る
        for c in range(1, C+1):
            tmp[c] = max(dp[c])
        # dpを初期化
        for c in range(C+1):  
            for s in range(4):
                dp[c][s] = 0
    ans = max(tmp)
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))