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:
R, C, K = map(int, input().split())
V = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
  r, c, v = map(int, input().split())
  V[r-1][c-1] = v
M = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
# M[r][c][count in column (c)]
for r in range(R):
  for c in range(C):
    for n_pick in [0, 1] if V[r][c] else [0]:
      M[r][c][n_pick] = max(M[r][c][n_pick], n_pick*V[r][c])
      if r > 0:
        for n_prev in range(4):
          #print('R++', r, c, n_pick, n_prev)
          if M[r-1][c][n_prev] > 0 or n_prev == 0:
            M[r][c][n_pick]=max(M[r][c][n_pick],
              M[r-1][c][n_prev] + n_pick*V[r][c])
      if c > 0:
        for n_prev in range(4 - n_pick):
          #print('C++', r, c, n_pick, n_prev)
          if  M[r][c-1][n_prev]  > 0 or n_prev == 0:
            M[r][c][n_prev+n_pick]=max(M[r][c][n_prev+n_pick],
              M[r][c-1][n_prev] + n_pick*V[r][c])
    #print(candidates)
    #print(r, c, candidates, M)
    #for i in range(4):
    #  M[r][c][i] = max(candidates[i])
    #print(r, c, M)
#print(V, M)
print(max(M[R-1][C-1]))

Submission status: Time Limit Exceeded

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 == 0 and col == 0:
                if item != 0:
                    dp[row][col][1] = item
                    continue
            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
            else:
                dp[row][col][0] = max(
                    dp[row][col-1][0],
                    dp[row-1][col][0],
                    dp[row-1][col][1],
                    dp[row-1][col][2],
                    dp[row-1][col][3])
                
                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-1][col][0] + item, 
                        dp[row-1][col][1] + item,
                        dp[row-1][col][2] + item,
                        dp[row-1][col][3] + item,
                        dp[row][col-1][0] + 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]
                        )
    print(max(dp[R-1][C-1]))
main()

Submission status: Time Limit Exceeded

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

def main():
    r,c,k=map(int,input().split())
    dp=[[[0 for kk in range(4)] for jj in range(c+1)] for ii in range(r+1)]

    g=[[0 for j in range(c)] for i in range(r)]
    for i in range(k):
        rr,cc,v=map(int,input().split())
        rr-=1
        cc-=1
        g[rr][cc]=v
    for i in range(1,r+1):
        for j in range(1,c+1):
            for k in range(4):
                dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])#,dp[i][j-1][k])
                dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][k])
                if k>0:
                    if dp[i][j][k]<dp[i][j-1][k-1]+g[i-1][j-1]:
                        dp[i][j][k]=dp[i][j-1][k-1]+g[i-1][j-1]
                if dp[i][j][1]<dp[i-1][j][k]+g[i-1][j-1]:
                    dp[i][j][1]=dp[i-1][j][k]+g[i-1][j-1]
    print(max(dp[r][c]))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
r,c,k = map(int,input().split())
K = defaultdict(int)
for _ in range(k):
    x,y,z= [int(i) for i in input().split()]
    K[(x,y)] = z


dp = [[0]*(c+1) for _ in range(4)]
 
for i in range(1, r+1):
    for j in range(1, c+1):
        v = K[(i, j)]
        dp[0][j] = max(dp[0][j-1], dp[0][j], dp[1][j], dp[2][j], dp[3][j])
        dp[1][j] = max(dp[1][j-1], dp[0][j]+v) if v>0 else dp[1][j-1]
        dp[2][j] = max(dp[2][j-1], dp[1][j-1]+v) if v>0 else dp[2][j-1]
        dp[3][j] = max(dp[3][j-1], dp[2][j-1]+v) if v>0 else dp[3][j-1]
        
print(max(dp[i][-1] for i in range(4)))

Submission status: Time Limit Exceeded

Solution code:
import sys

r,c,k=map(int,input().split())
dp=[[[0 for kk in range(4)] for jj in range(c+1)] for ii in range(r+1)]

g=[[0 for j in range(c)] for i in range(r)]
for i in range(k):
    rr,cc,v=map(int,input().split())
    rr-=1
    cc-=1
    g[rr][cc]=v
