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:
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]]
print(max(ans))

Submission status: Wrong Answer

Solution 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(3)]

for y in range(h):
    for x in range(w):
        for i in range(3):
            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 and i!=2:
                    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]]
print(max(ans))

Submission status: Wrong Answer

Solution 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(3)]

for y in range(h):
    for x in range(w):
        for i in range(3):
            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 and i!=2:
                    dp[i+1][y][x+1]=max(dp[i+1][y][x+1],dp[i][y][x] + item[y][x])

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

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

print(ans)

Submission status: Wrong Answer

Solution 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(3):
                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 and i!=2:
                    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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
r,c,k = list(map(int, input().split()))

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

F = {}

for _ in range(k):
  x,y,v = list(map(int, input().split()))
  x-=1
  y-=1
  F[(x,y)] = v
  
for i in range(r):
  for j in range(c):
    if (i,j) in F:
      val = F[(i,j)]
    else:
      val = 0

    dp[i][j][3] = max(dp[i][j-1][3], dp[i][j-1][2] + val)
    dp[i][j][2] = max(dp[i][j-1][2], dp[i][j-1][1] + val)
    dp[i][j][1] = max(dp[i][j-1][1], dp[i][j-1][0] + val)
    dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3],0)

best = 0

for j in range(c):
  for k in range(4):
    best = max(best, dp[-1][j][k])
    
print(best)

Submission status: Wrong Answer

Solution code:
r,c,k = list(map(int, input().split()))

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

F = {}

for _ in range(k):
  x,y,v = list(map(int, input().split()))
  x-=1
  y-=1
  F[(x,y)] = v
  
for i in range(r):
  for j in range(c):
    if (i,j) in F:
      val = F[(i,j)]
    else:
      val = 0

    dp[i][j][3] = max(dp[i][j-1][3], dp[i][j-1][2] + val)
    dp[i][j][2] = max(dp[i][j-1][2], dp[i][j-1][1] + val)
    dp[i][j][1] = max(dp[i][j-1][1], max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3]) + val)
    dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3],0)

best = 0

for j in range(c):
  for k in range(4):
    best = max(best, dp[-1][j][k])
    
print(best)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

for i in range(R):
	for j in range(C):
		if values[i][j] == 0:
			if j != 0:
				for k in range(4):
					dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1])
			if i != 0:
				for k in range(4):
					dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j])
		else:
			#取る
			if j != 0:
				for k in range(1, 4):
					dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i][j - 1] + values[i][j])
			if i != 0:
				for k in range(4):
					dp[1][i][j] = max(dp[1][i][j], dp[k][i - 1][j] + values[i][j])
			#取らん
			if j != 0:
				for k in range(4):
					dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1])
			if i != 0:
				for k in range(4):
					dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j])

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

Submission status: Wrong Answer

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

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

for i in range(R):
	for j in range(C):
		if values[i][j] == 0:
			if j != 0:
				for k in range(4):
					dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1])
			for k in range(4):
				dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j])
		else:
			#取る
			if j != 0:
				for k in range(1, 4):
					dp[k][i][j] = max(dp[k][i][j], dp[k - 1][i][j - 1] + values[i][j])
			for k in range(4):
				dp[1][i][j] = max(dp[1][i][j], dp[k][i - 1][j] + values[i][j])
			#取らん
			if j != 0:
				for k in range(4):
					dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1])
			for k in range(4):
				dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j])

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

Submission status: Wrong Answer

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

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

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

dp = [[[-1000 for c in range(C)] for r in range(R)] for k in range(4)]

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

for r in range(R):
    for c in range(C):
        if c >= 1:
            dp[0][r][c] = max(dp[0][r][c], dp[0][r][c-1])
            dp[1][r][c] = max(dp[1][r][c], dp[1][r][c-1])
            dp[2][r][c] = max(dp[2][r][c], dp[2][r][c-1])
            dp[3][r][c] = max(dp[3][r][c], dp[3][r][c-1])
        if r >= 1:
            dp[0][r][c] = max(dp[0][r][c], dp[0][r-1][c], dp[1][r-1][c], dp[2][r-1][c], dp[3][r-1][c])
        v = grid[r][c]
        if v > 0:
            if c >= 1:
                dp[1][r][c] = max(dp[1][r][c], dp[1][r][c-1] + v)
                dp[2][r][c] = max(dp[2][r][c], dp[2][r][c-1] + v)
                dp[3][r][c] = max(dp[3][r][c], dp[3][r][c-1] + v)
            if r >= 1:
                dp[1][r][c] = max(dp[1][r][c], dp[0][r-1][c]+v, dp[1][r-1][c]+v, dp[2][r-1][c]+v, dp[3][r-1][c]+v)

