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

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=[[[0,0,0,0] for _ in range(C+1) ] for __ in range(R+1)]

for y in range(1,R+1):
    for x in range(1,C+1):
        #右に移動
        for k in range(0,3+1):
            DP[y][x][k]=DP[y][x-1][k]

        for k in range(1,3+1):
            DP[y][x][k]=max(DP[y][x][k],DP[y][x-1][k-1]+Value[y][x])

        #下に移動
        G=max(DP[y-1][x])
        DP[y][x][0]=max(DP[y][x][0],G)

        DP[y][x][1]=max(DP[y][x][1],DP[y][x][0]+Value[y][x])


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

Submission status: Time Limit Exceeded

Solution code:
r,c,k=map(int,input().split())
goods=[list(map(int,input().split())) for _ in range(k)]
g=[[0]*c for _ in range(r)]
for cr,cc,ck in goods:
    g[cr-1][cc-1]=ck

dp=[[[-1]*4 for _ in range(c+1)] for _ in range(r+1)]
maxlst=[[0]*(c+1) for _ in range(r+1)]

for i in range(r):
    dp[i][0][0]=0
for j in range(c):
    dp[0][j][0]=0

for i in range(r):
    for j in range(c):
        tmp=0
        for k in range(3):
            if g[i][j]>0 and dp[i+1][j][k]>=0:
                dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
            else:
                dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
            tmp=max(tmp,dp[i+1][j+1][k+1])
        
        dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
        tmp=max(tmp,dp[i+1][j+1][0])
        
        if g[i][j]>0 and dp[i+1][j+1][0]>=0:
            dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
            tmp=max(tmp,dp[i+1][j+1][1])
        
        maxlst[i+1][j+1]=tmp
        
        
            
print(maxlst[r][c])

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
a = [[0]*C for _ in range(R)]
dp = [[[0]*4 for _ in range(C+5)] for _ in range(R+5)]
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[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
        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])

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
a = [[0]*3005 for _ in range(3005)]
dp = [[[0]*4 for _ in range(3005)] for _ in range(3005)]
R, C, K = map(int, input().split())
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[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
        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])

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

Submission status: Time Limit Exceeded

Solution code:
a = [[0]*3005 for _ in range(3005)]
dp = [[[0]*4 for _ in range(3005)] for _ in range(3005)]
R, C, K = map(int, input().split())
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[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
        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])

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

Submission status: Time Limit Exceeded

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+1)] for _ in range(h+1)] for _ in range(4)]

for y in range(h):
    for x in range(w):
        for i in range(4):
            dp[0][y+1][x]=max(dp[0][y+1][x],dp[i][y][x])
            dp[i][y][x+1]=max(dp[i][y][x+1],dp[i][y][x])
        for i in range(3):
            dp[0][y+1][x]  =max(dp[0][y+1][x],  dp[i][y][x]+item[y][x])
            dp[i+1][y][x+1]=max(dp[i+1][y][x+1],dp[i][y][x]+item[y][x])
ans=dp[0][-1][-2]
print(ans)

Submission status: Time Limit Exceeded

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(4)] for _ in range(w)] for _ in range(h)]

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    R, C, K = LI()
    G = [[0 for _ in range(C)] for __ in range(R)]
    dp = [[[0, 0, 0, 0] for _ in range(C+1)] for __ in range(R+1)]
    # k: taken
    for i in range(K):
        r, c, v = LI()
        r -= 1
        c -= 1
        G[r][c] = v
    dp[0][0][0] = 0
    dp[0][0][1] = G[0][0]

    for r in range(R):
        for c in range(C):
            for k in range(4):
                if 0 <= c+1 < C and k + 1 <= 3:
                    # go right and take
                    dp[r][c+1][k+1] = max(
                        dp[r][c][k] + G[r][c+1],
                        dp[r][c+1][k+1]
                    )

                if 0 <= c+1 < C:
                    # go right and not take
                    dp[r][c+1][k] = max(
                        dp[r][c+1][k],
                        dp[r][c][k]
                    )

                if 0 < r+1 < R:
                    # go down and take
                    dp[r+1][c][1] = max(
                        dp[r][c][k] + G[r+1][c],
                        dp[r+1][c][1]
                    )
                    # go down and not take
                    dp[r+1][c][0] = max(
                        dp[r][c][k],
                        dp[r+1][c][0]
                    )

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

