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: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
def main():
    R, C, K = map(int, input().split())
    items = [[0 for _ in range(C)] for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        items[r - 1][c - 1] = v
    dp = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(4)]
    dp[1][0][0] = items[0][0]
    for r in range(R):
        for c in range(C):
            for i in range(4):
                tmp = dp[i][r][c]
                # right
                if c != C - 1:
                    # unpick
                    dp[i][r][c + 1] = max(dp[i][r][c+1], tmp)
                    # pick
                    if i < 3:
                        dp[i+1][r][c + 1] = max(dp[i+1][r]
                                                [c + 1], tmp + items[r][c + 1])
                # down
                if r != R - 1:
                    # unpick
                    dp[i][r + 1][c] = max(dp[i][r + 1][c], tmp)
                    # pick
                    dp[1][r + 1][c] = max(dp[1][r + 1][c],
                                          tmp + items[r + 1][c])
    ans = 0
    for i in range(4):
        ans = max(ans, dp[i][-1][-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    R, C, K = map(int, input().split())
    items = [[0 for _ in range(C)] for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        items[r - 1][c - 1] = v
    dp = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(4)]
    dp[1][0][0] = items[0][0]
    for r in range(R):
        for c in range(C):
            for i in range(4):
                tmp = dp[i][r][c]
                # right
                if c != C - 1:
                    # unpick
                    dp[i][r][c + 1] = max(dp[i][r][c+1], tmp)
                    # pick
                    if i < 3:
                        dp[i+1][r][c + 1] = max(dp[i+1][r]
                                                [c + 1], tmp + items[r][c + 1])
                # down
                if r != R - 1:
                    # unpick
                    dp[i][r + 1][c] = max(dp[i][r + 1][c], tmp)
                    # pick
                    dp[1][r + 1][c] = max(dp[1][r + 1][c],
                                          tmp + items[r + 1][c])
    ans = 0
    for i in range(4):
        ans = max(ans, dp[i][-1][-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    R, C, K = map(int, input().split())
    items = [[0 for _ in range(C)] for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        items[r - 1][c - 1] = 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(R):
        for c in range(C):
            for i in range(4):
                tmp = dp[r][c][i]
                # right
                if c != C - 1:
                    # unpick
                    dp[r][c + 1][i] = max(dp[r][c + 1][i], tmp)
                    # pick
                    if i < 3:
                        dp[r][c + 1][i +
                                     1] = max(dp[r][c + 1][i + 1], tmp + items[r][c + 1])
                # down
                if r != R - 1:
                    # unpick
                    dp[r + 1][c][i] = max(dp[r + 1][c][i], tmp)
                    # pick
                    dp[r + 1][c][1] = max(dp[r + 1][c][1], tmp + items[r + 1][c])
    ans = 0
    for i in range(4):
        ans = max(ans, dp[-1][-1][i])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W,K = map(int,input().split())
A = [[0 for _ in range(W+1)] for _ in range(H+1)]
for _ in range(K):
    r,c,v = map(int,input().split())
    A[r][c]=v
dp = [[[0 for _ in range(4)] for _ in range(W+1)] for _ in range(H+1)]
dp[H][W][0] = A[H][W]
dp[H][W][1] = A[H][W]
dp[H][W][2] = A[H][W]
for j in range(W-1,0,-1):
    dp[H][j][3] = dp[H][j+1][3]
    for k in range(3):
        dp[H][j][k] = max(dp[H][j+1][k],dp[H][j+1][k+1]+A[H][j])
for i in range(H-1,0,-1):
    dp[i][W][3] = dp[i+1][W][0]
    for k in range(3):
        dp[i][W][k] = dp[i+1][W][0]+A[i][W]
for i in range(H-1,0,-1):
    for j in range(W-1,0,-1):
        dp[i][j][3] = max(dp[i][j+1][3],dp[i+1][j][0])
        for k in range(3):
            dp[i][j][k] = max(dp[i][j+1][k],dp[i][j+1][k+1]+A[i][j],dp[i+1][j][0]+A[i][j])
print(dp[1][1][0])

Submission status: Time Limit Exceeded

Solution code:
H,W,K = map(int,input().split())
A = [[0 for _ in range(W+1)] for _ in range(H+1)]
for _ in range(K):
    r,c,v = map(int,input().split())
    A[r][c]=v
dp = [[[0 for _ in range(4)] for _ in range(W+1)] for _ in range(H+1)]
if A[H][W]>0:
    dp[H][W][0] = A[H][W]
    dp[H][W][1] = A[H][W]
    dp[H][W][2] = A[H][W]
for j in range(W-1,0,-1):
    dp[H][j][3] = dp[H][j+1][3]
    for k in range(3):
        dp[H][j][k] = max(dp[H][j+1][k],dp[H][j+1][k+1]+A[H][j])
for i in range(H-1,0,-1):
    dp[i][W][3] = dp[i+1][W][0]
    for k in range(3):
        dp[i][W][k] = dp[i+1][W][0]+A[i][W]
for i in range(H-1,0,-1):
    for j in range(W-1,0,-1):
        dp[i][j][3] = max(dp[i][j+1][3],dp[i+1][j][0])
        for k in range(3):
            dp[i][j][k] = max(dp[i][j+1][k],dp[i][j+1][k+1]+A[i][j],dp[i+1][j][0]+A[i][j])
print(dp[1][1][0])

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
f=[[-1]*R for i in range(C)]
dp=[[[0]*4 for i in range(R)] for j in range(C)]

for i in range(K):
  r,c,v=map(int,input().split())
  r-=1
  c-=1
  f[c][r]=v
dp[0][0][1]=f[0][0]
for i in range(R):
  for j in range(C):
    for k in range(4):
      if j-1>=0 and k-1>=0:
        dp[j][i][k]=max(dp[j-1][i][k-1]+f[j][i],dp[j][i][k],dp[j-1][i][k])
      if i-1>=0:
        if k==0:
          dp[j][i][k]=max(max(dp[j][i-1]),dp[j][i][k])
        elif k==1:
          dp[j][i][k]=max(dp[j][i][k-1]+f[j][i],dp[j][i][k])
print(max(dp[C-1][R-1]))

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(2)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[1][j][0] = max(dp[0][j])
        dp[1][j][1] = dp[1][j][0] + bord[i][j]
        for x in range(1, 4):
            dp[1][j][x] = max(
                dp[1][j][x],
                dp[1][j-1][x],
                dp[1][j-1][x-1]+bord[i][j],             
            )
    dp[0] = dp[1][::]

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

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(2)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[1][j][0] = max(dp[0][j])
        dp[1][j][1] = max(
            dp[1][j][0] + bord[i][j],
            dp[1][j-1][1],
            dp[1][j-1][0]+bord[i][j]
        )

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

        dp[0] = dp[1][::]

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

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(2)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[1][j][0] = max(dp[0][j])
        dp[1][j][1] = max(
            dp[1][j][0] + bord[i][j],
            dp[1][j-1][1],
            dp[1][j-1][0]+bord[i][j]
        )
        for x in range(2, 4):
            dp[1][j][x] = max(
                dp[1][j-1][x],
                dp[1][j-1][x-1]+bord[i][j],             
            )
        dp[0] = dp[1][::]

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

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(2)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[1][j][0] = max(dp[0][j])
        dp[1][j][1] = dp[1][j][0] + bord[i][j]
        for x in range(1, 4):
            dp[1][j][x] = max(
                dp[1][j][x],
                dp[1][j-1][x],
                dp[1][j-1][x-1]+bord[i][j],             
            )
        dp[0] = dp[1][::]

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

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(r)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[i][j][0] = max(dp[i-1][j])
        dp[i][j][1] = dp[i][j][0] + bord[i][j]
        for x in range(1, 4):
            dp[i][j][x] = max(
                dp[i][j][x],
                dp[i][j-1][x],
                dp[i][j-1][x-1]+bord[i][j],             
            )

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

Submission status: Time Limit Exceeded

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

dp = [[[0,0,0,0] for _j in range(c+1)] for _i in range(r)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        y = max(dp[i-1][j])
        dp[i][j][0] = y
        dp[i][j][1] = y + bord[i][j]
        for x in range(1, 4):
            dp[i][j][x] = max(
                dp[i][j][x],
                dp[i][j-1][x],
                dp[i][j-1][x-1]+bord[i][j],             
            )

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

for i in range(K):
  r, c, v = map(int, input().split())
  score[r-1][c-1]+=v
  
ans = [[[0]*4 for i in range(C)] for j in range(R)]
ans[0][0][1]=score[0][0]
ans[0][0][2]=score[0][0]
ans[0][0][3]=score[0][0]

for i in range(R):
  for j in range(C):
    if i>0:
      ans[i][j][0]=max(ans[i][j][0],ans[i-1][j][3])
      ans[i][j][1]=max(ans[i][j][1],ans[i-1][j][3]+score[i][j])
      ans[i][j][2]=max(ans[i][j][2],ans[i-1][j][3]+score[i][j])
      ans[i][j][3]=max(ans[i][j][3],ans[i-1][j][3]+score[i][j])
    if j>0:
      ans[i][j][0]=max(ans[i][j][0],ans[i][j-1][0])
      ans[i][j][1]=max(ans[i][j][1],ans[i][j-1][0]+score[i][j],ans[i][j-1][1])
      ans[i][j][2]=max(ans[i][j][2],ans[i][j-1][1]+score[i][j],ans[i][j-1][2])
      ans[i][j][3]=max(ans[i][j][3],ans[i][j-1][2]+score[i][j],ans[i][j-1][3])

print(ans[R-1][C-1][3])

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
dp=[[[0] * R for i in range(C)] for j in range(4)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp[1][0][0]=l[0][0]
for x in range(1,R+C-1):
  for y in range(R):
    i=y
    j=x-y
    if j<0 or C<=j:
      continue
    for k in range(4):
      A=0
      if 0<j:
        A=max(A,dp[k][j-1][i])
        if 0<k:
          A=max(A,dp[k-1][j-1][i]+l[i][j])
      if 0<i and k<2:
        for q in range(4):
          A=max(A,dp[q][j][i-1]+k*l[i][j])
      dp[k][j][i]=A
X=dp[3][C-1][R-1]
for k in range(3):
  X=max(X,dp[k][C-1][R-1])
print(X)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
r,c,k=map(int, input().split())
v={}

for _ in range(k):
    ri,ci,vi=map(int, input().split())
    v[(ri-1,ci-1)]=vi
    
dp=[[0]*4 for _ in range(c+1)]
for j in range(c):
    if (0,j) in v:
        for t in range(1,4):
            dp[j][t]=max(dp[j-1][t-1]+v[(0,j)],dp[j-1][t])
    else:
        for t in range(1,4):
            dp[j][t]=dp[j-1][t]
import copy
for i in range(1,r):
    dp2=[[dp[x][3] for _ in range(4)] if (i,x) not in v else [dp[x][3]]+[dp[x][3]+v[(i,x)] for _ in range(3)] for x in range(c+1)]
    for j in range(c):
        if (i,j) in v:
            for t in range(1,4):
                dp2[j][t]=max(dp2[j-1][t-1]+v[(i,j)],dp2[j-1][t],dp2[j][t])
        else:
            for t in range(1,4):
                dp2[j][t]=max(dp2[j-1][t],dp2[j][t])
    dp=dp2

print(max(dp[-2]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    r, c, k = map(int, input().split())
    d = [[0]*c for _ in [0]*r]
    for _ in range(k):
        t1, t2, t3 = map(int, input().split())
        d[t1-1][t2-1] = t3
    dp = [[0]*4 for _ in [0]*c]
    dp[0][1] = d[0][0]
    for x in range(r):
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        dp[y+1][z+1] = max(dp[y+1]
                                           [z+1], dp[y][z]+d[x][y+1])
                    dp[y+1][z] = max(dp[y+1][z], dp[y][z])
            if x != r-1:
                dp2[y][1] = max(dp2[y][1],
                                max(dp[y])+d[x+1][y])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        if x != r-1:
            dp = dp2
    print(max(dp[c-1]))


main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

rcvs = rcvs

def main():
    for i in range(len(rcvs)):
        rcvs[i][0] -= 1
        rcvs[i][1] -= 1
    
    rewards = [[0 for _ in range(C)] for _ in range(R)]
    
    
    for rcv in rcvs:
        rewards[rcv[0]][rcv[1]] = rcv[2]
    
    dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
    answer = 0
    
    for i in range(R):
        for j in range(C):
            reward = rewards[i][j]
            dp[i+1][j+1][0] = max(dp[i][j+1])
            dp[i+1][j+1][1] = dp[i+1][j+1][0] + reward
            
            for k in range(4): # move downside without selecting
                candidate = dp[i+1][j][k]
                dp[i+1][j+1][k] = max(dp[i+1][j][k], dp[i+1][j+1][k])
            if reward > 0:
                for k in range(3):
                    dp[i+1][j+1][k+1] = max(dp[i+1][j][k]+reward,dp[i+1][j+1][k+1])
    print(max(dp[-1][-1]))

main()

Submission status: Time Limit Exceeded

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

R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

rcvs = rcvs


for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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


for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        dp[i+1][j+1][0] = max(dp[i][j+1])
        dp[i+1][j+1][1] = dp[i+1][j+1][0] + reward
        
        for k in range(4): # move downside without selecting
            candidate = dp[i+1][j][k]
            dp[i+1][j+1][k] = max(dp[i+1][j][k], dp[i+1][j+1][k])
        if reward > 0:
            for k in range(3):
                dp[i+1][j+1][k+1] = max(dp[i+1][j][k]+reward,dp[i+1][j+1][k+1])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:

R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

rcvs = rcvs

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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


for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        dp[i+1][j+1][0] = max(dp[i][j+1])
        dp[i+1][j+1][1] = dp[i+1][j+1][0] + reward
        
        for k in range(4): # move downside without selecting
            candidate = dp[i+1][j][k]
            dp[i+1][j+1][k] = max(dp[i+1][j][k], dp[i+1][j+1][k])
        if reward > 0:
            for k in range(3):
                dp[i+1][j+1][k+1] = max(dp[i+1][j][k]+reward,dp[i+1][j+1][k+1])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

rcvs = np.array(rcvs, dtype = "int16")

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

rewards = np.array([[0 for _ in range(C)] for _ in range(R)], dtype="int16")


for rcv in rcvs:
    rewards[rcv[0],rcv[1]] = rcv[2]

dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
dp = np.array(dp)
answer = 0

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        dp[i+1,j+1,0] = max(dp[i,j+1])
        dp[i+1,j+1,1] = dp[i+1,j+1,0] + reward
        
        for k in range(4): # move downside without selecting
            candidate = dp[i+1,j,k]
            dp[i+1,j+1,k] = max(dp[i+1,j,k], dp[i+1,j+1,k])
        for k in range(3):
            dp[i+1,j+1,k+1] = max(dp[i+1,j,k]+reward,dp[i+1,j+1,k+1])
print(max(dp[-1,-1]))

Submission status: Time Limit Exceeded

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

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        dp[i+1][j+1][0] = max(dp[i][j+1])
        dp[i+1][j+1][1] = dp[i+1][j+1][0] + reward
        
        for k in range(4): # move downside without selecting
            candidate = dp[i+1][j][k]
            dp[i+1][j+1][k] = max(dp[i+1][j][k], dp[i+1][j+1][k])
        for k in range(3):
            dp[i+1][j+1][k+1] = max(dp[i+1][j][k]+reward,dp[i+1][j+1][k+1])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        
        dp[i+1][j+1][0] = max(dp[i][j+1])
        for k in range(4): # move downside without selecting
            if dp[i][j+1][k] + reward > dp[i+1][j+1][1]:
                dp[i+1][j+1][1] = dp[i][j+1][k] + reward
                
        for k in range(4): # move downside without selecting
            if dp[i+1][j][k] > dp[i+1][j+1][k]:
                dp[i+1][j+1][k] = dp[i+1][j][k]
            if k <= 2:
                if dp[i+1][j][k] + reward > dp[i+1][j+1][k+1]:
                    dp[i+1][j+1][k+1] = dp[i+1][j][k] + reward
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        for k in range(4): # move downside without selecting
            if dp[i][j+1][k] > dp[i+1][j+1][0]:
                dp[i+1][j+1][0] = dp[i][j+1][k]
                
            if dp[i][j+1][k] + reward > dp[i+1][j+1][1]:
                dp[i+1][j+1][1] = dp[i][j+1][k] + reward
                
        for k in range(4): # move downside without selecting
            if dp[i+1][j][k] > dp[i+1][j+1][k]:
                dp[i+1][j+1][k] = dp[i+1][j][k]
            if k <= 2:
                if dp[i+1][j][k] + reward > dp[i+1][j+1][k+1]:
                    dp[i+1][j+1][k+1] = dp[i+1][j][k] + reward
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        for k in range(4): # move downside without selecting
            if dp[i][j+1][k] > dp[i+1][j+1][0]:
                dp[i+1][j+1][0] = dp[i][j+1][k]
            if dp[i][j+1][k] + reward > dp[i+1][j+1][1]:
                dp[i+1][j+1][1] = dp[i][j+1][k] + reward
        for k in range(4): # move downside without selecting
            if dp[i+1][j][k] > dp[i+1][j+1][k]:
                dp[i+1][j+1][k] = dp[i+1][j][k]
            if k <= 2:
                if dp[i+1][j][k] + reward > dp[i+1][j+1][k+1]:
                    dp[i+1][j+1][k+1] = dp[i+1][j][k] + reward

answer = 0
for i in range(R):
    for j in range(C):
        for k in range(4):
            if answer < dp[i+1][j+1][k]:
                answer = dp[i+1][j+1][k]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
r, c, k = map(int, input().split())
value = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
    a, b, v = map(int, input().split())
    value[a][b] = v

dp = [[[0] * 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):
        dp[i][j][0] = dp[i-1][j][3]
        if value[i][j] != 0:
            dp[i][j][1] = max(
                dp[i][j][0] + value[i][j],
                dp[i][j-1][0] + value[i][j],
                dp[i][j-1][1]
            )
            for y in range(1, 3):
                dp[i][j][y+1] = max(
                    dp[i][j][y],
                    dp[i][j - 1][y] + value[i][j],
                    dp[i][j-1][y+1]
                )
        else:
            for y in range(3):
                dp[i][j][y + 1] = max(
                    dp[i][j][y],
                    dp[i][j - 1][y+1]
                )

#print(dp)
print(dp[r][c][3])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

R,C,K=map(int,input().split())
V=np.zeros((R,C),dtype=np.int64)

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

dp=np.zeros((C+1,4),dtype=np.int64)
for i in range(R):
    ndp=np.zeros((C+1,4),dtype=np.int64)
    for j in range(C):
        ndp[j][0]=max(dp[j])

    for j in range(C):
        v=V[i][j]
        for n in range(1,4):
            ndp[j][n]=max(ndp[j-1][n-1]+v,dp[j][3]+v,ndp[j-1][n])

    dp=ndp

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def solve():
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    for i in range(R):
        for j in range(C):
            for k in range(2, -1, -1):
                dp[i][j][k + 1] = max(
                    dp[i][j][k + 1], dp[i][j][k] + items.get((i, j), 0)
                )
            for k in range(4):
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k])
                dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
    return dp[R][C - 1][0]


R, C, K, *rcv = map(int, open(0).read().split())
items = {(r - 1, c - 1): v for r, c, v in zip(*[iter(rcv)] * 3)}
print(solve())

Submission status: Time Limit Exceeded

Solution code:
R,C,K = map(int,input().split())
G = {}
for _ in range(K):
    r,c,v = map(int,input().split())
    G[(r,c)] = v
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
if (1,1) in G:
    dp[1][1][1] = G[(1,1)]
for j in range(2,C+1):
    dp[1][j][0] = dp[1][j-1][0]
    if (1,j) in G:
        dp[1][j][1] = max(dp[1][j-1][0]+G[(1,j)],dp[1][j-1][1])
        dp[1][j][2] = max(dp[1][j-1][1]+G[(1,j)],dp[1][j-1][2])
        dp[1][j][3] = max(dp[1][j-1][2]+G[(1,j)],dp[1][j-1][3])
    else:
        dp[1][j][1] = dp[1][j-1][1]
        dp[1][j][2] = dp[1][j-1][2]
        dp[1][j][3] = dp[1][j-1][3]
for i in range(2,R+1):
    for j in range(1,C+1):
        if j-1>=1:
            dp[i][j][0] = dp[i][j-1][0]
        for k in range(4):
            dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][k])
        if (i,j) in G:
            if j-1>=1:
                dp[i][j][1] = max(dp[i][j-1][1],dp[i][j-1][0]+G[(i,j)])
                dp[i][j][2] = max(dp[i][j-1][2],dp[i][j-1][1]+G[(i,j)])
                dp[i][j][3] = max(dp[i][j-1][3],dp[i][j-1][2]+G[(i,j)])
            for k in range(4):
                dp[i][j][1] = max(dp[i][j][1],dp[i-1][j][k]+G[(i,j)])
        elif j-1>=1:
            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][0],dp[R][C][1],dp[R][C][2],dp[R][C][3]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import defaultdict

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

def index(i,j):
  return i*C+j

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

dp = np.zeros([C,4],dtype=int)

for i in range(R):
  if i > 0:
    prev = dp
  for j in range(C):
    if i == 0 and j > 0:
      dp[j,:] = dp[j-1,:]
    elif j == 0 and i > 0:
      dp[j,:] = prev[j,:].max() #reset
    elif i > 0 and j > 0:
      tmp = prev[j,:].max()
      dp[j,:] = np.maximum(dp[j-1,:],tmp) #reset
    dp[j,1:] = np.maximum(dp[j,1:],dp[j,:-1]+items[index(i,j)])

print(dp[-1,:].max())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import defaultdict

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

def index(i,j):
  return i*C+j

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

dp = np.zeros([C,4],dtype=int)

for i in range(R):
  if i > 0:
    prev = dp
  for j in range(C):
    if i == 0 and j > 0:
      dp[j,:] = dp[j-1,:]
    elif j == 0 and i > 0:
      dp[j,:] = prev[j,:].max() #reset
    elif i > 0 and j > 0:
      dp[j,:] = np.maximum(dp[j-1,:],prev[j,:].max()) #reset
    dp[j,1:] = np.maximum(dp[j,1:],dp[j,:-1]+items[index(i,j)])

print(dp[-1,:].max())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 = np.zeros([C,4],dtype=int)

for i in range(R):
  if i > 0:
    prev = dp.copy()
  for j in range(C):
    if i == 0 and j > 0:
      dp[j,:] = dp[j-1,:]
    elif j == 0 and i > 0:
      dp[j,:] = prev[j,:].max() #reset
    elif i > 0 and j > 0:
      dp[j,:] = np.maximum(dp[j-1,:],prev[j,:].max()) #reset
    dp[j,1:] = np.maximum(dp[j,1:],dp[j,:-1]+items[(i,j)])

print(dp[-1,:].max())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import defaultdict
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

#sys.setrecursionlimit(10**8)

R,C,K=map(int,readline().split())
m = map(int, read().split())
I = zip(m, m, m) 

items = defaultdict(int)
for r,c,v in I:
  items[(r-1,c-1)] = v

def solve(R,C):
  dp = np.zeros([4,R,C],dtype=int)

  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] = dp[:,i-1,j].max() #reset
      elif i > 0 and j > 0:
        dp[:,i,j] = np.maximum(dp[:,i,j-1],dp[:,i-1,j].max()) #reset
      if items[(i,j)]:
        dp[1:,i,j] = np.maximum(dp[1:,i,j],dp[:-1,i,j]+items[(i,j)])

  print(dp[:,-1,-1].max())
  
if __name__=="__main__":
  solve(R,C)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import defaultdict
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

#sys.setrecursionlimit(10**8)

R,C,K=map(int,readline().split())
m = map(int, read().split())
I = zip(m, m, m) 

items = defaultdict(int)
for r,c,v in I:
  items[(r-1,c-1)] = v

dp = np.zeros([4,R,C],dtype=int)

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] = dp[:,i-1,j].max() #reset
    elif i > 0 and j > 0:
      dp[:,i,j] = np.maximum(dp[:,i,j-1],dp[:,i-1,j].max()) #reset
    if items[(i,j)]:
      dp[1:,i,j] = np.maximum(dp[1:,i,j],dp[:-1,i,j]+items[(i,j)])

print(dp[:,-1,-1].max())