print(max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1], dp[3][R-1][C-1]))

Submission status: Wrong Answer

Solution code:
r,c,k=list(map(int,input().split()))
lic=[[0 for _ in range(c)]for _ in range(r)]

for i in range(k):
    ci=list(map(int,input().split()))
    lic[ci[0]-1][ci[1]-1]=ci[2]
print(*lic)

nowi,nowj,score=0,0,0
for i in range(r):
    score+=lic[i][nowj]
    print(i+1,nowj+1,"::",lic[i][nowj])
    for j in range(nowj,c):
        if j==nowj:
            continue
        score+=sum(lic[i][nowj:j])
        nowj=j
        print(i+1,j+1,":",lic[i][j])

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8, i8, i8[:]), cache=True)
def main(H, W, K, XYV):
    G = np.zeros((H + 1, W + 1), np.int64)
    for i in range(0, 3 * K, 3):
        x, y, v = XYV[i:i + 3]
        G[x - 1, y - 1] = v
    dp = np.zeros((W + 1, 4), np.int64)
    for h in range(H):
        newdp = np.zeros((W + 1, 4), np.int64)
        for n in range(4):
            newdp[:, 0] = np.maximum(newdp[:, 0], dp[:, n])
        dp = newdp
        for w in range(W):
            for n in range(2, -1, -1):
                # ひろう
                dp[w, n + 1] = max(dp[w, n + 1], dp[w, n] + G[h, w])
            for n in range(4):
                dp[w + 1, n] = max(dp[w + 1, n], dp[w, n])
    return dp.max()

H, W, K = map(int, readline().split())
XYV = np.array(read().split(), np.int64)

# print(main(H, W, K, XYV))

Submission status: Wrong Answer

Solution code:
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

@njit((i8, i8, i8, i8[:,:]), cache=True)
def solve(R,C,K,item):
  dp = np.zeros((C+1,4), dtype=np.int64)
  for i in range(1,R+1):
    new_dp = np.zeros((C+1,4), dtype=np.int64)
    #上から移動
    for k in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,k])
    dp = new_dp
    for j in range(1,C+1):
      #横から移動
      new_dp[j] = np.maximum(new_dp[j],new_dp[j-1])
      #アイテムを取る
      for k in range(2,-1,-1):
        dp[j,k+1] = np.maximum(dp[j,k+1],dp[j,k]+item[i-1][j-1])
  ans = dp[-1].max()
  return ans

R, C, K = map(int, input().split())
item = np.zeros((R,C), dtype=np.int64)
for i in range(K):
  r,c,v = map(int, input().split())
  item[r-1,c-1] = v
# print(solve(R,C,K,item))

Submission status: Wrong Answer

Solution code:
#E_Picking Goods
R,C,K = map(int,input().split())
A =  [[0 for j in range(C+1)] for i in range(R+1)]
#vi = [[[-1 for j in range(C+1)] for i in range(R+1)] for k in range(C+1)]
vi = [[0 for j in range(C+1)] for i in range(R+1)]

for inputk in range(K):
    r,c,v = map(int,input().split())
    A[r][c] = v
#print(A)

i = 0
j = 0

#for i in range(0,R+1):
#    vi[i][0] = 0
#for j in range(0,C+1):
#    vi[0][j] = 0    
vi[1][1] = A[1][1]

for i in range(1,R+1):
    rowgoodscount = 0
    min1 = 1000000010
    min2 = 1000000010
    min3 = 1000000010
    for j in range(1,C+1):
        if vi[i][j-1] > vi[i-1][j]:
            vi[i][j] = vi[i][j-1]
        else:
            vi[i][j] = vi[i-1][j]
        if A[i][j] > 0:
            vi[i][j] += A[i][j]
            if min1 >  A[i][j]:
                min3 = min2
                min2 = min1
                min1 = A[i][j]
            else:
                if min2 >  A[i][j]:
                    min3 = min2
                    min2 = A[i][j]
                else:
                    if min3 >  A[i][j]:
                        min3 = A[i][j]
            rowgoodscount += 1
            if rowgoodscount > 3:
                vi[i][j] -= min1   
print(vi[R][C])

Submission status: Wrong Answer