for i in range(1,r+1):
    for j in range(1,c+1):
        for k in range(4):
            dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])#,dp[i][j-1][k])
            dp[i][j][0]=max(dp[i][j][0],dp[i-1][j][k])
            if k>0:
                if dp[i][j][k]<dp[i][j-1][k-1]+g[i-1][j-1]:
                    dp[i][j][k]=dp[i][j-1][k-1]+g[i-1][j-1]
            if dp[i][j][1]<dp[i-1][j][k]+g[i-1][j-1]:
                dp[i][j][1]=dp[i-1][j][k]+g[i-1][j-1]
print(max(dp[r][c]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

(R, C, K), *D = [[*map(int,o.split())] for o in open(0)]
D = {(r - 1, c - 1) : v for r, c, v in D}

dp = [[[-1] * 4 for _ in [0] * C] for _ in [0] * R]

dp[0][0][0] = 0
for r in range(R):
    for c in range(C):
        v = D.get((r, c), None)
        for i in range(4):
            if dp[r][c][i] > -1:
                if r < R - 1:
                    dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i])
                if c < C - 1:
                    dp[r][c + 1][i] = max(dp[r][c + 1][i], dp[r][c][i])
                if i < 3 and v:
                    if r < R - 1:
                        dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i] + v)
                    if c < C - 1:
                        dp[r][c + 1][i + 1] = max(dp[r][c + 1][i + 1], dp[r][c][i] + v)
#for l in dp:print(l)
for i in range(3):
    dp[R - 1][C - 1][i] += D.get((R - 1, C - 1), 0)
print(max(dp[R - 1][C - 1]))

Submission status: Time Limit Exceeded

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 == 0 and col == 0:
                if item != 0:
                    dp[row][col][1] = item
                    continue
            elif row == 0:
                dp[row][col][0] = dp[row][col-1][0]
                if item == 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]
                else:
                    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 != 0:
                    dp[row][col][1] = dp[row][col][0] + item
                continue
            else:
                dp[row][col][0] = max(
                    dp[row][col-1][0],
                    dp[row-1][col][0],
                    dp[row-1][col][1],
                    dp[row-1][col][2],
                    dp[row-1][col][3])
                if item == 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]
                else:
                    dp[row][col][1] = max(
                        dp[row-1][col][0] + item, 
                        dp[row-1][col][1] + item,
                        dp[row-1][col][2] + item,
                        dp[row-1][col][3] + item,
                        dp[row][col-1][0] + 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]
                        )
    print(max(dp[R-1][C-1]))
main()

Submission status: Time Limit Exceeded

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

I = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K): 
  r, c, v = map(int, input().split())
  I[r][c] = v 
  
DP1 = [0 for _ in range(C+1)] #c列目までとったとき、最新の行で1個以下とったという条件のもとの最大
DP2 = [0 for _ in range(C+1)] #c列目までとったとき、最新の行で2個以下とったという条件のもとの最大
DP3 = [0 for _ in range(C+1)] #c列目までとったとき、最新の行で3個以下とったという条件のもとの最大
#print(I)

for r in range(1,R+1):
  for c in range(1,C+1): 
    #更新の順番に注意
    #mid = max(DP1[c], DP2[c], DP3[c]) + I[r][c]
    mid = DP3[c] + I[r][c] 
    DP3[c] = max(DP3[c-1], mid, DP2[c-1] + I[r][c]) #3個
    DP2[c] = max(DP2[c-1], mid, DP1[c-1] + I[r][c]) #2個
    DP1[c] = max(DP1[c-1], mid) #1個
  #print(DP3)
    
print(DP3[C])

Submission status: Time Limit Exceeded

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]

point1 = [[0 for _ in range(C)] for _ in range(R)]
point2 = [[0 for _ in range(C)] for _ in range(R)]
point3 = [[0 for _ in range(C)] for _ in range(R)]

point1[0][0] = maps[0][0]

for r in range(R):
  for c in range(C):
    a, b, d = point1[r][c], point2[r][c], point3[r][c]  
    if c < C - 1:
      x = maps[r][c+1]
      point1[r][c+1] = max(point1[r][c+1], x, a)
      point2[r][c+1] = max(point2[r][c+1], a + x, b)
      point3[r][c+1] = max(point3[r][c+1], b + x, d)
    if r < R - 1:
      point1[r+1][c] = maps[r+1][c] + max(a, b, d)


print(max(point1[-1][-1], point2[-1][-1], point3[-1][-1]))