Submission status: Time Limit Exceeded

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

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


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

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

    val = F[i][j]
  
      
    mxv = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3],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, mxv + val)
    dp[i][j][0] = mxv

best = 0

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

Submission status: Time Limit Exceeded

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
      
    mxv = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3],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, mxv + val)
    dp[i][j][0] = mxv
    #print(dp[i])
    
best = 0

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

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目、j列のマスまで見る。k個取る時の最高値。
"""
def main():
    import sys
    input = sys.stdin.readline
    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]*4 for _ in range(C)]for _ in range(R)]

    for i in range(R):
        for j in range(C):
            v = items[i][j]
            if i == 0 and j == 0 and v != 0:
                dp[i][j][1] = v

            #上から降りてくる場合の価値を求める
            if i != 0:
                #i行目j列のアイテムを取らない場合
                dp[i][j][0] = max(dp[i-1][j])
                #i行目j列のアイテムを取る場合
                if v!=0:
                    dp[i][j][1] = max(dp[i-1][j]) + v

            #左から入ってくる場合の価値を求める
            if j != 0:
                if v == 0:
                    for k in range(4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
                else:
                    for k in range(1,4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k],dp[i][j-1][k-1]+v)
        if i > 0:
            tmp = dp[i-1]
            dp[i-1] = []
            del(tmp)
    print(max(dp[-1][-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目、j列のマスまで見る。k個取る時の最高値。
"""
def main():
    import sys
    input = sys.stdin.readline
    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]*4 for _ in range(C)]for _ in range(R)]

    for i in range(R):
        for j in range(C):
            v = items[i][j]
            if i == 0 and j == 0 and v != 0:
                dp[i][j][1] = v

            #上から降りてくる場合の価値を求める
            if i != 0:
                #i行目j列のアイテムを取らない場合
                dp[i][j][0] = max(dp[i-1][j])
                #i行目j列のアイテムを取る場合
                if v!=0:
                    dp[i][j][1] = max(dp[i-1][j]) + v

            #左から入ってくる場合の価値を求める
            if j != 0:
                if v == 0:
                    for k in range(4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
                else:
                    for k in range(1,4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k],dp[i][j-1][k-1]+v)
        if i > 0:
            dp[i-1] = []
    print(max(dp[-1][-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目、j列のマスまで見る。k個取る時の最高値。
"""
def main():
    import sys
    input = sys.stdin.readline
    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]*4 for _ in range(C)]for _ in range(R)]

    for i in range(R):
        for j in range(C):
            v = items[i][j]
            if i == 0 and j == 0 and v != 0:
                dp[i][j][1] = v

            #上から降りてくる場合の価値を求める
            if i != 0:
                #i行目j列のアイテムを取らない場合
                dp[i][j][0] = max(dp[i-1][j])
                #i行目j列のアイテムを取る場合
                if v!=0:
                    dp[i][j][1] = max(dp[i-1][j]) + v

            #左から入ってくる場合の価値を求める
            if j != 0:
                if v == 0:
                    for k in range(4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
                else:
                    for k in range(1,4):
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k],dp[i][j-1][k-1]+v)
    print(max(dp[-1][-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目、j列のマスまで見る。k個取る時の最高値。
"""
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]*4 for _ in range(C)]for _ in range(R)]

for i in range(R):
    for j in range(C):
        v = items[i][j]
        if i == 0 and j == 0 and v != 0:
            dp[i][j][1] = v

        #上から降りてくる場合の価値を求める
        if i != 0:
            #i行目j列のアイテムを取らない場合
            dp[i][j][0] = max(dp[i-1][j])
            #i行目j列のアイテムを取る場合
            if v!=0:
                dp[i][j][1] = max(dp[i-1][j]) + v

        #左から入ってくる場合の価値を求める
        if j != 0:
            if v == 0:
                for k in range(4):
                    dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
            else:
                for k in range(1,4):
                    dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k],dp[i][j-1][k-1]+v)
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(ans)

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:
R,C,K = map(int, input().split())

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

for i in range(K):
    r, c, v = map(int, input().split())
    d[r-1][c-1] = v
for i in range(R):
    for j in range(C):
        # kが大きい方からやらないとダメ。01ナップサックと同じ考え方。書き換えられた値を見ないように。
        for k in range(2, -1, -1):
            # 拾う遷移。d[i][j]を取るかどうかという判定
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k] + d[i][j])
        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])
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys


def main():
    R,C,K = list(map(int, sys.stdin.readline().split()))
    
    items_dic = {}
    for _ in range(K):
        r,c,v = list(map(int, sys.stdin.readline().split()))
        items_dic[ (r,c) ] = v
    

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

    for r in range(1, R+1):
        for c in range(1, C+1):

            for k in range(4):
                if (r,c) in items_dic:
                    v = items_dic[(r,c)]

                    # get item, move ↓
                    dp[1][r][c] = max( dp[1][r][c] , dp[k][r-1][c] + v )  # k=0,1,2,3

                    # get item, move →
                    if k >= 1:
                        dp[k][r][c] = max( dp[k][r][c] , dp[k-1][r][c-1] + v )  # k=1,2,3

                # don't get item, move ↓
                dp[0][r][c] = max( dp[0][r][c] , dp[k][r-1][c] )  # k=0,1,2,3
                # don't get item, move →
                dp[k][r][c] = max( dp[k][r][c] , dp[k][r][c-1] )  # k=0,1,2,3


    ans = 0

    for k in range(4):
        ans = max(ans, dp[k][R][C])

    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys


def main():
    R,C,K = list(map(int, sys.stdin.readline().split()))
    rcv_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(K) ]


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

    items_dic = {}

    for r,c,v in rcv_list:
        items_dic[ (r,c) ] = v
    

    for r in range(1, R+1):
        for c in range(1, C+1):

            for k in range(4):
                if (r,c) in items_dic:
                    v = items_dic[(r,c)]

                    # get item, move ↓
                    dp[1][r][c] = max( dp[1][r][c] , dp[k][r-1][c] + v )  # k=0,1,2,3

                    # get item, move →
                    if k >= 1:
                        dp[k][r][c] = max( dp[k][r][c] , dp[k-1][r][c-1] + v )  # k=1,2,3

                # don't get item, move ↓
                dp[0][r][c] = max( dp[0][r][c] , dp[k][r-1][c] )  # k=0,1,2,3
                # don't get item, move →
                dp[k][r][c] = max( dp[k][r][c] , dp[k][r][c-1] )  # k=0,1,2,3


    ans = 0

    for k in range(4):
        ans = max(ans, dp[k][R][C])

    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

dp[1][0][0] = item[0][0]

for h in range(R):
    for w in range(C):
        for k in range(4):
            if h!=0:
                dp[0][h][w] = max(dp[0][h][w], dp[k][h-1][w])
                dp[1][h][w] = max(dp[1][h][w], dp[k][h-1][w]+item[h][w])
            if w!=0:
                dp[k][h][w] = max(dp[k][h][w], dp[k][h][w-1])
                if k==0: continue
                dp[k][h][w] = max(dp[k][h][w], dp[k-1][h][w-1]+item[h][w])
ans = []
for k in range(4):
    ans.append(dp[k][-1][-1])
print(max(ans))

Submission status: Time Limit Exceeded

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

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[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+a[i][j])
    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])
    
ans = 0
for k in range(4):
  ans = max(ans, dp[R-1][C-1][k])
print(ans)

Submission status: Time Limit Exceeded

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[0][r][c-1] + v)
                dp[2][r][c] = max(dp[2][r][c], dp[1][r][c-1] + v)
                dp[3][r][c] = max(dp[3][r][c], dp[2][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: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque


def main():
    R,C,K = list(map(int, sys.stdin.readline().split()))
    rcv_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(K) ]


    items_dic = {}

    for r,c,v in rcv_list:
        items_dic[ (r,c) ] = v
    
    stack = deque([ (1,1,0,0) ])  #  (row, column, score, count of items at same row)
    max_score = 0

    while stack:
        row,col,score,cnt = stack.pop()

        if (row == R) and (col == C):
            if (row, col) in items_dic  and  cnt <= 2:
                score += items_dic[(row, col)]
            
            max_score = max(max_score, score)


        if (row, col) in items_dic:
            if (col+1) <= C:
                if cnt <= 2:
                    stack.append( ( row , col+1 , score + items_dic[(row, col)] , cnt + 1 ) )
                
                stack.append( ( row , col+1 , score , cnt ) )

            if (row+1) <= R:
                if cnt <= 2:
                    stack.append( ( row+1 , col , score + items_dic[(row, col)] , 0 ) )
                
                stack.append( ( row+1 , col , score , 0 ) )
        
        else:
            if (col+1) <= C:
                stack.append( ( row , col+1 , score , cnt ) )
            if (row+1) <= R:
                stack.append( ( row+1 , col , score , 0 ) )
    
    
    print(max_score)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

for inputk 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):
            if varr[k][i][j] >= 0:
                if  varr[k+1][i][j] < varr[k][i][j] + A[i][j]:
                    varr[k+1][i][j] = varr[k][i][j] + A[i][j]
        for kk in range(0,4):
            if varr[kk][i][j] >= 0:
                if i +1 < R :
                    if  varr[0][i + 1][j] < varr[kk][i][j]:
                        varr[0][i + 1][j] = varr[kk][i][j] 
                if j +1 < C :
                    if  varr[kk][i][j + 1] < varr[kk][i][j]:
                        varr[kk][i][j + 1] = varr[kk][i][j] 