Solution code:
#E_Picking Goods
R,C,K = map(int,input().split())
A =  [[0 for j in range(C+1)] for i in range(R+1)]
#vi = [[[-1 for j in range(C+1)] for i in range(R+1)] for k in range(C+1)]
vi = [[0 for j in range(C+1)] for i in range(R+1)]

for inputk in range(K):
    r,c,v = map(int,input().split())
    A[r][c] = v
#print(A)

i = 0
j = 0

#for i in range(0,R+1):
#    vi[i][0] = 0
#for j in range(0,C+1):
#    vi[0][j] = 0    
vi[1][1] = A[1][1]

for i in range(1,R+1):
    rowgoodscount = 0
    min = 1000000010  
    for j in range(1,C+1):
        if vi[i][j-1] > vi[i-1][j]:
            vi[i][j] = vi[i][j-1]
        else:
            vi[i][j] = vi[i-1][j]
        if A[i][j] > 0:
            vi[i][j] += A[i][j]
            if min >  A[i][j]:
                min = A[i][j]
            rowgoodscount += 1
            if rowgoodscount > 3:
                vi[i][j] -= min   
print(vi[R][C])

Submission status: Wrong Answer

Solution code:
#E_Picking Goods
R,C,K = map(int,input().split())
A =  [[0 for j in range(C+1)] for i in range(R+1)]
#vi = [[[-1 for j in range(C+1)] for i in range(R+1)] for k in range(C+1)]
vi = [[-1 for j in range(C+1)] for i in range(R+1)]

for inputk in range(K):
    r,c,v = map(int,input().split())
    A[r][c] = v
#print(A)

i = 0
j = 0

for i in range(0,R+1):
    vi[i][0] = 0
for j in range(0,C+1):
    vi[0][j] = 0    
vi[1][1] = A[1][1]

for i in range(1,R+1):
    rowgoodscount = 0
    min = 1000000010  
    for j in range(1,C+1):
        if vi[i][j-1] > vi[i-1][j]:
            vi[i][j] = vi[i][j-1]
        else:
            vi[i][j] = vi[i-1][j]
        if A[i][j] > 0:
            vi[i][j] += A[i][j]
            if min >  A[i][j]:
                min = A[i][j]
            rowgoodscount += 1
            if rowgoodscount > 3:
                vi[i][j] -= min   
print(vi[R][C])

Submission status: Wrong Answer

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

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

dp = [[[0 for k in range(4)]
       for j in range(C+1)] for i in range(R+1)]
for i in range(R):
    for j in range(C):
        for k in range(3):
            if k <= 2:
                dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k]+I[i][j])
                dp[i][j+1][k+1] = max(dp[i][j+1][k+1], dp[i][j][k]+I[i][j])
            else:
                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])


# print(*dp, sep='\n')

ans = 0
for k in range(4):
    if k <= 2:
        ans = max(ans, dp[R-1][C-1][k]+I[R-1][C-1])
    else:
        ans = max(ans, dp[R-1][C-1][k])
print(ans)

Submission status: Wrong Answer