Submission status: Time Limit Exceeded

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):
            if row == 0 and col == 0:
                if item_map[row][col] != 0:
                    dp[row][col][1] = item_map[row][col]
                    continue
            elif row == 0:
                dp[row][col][0] = dp[row][col-1][0]
                if item_map[row][col] == 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]
                else:
                    dp[row][col][1] = max(dp[row][col-1][1], dp[row][col-1][0] + item_map[row][col])
                    dp[row][col][2] = max(dp[row][col-1][2], dp[row][col-1][1] + item_map[row][col])
                    dp[row][col][3] = max(dp[row][col-1][3], dp[row][col-1][2] + item_map[row][col])
                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_map[row][col] != 0:
                    dp[row][col][1] = dp[row][col][0] + item_map[row][col]
                continue
            else:
                dp[row][col][0] = max(
                    dp[row][col-1][0],
                    dp[row-1][col][0],
                    dp[row-1][col][1],
                    dp[row-1][col][2],
                    dp[row-1][col][3])
                if item_map[row][col] == 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]
                else:
                    dp[row][col][1] = max(
                        dp[row-1][col][0] + item_map[row][col], 
                        dp[row-1][col][1] + item_map[row][col],
                        dp[row-1][col][2] + item_map[row][col],
                        dp[row-1][col][3] + item_map[row][col],
                        dp[row][col-1][0] + item_map[row][col],
                        dp[row][col-1][1]
                        )
                    dp[row][col][2] = max(
                        dp[row][col-1][1] + item_map[row][col],
                        dp[row][col-1][2]
                        )
                    dp[row][col][3] = max(
                        dp[row][col-1][2] + item_map[row][col],
                        dp[row][col-1][3]
                        )
    print(max(dp[R-1][C-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
#dp[i][j][k]=(i,j)にアイテムをk個以下使って移動するときの最大値
#入力
r,c,k=list(map(int,input().split()))

rck=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(k):
     r_num,c_num,k_num=list(map(int,input().split()))
     rck[r_num][c_num]=k_num

#dpテーブルと初期条件
dp=[[[0 for i in range(4)] for j in range(c+1)] for k in range(r+1)]
#dp
for i in range(1,r+1):
  for j in range(1,c+1):
    for k in range(1,4):
      dp[i][j][k]=max(dp[i-1][j][3]+rck[i][j],dp[i][j-1][k-1]+rck[i][j],dp[i][j-1][k],dp[i][j][k-1])
    #print(dp[i][j])
print(dp[r][c][3])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():

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

    # row2item = {r: [0]*C for r in range(R)}
    #  # r -> index, col, val
    item_map = [[0]*C for _ in range(R)]

    for i 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):
            if row == 0 and col == 0:
                if item_map[row][col] != 0:
                    dp[row][col][1] = item_map[row][col]
            elif row == 0:
                dp[row][col][0] = dp[row][col-1][0]
                if item_map[row][col] == 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]
                else:
                    dp[row][col][1] = max(dp[row][col-1][1], dp[row][col-1][0] + item_map[row][col])
                    dp[row][col][2] = max(dp[row][col-1][2], dp[row][col-1][1] + item_map[row][col])
                    dp[row][col][3] = max(dp[row][col-1][3], dp[row][col-1][2] + item_map[row][col])
            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_map[row][col] != 0:
                    dp[row][col][1] = dp[row][col][0] + item_map[row][col]
            else:
                dp[row][col][0] = max(
                    dp[row][col-1][0],
                    dp[row-1][col][0],
                    dp[row-1][col][1],
                    dp[row-1][col][2],
                    dp[row-1][col][3])
                if item_map[row][col] == 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]
                else:
                    dp[row][col][1] = max(
                        dp[row-1][col][0] + item_map[row][col], 
                        dp[row-1][col][1] + item_map[row][col],
                        dp[row-1][col][2] + item_map[row][col],
                        dp[row-1][col][3] + item_map[row][col],
                        dp[row][col-1][0] + item_map[row][col],
                        dp[row][col-1][1]
                        )
                    dp[row][col][2] = max(
                        dp[row][col-1][1] + item_map[row][col],
                        dp[row][col-1][2]
                        )
                    dp[row][col][3] = max(
                        dp[row][col-1][2] + item_map[row][col],
                        dp[row][col-1][3]
                        )
    print(max(dp[R-1][C-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
# = int(input())
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 r in range(R):
  for c in range(C):
    a, b, d = point[r][c][0], point[r][c][1], point[r][c][2]  
    if c < C - 1:
      x = maps[r][c+1]
      y = point[r][c+1]
      y[0] = max(y[0], x, a)
      y[1] = max(y[1], a + x, b)
      y[2] = max(y[2], b + x, d)
      point[r][c+1] = y
    if r < R - 1:
      point[r+1][c][0] = max(point[r+1][c][0], maps[r+1][c] + max(a, b, d))     


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

Submission status: Time Limit Exceeded

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

# row2item = {r: [0]*C for r in range(R)}
#  # r -> index, col, val
item_map = [[0]*C for _ in range(R)]

for i 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):
        if row == 0 and col == 0:
            if item_map[row][col] != 0:
                dp[row][col][1] = item_map[row][col]
        elif row == 0:
            dp[row][col][0] = dp[row][col-1][0]
            if item_map[row][col] == 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]
            else:
                dp[row][col][1] = max(dp[row][col-1][1], dp[row][col-1][0] + item_map[row][col])
                dp[row][col][2] = max(dp[row][col-1][2], dp[row][col-1][1] + item_map[row][col])
                dp[row][col][3] = max(dp[row][col-1][3], dp[row][col-1][2] + item_map[row][col])
        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_map[row][col] != 0:
                dp[row][col][1] = dp[row][col][0] + item_map[row][col]
        else:
            dp[row][col][0] = max(
                dp[row][col-1][0],
                dp[row-1][col][0],
                dp[row-1][col][1],
                dp[row-1][col][2],
                dp[row-1][col][3])
            if item_map[row][col] == 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]
            else:
                dp[row][col][1] = max(
                    dp[row-1][col][0] + item_map[row][col], 
                    dp[row-1][col][1] + item_map[row][col],
                    dp[row-1][col][2] + item_map[row][col],
                    dp[row-1][col][3] + item_map[row][col],
                    dp[row][col-1][0] + item_map[row][col],
                    dp[row][col-1][1]
                    )
                dp[row][col][2] = max(
                    dp[row][col-1][1] + item_map[row][col],
                    dp[row][col-1][2]
                    )
                dp[row][col][3] = max(
                    dp[row][col-1][2] + item_map[row][col],
                    dp[row][col-1][3]
                    )