ans = 0
for k in range (4):
    if ans < varr[k][R-1][C-1]:
        ans = varr[k][R-1][C-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve(R,C,K,item):
  dp = np.zeros((C+1,5), dtype=np.int64)
  for i in range(1,R+1):
    new_dp = np.zeros((C+1,5), dtype=np.int64)
    #上からアイテムを取らずに移動
    new_dp[:,0] = dp[:,-1]
    #上からアイテムを取って移動
    new_dp[1:,1] = np.maximum(new_dp[1:,1],dp[1:,-1]+item[i-1])
    for j in range(1,C+1):
      #横からアイテムを取らずに移動
      new_dp[j] = np.maximum(new_dp[j],new_dp[j-1])
      #横からアイテムを取って移動
      for k in range(1,4):
        new_dp[j,k] = max(new_dp[j,k],new_dp[j-1,k-1]+item[i-1,j-1])
    for k in range(4):
      new_dp[:,-1] = np.maximum(new_dp[:,-1],new_dp[:,k])
    dp = new_dp
  ans = dp[-1,-1]
  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: Time Limit Exceeded

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

H,W,K = map(int,readline().split())
from collections import defaultdict
goods = defaultdict(int)
for i in range(K):
  r,c,v = map(int,readline().split())
  r -= 1
  c -= 1
  goods[(r,c)] = v
  
# dp[k][i][j] = マス(i,j)までに、その行でk回(0 - 3)アイテムを拾った場合の最大得点
dp = [[[0 for j in range(W)] for i in range(H)] for k in range(4)]
# 最初のマスにアイテムがある場合の得点を入れておく
if (0,0) in goods:
  dp[1][0][0] = goods[(0,0)]

def show():
  for i in range(len(dp)):
    for j in range(len(dp[i])):
      print("i",i,"j",j,dp[i][j])

for i in range(H):
  for j in range(W):
    if i + 1 < H:
      # 縦の次のマスに行ける場合
      check = False
#      if i == 0 and j == 1:
#        check = True
        
      if (i + 1, j) in goods:
        value = goods[(i + 1, j)]
        # 次のマスにアイテムがあり、それを取る場合
        # 縦に進む場合は、必ず0からスタート
        for k in range(4):
          dp[1][i + 1][j] = max(dp[1][i + 1][j], dp[k][i][j] + value)
      # 次のマスのアイテムがない/取らない場合
      for k in range(4):
        if check:
          print("dp[i + 1][j][k]",dp[i + 1][j][k],"dp[i][j][k]",dp[i][j][k])
        dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[k][i][j])
        if check:
          print("i + 1",i + 1,"j",j,"dp[i + 1][j][0]",dp[i + 1][j][0])
          show()
    if j + 1 < W:
      if (i, j + 1) in goods:
        value = goods[(i, j + 1)]
        # 次のマスにアイテムがあり、それを取る場合
        for k in range(3):
          dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], dp[k][i][j] + value)
      # 次のマスにアイテムがない/取らない場合
      for k in range(4):
        dp[k][i][j + 1] = max(dp[k][i][j + 1], dp[k][i][j])

ans = 0
for i in range(4):
  if dp[i][-1][-1] > ans:
    ans = dp[i][-1][-1]
print(ans)

Submission status: Time Limit Exceeded

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

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

I = [[0]*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]*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(2, -1, -1):
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+I[i][j])
        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])


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

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

Submission status: Time Limit Exceeded

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

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

I = [[0]*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(2, -1, -1):
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+I[i][j])
        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])


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

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

Submission status: Time Limit Exceeded

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

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(2, -1, -1):
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+I[i][j])
        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])


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

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

Submission status: Time Limit Exceeded

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(2, -1, -1):
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]+I[i][j])
        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])


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

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