Solution code:
"""
dp[i][j]　=> 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)]

for i in range(R):
    memo = [0,0,0]
    for j in range(C):
        v = items[i][j]

        if i==0 and j == 0:
            dp[0][0] = v
            memo[2] = v
        
        if j != 0:
            #左から入る場合の価値を求める。
            dp[i][j] = dp[i][j-1]
            memo.sort()
            if v > memo[0]:
                dp[i][j] -= memo[0]
                dp[i][j] += v
                memo[0] = v
        if i != 0:
            #上から入る場合の価値を求める。
            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; input = sys.stdin.readline
n, m, k = map(int, input().split())
matrix = [[0 for _ in range(m)] for _ in range(n)]
dp = [[[0 for _ in range(m)] for _ in range(n)] for i in range(3)]
for i in range(k):
	a, b, c = map(int, input().split())
	matrix[a-1][b-1] = c
dp[0][n-1][m-1] = matrix[n-1][m-1]
for i in range(n-2, -1, -1): dp[1][i][m-1] = dp[2][i][m-1] = dp[0][i][m-1] = dp[0][i+1][m-1]+matrix[i][m-1]
if m-2>=0: dp[1][n-1][m-2] = matrix[n-1][m-1]+matrix[n-1][m-2]
if m-3>=0: dp[2][n-1][m-3] = matrix[n-1][m-1]+matrix[n-1][m-2]+matrix[n-1][m-3]
for i in range(m-1, -1, -1): dp[0][n-1][i] = matrix[n-1][i]
for i in range(m-4, -1, -1): 
	dp[1][n-1][i] = max(dp[1][n-1][i+1], dp[0][n-1][i+1]+matrix[n-1][i])
	dp[2][n-1][i] = max(dp[2][n-1][i+1], dp[1][n-1][i+1]+matrix[n-1][i])

for i in range(n-2, -1, -1):
	for j in range(m-2, -1 ,-1):
		dp[0][i][j] = matrix[i][j] + max(dp[0][i+1][j], dp[1][i+1][j], dp[2][i+1][j])
		dp[1][i][j] = max(dp[1][i][j+1], dp[0][i][j+1]+matrix[i][j])
		dp[2][i][j] = max(dp[2][i][j+1], dp[1][i][j+1]+matrix[i][j])
print(max(dp[0][0][0], dp[1][0][0], dp[2][0][0]))

Submission status: Wrong Answer

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

n, m, k = map(int, input().split())
matrix = [[0 for _ in range(m)] for _ in range(n)]
dp = [[0 for _ in range(m)] for _ in range(n)]
for i in range(k):
	a, b, c = map(int, input().split())
	matrix[a-1][b-1] = c

top = [[(0, 0, 0) for _ in range(m)] for _ in range(n)]
for i in range(n):
	m1, m2, m3 = 0, 0, 0
	for j in range(m):
		if matrix[i][j] > m1:
			m3 = m2
			m2 = m1
			m1 = matrix[i][j]
		elif matrix[i][j] > m2:
			m3 = m2
			m2 = matrix[i][j]
		elif matrix[i][j] > m3: m3 = matrix[i][j]
		top[i][j] = (m1, m2, m3)

dp[0][0] = matrix[0][0]
for i in range(1, n): dp[i][0] = dp[i-1][0] + matrix[i][0]
for i in range(1, m): dp[0][i] = sum(top[0][i])
for i in range(1, n):
	for j in range(1, m):
		dp[i][j] = max(dp[i-1][j] + matrix[i][j], dp[i][j-1] + sum(top[i][j])-sum(top[i][j-1]))
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
dp=[[[0]*4 for _ in range(w+1)] for _ in range(h+1)]
v=[[0]*(w+1) for _ in range(h+1)]
for _ in range(n):
  r,c,f=map(int,input().split())
  v[r][c]=f
for i in range(1,h+1):
  for x in 0,1,2:
    for j in range(1,w+1):
      dp[i][j][x+1]=max(dp[i][j][x],max(dp[i][j-1][x],(x<1)*dp[i-1][j][3])+v[i][j],dp[i-1][j][x+1])
print(dp[-1][-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

items = defaultdict(int)
for k in range(K):
  r,c,v=map(int,input().split())
  items[(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 i == 0 and j > 0:
      dp[i][j] = dp[i][j-1]
    elif j == 0 and i > 0:
      dp[i][j]= [max(dp[i-1][j])]*4
    elif i > 0 and j > 0:
      dp[i][j] = [max(dp[i][j-1][k],max(dp[i-1][j])) for k in range(4)]
    for k in range(3,0,-1):
      dp[i][j][k] = max(dp[i][j][k],dp[i][j][k-1]+items[(i,j)])

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

Submission status: Wrong Answer

Solution 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()

Submission status: Wrong Answer

Solution code:
# R, C: Y, H
H, W, K = map(int, input().split())
RCV = [list(map(int, input().split())) for _ in range(K)]
# H, W, K = 3000, 3000, 2*10**5
# RCV = [[i//W, i%W, i] for i in range(K)]

# print("pre")

item = [[0]*(W+1) for _ in range(H+1)]
for r, c, v in RCV:
    item[r][c] = v
# for item_i in item:
#     print(item_i)

dp = [[[0]*4 for _ in range(W+1)] for _ in range(H+1)]

# 3000**2*4*2 + 2*10**5*2*4
for y in range(1, H+1):
    for x in range(1, W+1):
        for k in range(4):
            dp[y][x][k] = dp[y][x-1][k]
        for k in range(4):
            dp[y][x][0] = max(
                dp[y-1][x][k],
                dp[y][x][0]
            )
        if item[y][x] == 0:
            continue
        for k in range(1, 4):
            dp[y][x][k] = max(
                dp[y][x-1][k-1] + item[y][x],
                dp[y][x][k]
            )
        for k in range(1, 4):
            dp[y][x][0] = max(
                dp[y-1][x][k-1] + item[y][x],
                dp[y][x][0]
            )
# 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())
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)