print(max(dp[R-1][C-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)
 
R, C, K = rl()
I = [[0]*(C+2) for _ in range(R+2)]
for i in range(K):
	r, c, v = rl()
	I[r][c] = v
 
dp = [[[0 for _ in range(C+2)] for _ in range(R+2)] for _ in range(4)]

for i in range(R+1):
	for j in range(C+1):
		for m in range(4):
			ni, nj = i, j+1
			dp[m][ni][nj] = max(dp[m][ni][nj], dp[m][i][j])
			if m < 3:
				dp[m+1][ni][nj] = max(dp[m+1][ni][nj], dp[m][i][j]+I[ni][nj])
			ni, nj = i+1, j
			dp[0][ni][nj] = max(dp[0][ni][nj], dp[m][i][j])
			dp[1][ni][nj] = max(dp[1][ni][nj], dp[m][i][j]+I[ni][nj])
ans = 0
for m in range(4):
	ans = max(ans, dp[m][R][C])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

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


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

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

Submission status: Time Limit Exceeded

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

X = [[0] * 4 for _ in range(R + 1)]
for i in range(1, C + 1):
    Vi = V[i]
    for j in range(1, R + 1):
        v = Vi[j]
        Xj = X[j]
        if v:
            Xj[3] = max(Xj[3], Xj[2] + v)
            Xj[2] = max(Xj[2], Xj[1] + v)
            Xj[0] = max(Xj[0], max(X[j-1]))
            Xj[1] = max(Xj[1], Xj[0] + v)
        else:
            Xj[0] = max(Xj[0], max(X[j-1]))

print(max(X[R]))

Submission status: Time Limit Exceeded

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]
        for k in range(4):
            # Use
            if item and k < 3:
                dp[k+1][ij1] = max(dp[k+1][ij1], dp[k][ij] + item) 
                dp0[i1j] = max(dp0[i1j], dp[k][ij] + item) 
            # No use down
            if dp[k][ij] > dp0[i1j]:
                dp0[i1j] = dp[k][ij]
            # No use right
            if dp[k][ij] > dp[k][ij1]:
                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: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
H, W, K = map(int, input().split())
B = {}
for _ in range(K):
    r, c, v = map(int, input().split())
    B[(r, c)] = v

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if (i, j) in B:
            v = B[(i, j)]
            dp[j][0] = max(dp[j-1][0], dp[j][0], dp[j][1], dp[j][2], dp[j][3])
            dp[j][1] = max(dp[j-1][1], dp[j][0]+v)
            dp[j][2] = max(dp[j-1][2], dp[j-1][1]+v)
            dp[j][3] = max(dp[j-1][3], dp[j-1][2]+v)
        else:
            dp[j][0] = max(dp[j-1][0], dp[j][0], dp[j][1], dp[j][2], dp[j][3])
            dp[j][1] = dp[j-1][1]
            dp[j][2] = dp[j-1][2]
            dp[j][3] = dp[j-1][3]

print(max(dp[-1]))

Submission status: Time Limit Exceeded

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 and k < 3:
                dp[k+1][ij1] = max(dp[k+1][ij1], dp[k][ij] + item) 
                dp0[i1j] = max(dp0[i1j], dp[k][ij] + item) 
            # No use down
            if dp[k][ij] > dp0[i1j]:
                dp0[i1j] = dp[k][ij]
            # No use right
            if dp[k][ij] > dp[k][ij1]:
                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: Time Limit Exceeded

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

R, C, K = map(int, readline().split())
g = [[-float('inf')]*C for _ in range(R)]
for i in range(K):
    r, c, k = map(int, readline().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(2, -1, -1):
            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: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
r, c, k = map(int, input().split())
pt = [[0 for _ in range(c + 5)] for _ in range(r + 5)]

for i in range(k):
    r1, c1, v = map(int, input().split())
    pt[r1][c1] = v

memo = [[0] * 4 for _ in range(c + 1)]
prev = [[0] * 4 for _ in range(c + 1)]
for i in range(1, r + 1):
    memo, prev = prev, memo
    for j in range(1, c + 1):
        V = pt[i][j]
        memo[j][0] = max(prev[j][3], memo[j - 1][0])
        memo[j][1] = max(memo[j][0] + V, memo[j - 1][1])
        memo[j][2] = max(memo[j][1], memo[j - 1][1] + V, memo[j - 1][2])
        memo[j][3] = max(memo[j][2], memo[j - 1][2] + V, memo[j - 1][3])
print(memo[-1][3])

Submission status: Time Limit Exceeded

Solution code:
def main():
    (h,w,k),*t=[[*map(int,t.split())]for t in open(0)]
    dp=[[[0]*4for _ in range(w+1)]for _ in range(h+1)]
    c=[[0]*(w+1)for _ in range(h+1)]
    for y,x,v in t:c[y][x]=v
    for i in range(1,h+1):
        for j in range(1,w+1):
            for k in range(4):dp[i][j][k]=dp[i][j-1][k]
            for k in range(1,4):dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+c[i][j])
            k=c[i][j]>0
            dp[i][j][k]=max(dp[i][j][k],max(dp[i-1][j])+c[i][j])
    print(max(dp[h][w]))
main()

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 20
R, C, K = map(int, input().split())
W = 3
value = [[0] * (C+1) for _ in range(R+1)]
for _ in range(K):
  r, c, v = map(int, input().split())
  value[r][c] = v

dp = [[[-INF] * (W+1) for _ in range(C+1)] for _ in range(R+1)]
dp[0][1][0] = 0
dp[1][0][0] = 0


for r in range(1, R+1):
  for c in range(1, C+1):
    for w in range(W+1):
      if w == 0:
        dp[r][c][w] = max(max(dp[r-1][c]), dp[r][c-1][w], dp[r][c][w])
      else:
        dp[r][c][w] = max(dp[r][c-1][w], dp[r][c][w])
      if -1 < w-1 and 0 < value[r][c] :
        if w == 1:
          dp[r][c][w] = max(max(dp[r-1][c]) + value[r][c], dp[r][c-1][w-1] + value[r][c], dp[r][c][w])
        elif 0 <= dp[r][c-1][w-1]:
          dp[r][c][w] = max(dp[r][c-1][w-1] + value[r][c], dp[r][c][w])

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