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 collections import deque
R,C,K = map(int, input().split())
data = [[0 for i in range(C)] for j in range(R)]
for i in range(K):
    r,c,v = map(int, input().split())
    data[r-1][c-1] = v

ans = [[[-1,-1,-1,-1] for i in range(C)] for j in range(R)]

que = deque()
if data[0][0]:
    ans[0][0][1] = data[0][0]
    que.append([0,0,1])

ans[0][0][0] = 0
que.append([0,0,0])
while que:
    i,j,now = que.popleft()
    if i < R-1:
        if data[i+1][j]:
            if ans[i+1][j][1] == -1:
                que.append([i+1,j,1])
            ans[i+1][j][1] = max(ans[i+1][j][1],max(ans[i][j])+data[i+1][j])

        if ans[i+1][j][0] == -1:
            que.append([i+1,j,0])
        ans[i+1][j][0] = max(ans[i+1][j][0], max(ans[i][j]))
    if j < C-1:
        if data[i][j+1] and now <= 2:
            if ans[i][j+1][now+1] == -1:
                ans[i][j+1][now+1] = ans[i][j][now] + data[i][j+1]
                que.append([i,j+1,now+1])
            else:
                ans[i][j + 1][now + 1] = max(ans[i][j + 1][now + 1],ans[i][j][now] + data[i][j + 1])

        if ans[i][j+1][now] == -1:
            que.append([i,j+1,now])

        ans[i][j+1][now] = max(ans[i][j+1][now],ans[i][j][now])

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

Submission status: Time Limit Exceeded

Solution code:
R,C,K = map(int,input().split())
goods = [[-1]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = map(int,input().split())
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if goods[1][1] != -1:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1] != -1:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if goods[i+1][1] != -1:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if goods[i][j] != -1:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            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]))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

def smax(i,j):
    if i>j:
        return i
    else:
        return j


def main():
    R,C,K = list(map(int,input().split()))

    D = [[0]*C for i in range(R)]

    for i in range(K):
        r,c,v = list(map(int,input().split()))
        r,c = r-1,c-1
        D[r][c] = v
        
    DP = [[[0]*4 for j in range(C)] for i in range(R)]

    for i in range(R):
        for j in range(C):
            if i!=0 and j!=0:
                DP[i][j][0] = smax(max(DP[i-1][j]), DP[i][j-1][0])
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], smax(DP[i][j-1][1],max(DP[i-1][j])+D[i][j]))
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            elif i!=0 and j==0:
                DP[i][j][0] = max(DP[i-1][j])
                DP[i][j][1] = max(DP[i-1][j])+D[i][j]
            elif i==0 and j!=0:
                DP[i][j][0] = DP[i][j-1][0]
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], DP[i][j-1][1])
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            else:
                DP[i][j][0] = 0
                DP[i][j][1] = D[i][j]

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

dp=[[[0 for k in range(3)] for j in range(C)] for i in range(R)]

if (R-1,C-1) in item:
    dp[R-1][C-1][0]=item[R-1,C-1]
    dp[R-1][C-1][1]=item[R-1,C-1]
    dp[R-1][C-1][2]=item[R-1,C-1]

for j in range(C-2,-1,-1):
    if (R-1,j) in item:
        dp[R-1][j][0]=max(dp[R-1][j+1][1]+item[R-1,j],dp[R-1][j+1][0])
        dp[R-1][j][1]=max(dp[R-1][j+1][2]+item[R-1,j],dp[R-1][j+1][1])
        dp[R-1][j][2]=max(item[R-1,j],dp[R-1][j+1][2])
    else:
        dp[R-1][j][0]=dp[R-1][j+1][0]
        dp[R-1][j][1]=dp[R-1][j+1][1]
        dp[R-1][j][2]=dp[R-1][j+1][2]

for i in range(R-2,-1,-1):
    if (i,C-1) in item:
        dp[i][C-1][0]=dp[i+1][C-1][0]+item[i,C-1]
        dp[i][C-1][1]=dp[i+1][C-1][0]+item[i,C-1]
        dp[i][C-1][2]=dp[i+1][C-1][0]+item[i,C-1]
    else:
        dp[i][C-1][0]=dp[i+1][C-1][0]
        dp[i][C-1][1]=dp[i+1][C-1][0]
        dp[i][C-1][2]=dp[i+1][C-1][0]

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

print(dp[0][0][0])

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

d = [(1,0),(0,1)]
def solve():
    h,w,k = LI()
    s = [[0]*w for i in range(h)]
    for _ in range(k):
        y,x,v = LI()
        y -= 1
        x -= 1
        s[y][x] = v
    bfs = [[[-float("inf")]*4 for _ in range(w)] for _ in range(h)]
    bfs[0][0][0] = 0
    sc = s[0][0]
    if sc != 0:
        bfs[0][0][1] = sc
    gy,gx = h-1,w-1
    v = h*w*4
    for i in range(v-1):
        update = False
        for y in range(h):
            for x in range(w):
                for n in range(4):
                    sc = bfs[y][x][n]
                    for dy,dx in d:
                        ny,nx = y+dy,x+dx
                        if dy:
                            nn = 0
                        else:
                            nn = n
                        if 0 <= ny < h and 0 <= nx < w:
                            if bfs[ny][nx][nn] < sc:
                                bfs[ny][nx][nn] = sc
                                update = True
                            if nn < 3:
                                nsc = s[ny][nx]
                                if nsc != 0:
                                    nn += 1
                                    nbfs = sc+nsc
                                    if bfs[ny][nx][nn] < nbfs:
                                        bfs[ny][nx][nn] = nbfs
                                        update = True
        if not update:
            break
    print(max(bfs[gy][gx]))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*4 for i in range(w)] for j in range(h)]
dp[0][0][0] = 0
dp[0][0][1] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[i][j][0] = dp[i][j-1][0]
      for k in range(1, 4):
        dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1]+G[i][j], dp[i][j-1][k])
    if i:
      dp[i][j][0] = max(dp[i][j][0], max(dp[i-1][j]))
      dp[i][j][1] = max(dp[i][j][1], max(dp[i-1][j])+G[i][j])
ans = max(dp[h-1][w-1])
print(ans)

Submission status: Time Limit Exceeded

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

X = [[[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):
        X[i][j][0] = max(X[i-1][j][0], X[i-1][j][1], X[i-1][j][2], X[i-1][j][3], X[i][j-1][0])
        X[i][j][1] = max(X[i][j][0] + V[i][j], X[i][j-1][1])
        X[i][j][2] = max(X[i][j-1][1] + V[i][j], X[i][j-1][2])
        X[i][j][3] = max(X[i][j-1][2] + V[i][j], X[i][j-1][3])

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

Submission status: Time Limit Exceeded

Solution code:
# dp[r][c][p] := 位置r, cに到着し、該当行で拾ったアイテムがp個の時の、最大価値
# r = 0〜R, c = 0〜C, p = 0〜3

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

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

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

for item in items:
    item_values[item[0]][item[1]] = item[2]

for r in range(R+1):
    for c in range(C+1):
        value_r_plus = item_values[r+1][c]
        value_c_plus = item_values[r][c+1]
        for k in range(4):
            if value_r_plus > 0:
                # 上方にいどうしてアイテムを拾う
                dp[r+1][c][1] = max(dp[r+1][c][1], dp[r][c][k] + value_r_plus)
            
            # 上方に移動してアイテムを拾わない
            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])

        for k in range(3):
            if value_c_plus > 0:
                # 右に移動してアイテムを拾う
                dp[r][c+1][k+1] = max(dp[r][c+1][k+1], dp[r][c][k] + value_c_plus)
            
print(max(dp[R][C]))

# print(dp)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

d = [(1,0),(0,1)]
def solve():
    h,w,k = LI()
    s = [[0]*w for i in range(h)]
    for _ in range(k):
        y,x,v = LI()
        y -= 1
        x -= 1
        s[y][x] = v
    bfs = defaultdict(lambda : -float("inf"))
    bfs[(0,0,0)] = 0
    sc = s[0][0]
    if sc != 0:
        bfs[(0,0,1)] = sc
    gy,gx = h-1,w-1
    v = h*w*4
    for i in range(v-1):
        update = False
        for y in range(h):
            for x in range(w):
                for n in range(4):
                    sc = bfs[(y,x,n)]
                    for dy,dx in d:
                        ny,nx = y+dy,x+dx
                        if dy:
                            nn = 0
                        else:
                            nn = n
                        if 0 <= ny < h and 0 <= nx < w:
                            if bfs[(ny,nx,nn)] < sc:
                                bfs[(ny,nx,nn)] = sc
                            if nn < 3:
                                nsc = s[ny][nx]
                                if nsc != 0:
                                    nn += 1
                                    nbfs = sc+nsc
                                    if bfs[(ny,nx,nn)] < nbfs:
                                        bfs[(ny,nx,nn)] = nbfs
    print(max([bfs[(gy,gx,n)] for n in range(4)]))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

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


for i in range(R):
  for j in range(C):
    for k in range(4):
      if (i == 0) and (j == 0):
        #if item[0][0] != 0:
        #  dp[0][0][1] = item[0][0]
        continue
      else:
        if i == 0:
          dp[i][j][k] = dp[i][j - 1][k]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])  
        elif j == 0:
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])           
        else:
          dp[i][j][k] = dp[i][j - 1][k]
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])       
    for k in range(3):
      dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k])
    
    for k in range(3):
          if (item[i][j] != 0):
            dp[i][j][3 - k] = max(dp[i][j][3 - k], dp[i][j][2 - k] + item[i][j])          

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

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

for ri in range(R):
    for ci in range(C):
        dp[ri+1][ci+1][0] = max(dp[ri+1][ci][0], dp[ri][ci+1][3])
        dp[ri+1][ci+1][1] = max(dp[ri+1][ci][1], dp[ri+1][ci][0]+item[ri][ci], dp[ri][ci+1][3]+item[ri][ci], dp[ri+1][ci+1][0])
        dp[ri+1][ci+1][2] = max(dp[ri+1][ci][2], dp[ri+1][ci][1]+item[ri][ci], dp[ri+1][ci+1][1])
        dp[ri+1][ci+1][3] = max(dp[ri+1][ci][3], dp[ri+1][ci][2]+item[ri][ci], dp[ri+1][ci+1][2])

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    r,c,k = map(int,input().split())
    dp = [[[0 for t in range(4)] for j in range(c+1)] for i in range(r+1)]
    grid = [[0 for j in range(c+1)] for i in range(r+1)]
    for i in range(k):
        x,y,v = map(int,input().split())
        grid[x][y] = v

    for row in range(1,r+1):
        for col in range(1,c+1):
            dp[row][col][0] = max(dp[row][col-1][0],max(dp[row-1][col]))
            if grid[row][col]:
                for taken in range(3):
                    dp[row][col][taken+1] = max(dp[row][col-1][taken],max(dp[row-1][col]))+grid[row][col]
                
            for taken in range(4):
                dp[row][col][taken] = max(dp[row][col][taken],dp[row][col-1][taken])

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

main()

Submission status: Time Limit Exceeded

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

def main():
    r, c, k = map(int, input().split())
    item = [[0]*c for _ in range(r)]
    
    for i in range(k):
        a, b, v = map(int, input().split())
        item[a-1][b-1] = v
    
    dp0 = [[0]*(c+1) for _ in range(r+1)]
    dp1 = [[0]*(c+1) for _ in range(r+1)]
    dp2 = [[0]*(c+1) for _ in range(r+1)]
    dp3 = [[0]*(c+1) for _ in range(r+1)]
    
    for i in range(r):
        for j in range(c):
            if item[i][j] == 0:
                dp0[i+1][j+1] = max([dp0[i][j+1], dp0[i+1][j], dp1[i][j+1], dp2[i][j+1], dp3[i][j+1]])
                dp1[i+1][j+1] = dp1[i+1][j]
                dp2[i+1][j+1] = dp2[i+1][j]
                dp3[i+1][j+1] = dp3[i+1][j]
                continue
            dp0[i+1][j+1] = max([dp0[i][j+1], dp0[i+1][j], dp1[i][j+1], dp2[i][j+1], dp3[i][j+1]])
            dp1[i+1][j+1] = max(dp0[i+1][j]+item[i][j], dp1[i+1][j], dp0[i][j+1]+item[i][j], dp1[i][j+1]+item[i][j], dp2[i][j+1]+item[i][j], dp3[i][j+1]+item[i][j])
            dp2[i+1][j+1] = max(dp1[i+1][j]+item[i][j], dp2[i+1][j])
            dp3[i+1][j+1] = max(dp2[i+1][j]+item[i][j], dp3[i+1][j])
    
    ans = max([dp0[r][c], dp1[r][c], dp2[r][c], dp3[r][c]])
    print(ans)
            
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W,K=map(int,input().split())
X=[list(map(int,input().split())) for i in range(K)]
Z=[[0]*(W+1) for i in range(H+1)]
for i in range(K):
  Z[X[i][0]-1][X[i][1]-1]=X[i][2]
DP=[[[0,0,0,0,0] for j in range(W+1)] for i in range(H+1)]
DP[0][0][1]=Z[0][0]
for i in range(H):
  for j in range(W):
    for k in range(4):
      DP[i+1][j][0]=max(DP[i+1][j][0],DP[i][j][k])
      DP[i+1][j][1]=max(DP[i+1][j][1],DP[i][j][k]+Z[i+1][j])
      DP[i][j+1][k]=max(DP[i][j+1][k],DP[i][j][k])
      DP[i][j+1][k+1]=max(DP[i][j+1][k+1],DP[i][j][k]+Z[i][j+1])
print(max(DP[H-1][W-1][:4]))

Submission status: Time Limit Exceeded

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

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

items = dict()

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

dp = [0] * 4 * R * C

if (0,0) in items:
    dp[1] = items[(0,0)]

def ind(i,j,k):
    return 4 * C * i + 4 * j + k

for row in range(R):
    for col in range(C):
        for pick in range(4):
            curr = dp[ind(row,col,pick)]
            #Go Right
            if col + 1 < C:
                dp[ind(row,col+1,pick)] = max(curr, dp[ind(row,col+1,pick)])
                if (row, col+1) in items and pick < 3:
                    dp[ind(row,col+1,pick+1)] = max(curr + items[(row,col+1)], dp[ind(row,col+1,pick+1)])
            #Go Down
            if row + 1 < R:
                dp[ind(row+1,col,0)] = max(curr, dp[ind(row+1,col,0)])
                if (row + 1, col) in items:
                    dp[ind(row+1,col,1)] = max(curr + items[(row+1,col)], dp[ind(row+1,col,1)])
                    

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
r,c,k = map(int, input().split())
rcv = [list(map(int, input().split())) for i in range(k)]
d = dict()
for rr,cc,vv in rcv:
    d[(rr-1,cc-1)] = vv

DP = [[[0]*4 for i in range(c)]for j in range(r)]
for i in range(r):
    for j in range(c):
        if (i,j) in d:
            v = d[(i,j)]
            max_top = max(DP[i-1][j]) if i!=0 else 0
            DP[i][j][0] = max_top
            DP[i][j][1] = max_top + v
            for k in range(4):
                if j != 0:
                    if DP[i][j][k] < DP[i][j-1][k]:
                        DP[i][j][k] = DP[i][j-1][k]
                    if k!=0 and DP[i][j][k] < DP[i][j-1][k-1] + v:
                        DP[i][j][k] =  DP[i][j-1][k-1] + v
        else:
            max_top = max(DP[i-1][j]) if i!=0 else 0
            DP[i][j][0] = max_top
            for k in range(4):
                if j != 0 and DP[i][j][k] < DP[i][j-1][k]:
                    DP[i][j][k] = DP[i][j-1][k]
ans = max(DP[-1][-1])
print(ans)

Submission status: Time Limit Exceeded

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

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

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

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

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

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

Submission status: Time Limit Exceeded

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

r, c, k = map(int, input().split())
tizu = [[0 for _ in range(c)] for _ in range(r)]
for _ in range(k):
    rr, cc, v = map(int, input().split())
    tizu[rr - 1][cc - 1] = v
    
dp = [[[0, 0, 0, 0] for _ in range(c)] for _ in range(r)]
dp[0][0][1] = tizu[0][0]

for i in range(r):
    for j in range(c):
        if i != 0:
            dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j]))
            dp[i][j][1] = max(dp[i][j][1], max(dp[i - 1][j]) + tizu[i][j])
        if j != 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0])
            dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1], dp[i][j - 1][0] + tizu[i][j])
            dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2], dp[i][j - 1][1] + tizu[i][j])
            dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3], dp[i][j - 1][2] + tizu[i][j])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
r, c, k = map(int, input().split())
tizu = [[0 for _ in range(c)] for _ in range(r)]
for _ in range(k):
    rr, cc, v = map(int, input().split())
    tizu[rr - 1][cc - 1] = v
    
dp = [[[0, 0, 0, 0] for _ in range(c)] for _ in range(r)]
dp[0][0][1] = tizu[0][0]

for i in range(r):
    for j in range(c):
        if i != 0:
            dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j]))
            dp[i][j][1] = max(dp[i][j][1], max(dp[i - 1][j]) + tizu[i][j])
        if j != 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0])
            dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1], dp[i][j - 1][0] + tizu[i][j])
            dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2], dp[i][j - 1][1] + tizu[i][j])
            dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3], dp[i][j - 1][2] + tizu[i][j])
print(max(dp[-1][-1]))

Submission status: Memory Limit Exceeded

Solution code:
from collections import defaultdict
import sys
input=sys.stdin.readline
r,c,k=map(int,input().split())
d=defaultdict(int)
for i in range(k):
  a1,a2,a3=map(int,input().split())
  d[a1*3001+a2]=a3
dp0=[[0 for i in range(c+1)] for j in range(r+1)]
dp1=[[0 for i in range(c+1)] for j in range(r+1)]
dp2=[[0 for i in range(c+1)] for j in range(r+1)]
dp3=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
  for j in range(c):
    x=d[i*3001+j+1]
    y=d[(i+1)*3001+j]
    dp3[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp2[i+1][j]+y,dp3[i+1][j])
    dp2[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp1[i+1][j]+y,dp2[i+1][j])
    dp1[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j]+y,dp1[i+1][j])
    dp0[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j])
z=d[r*3001+c]
print(max(dp2[i+1][j+1]+z,dp3[i+1][j+1]))

Submission status: Memory Limit Exceeded

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

    H, W, K = map(int, input().split())

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        G[r - 1][c - 1] = v

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

    for r in range(H):
        left = [0] * 4
        for c in range(W):
            cell = dp[r][c]
            up = dp[r - 1][c]  # dp[H-1]行目はr=0のときは未使用なので問題ない

            for k in range(4):
                cell[0] = max(cell[0], up[k])
                cell[k] = max(cell[k], left[k])

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])

            left = cell

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

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

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


if __name__ == '__main__':
    main()

Submission status: Memory Limit Exceeded

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

    H, W, K = map(int, input().split())

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        G[r - 1][c - 1] = v

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

    for r, row in enumerate(dp):
        left = [0] * 4
        for c, cell in enumerate(row):
            up = dp[r - 1][c]  # dp[H-1]行目はr=0のときは未使用なので問題ない

            for k in range(4):
                cell[0] = max(cell[0], up[k])
                cell[k] = max(cell[k], left[k])

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])

            left = cell

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

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

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


if __name__ == '__main__':
    main()

Submission status: Memory Limit Exceeded

Solution code:
r,c,k=[int(i) for i in raw_input().split()]
a=[[-1 for i in range(3005)]for j in range(3005)]
for i in range(k):
    x,y,z=[int(j) for j in raw_input().split()]
    a[x-1][y-1]=z
dp=[[[0,0,0,0] for i in range(3005)]for j in range(3005)]
for i in range(r+1):
    for j in range(c+1):
        if(i>0):
            dp[i][j][0]=dp[i-1][j][3]
            dp[i][j][1]=dp[i-1][j][3]
            dp[i][j][2]=dp[i-1][j][3]
            dp[i][j][3]=dp[i-1][j][3]
        if j>0:
            dp[i][j][3]=max(dp[i][j][3],dp[i][j-1][3])
            dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1])
            dp[i][j][0]=max(dp[i][j][0],dp[i][j-1][0])
        if(i>0 and j>0 and a[i-1][j-1]!=-1):
            dp[i][j][3]=max(dp[i][j][2]+a[i-1][j-1],dp[i][j-1][3])
            dp[i][j][2]=max(dp[i][j][1]+a[i-1][j-1],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][0]+a[i-1][j-1],dp[i][j-1][1])
        
print max(dp[r][c])

Submission status: Memory Limit Exceeded

Solution code:
r,c,k=[int(i) for i in raw_input().split()]
a=[[-1 for i in range(c)]for j in range(r)]
for i in range(k):
    x,y,z=[int(j) for j in raw_input().split()]
    a[x-1][y-1]=z
dp=[[[0,0,0,0] for i in range(c+1)]for j in range(r+1)]
for i in range(r+1):
    for j in range(c+1):
        if(i>0):
            dp[i][j][0]=dp[i-1][j][3]
            dp[i][j][1]=dp[i-1][j][3]
            dp[i][j][2]=dp[i-1][j][3]
            dp[i][j][3]=dp[i-1][j][3]
        if j>0:
            dp[i][j][3]=max(dp[i][j][3],dp[i][j-1][3])
            dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1])
            dp[i][j][0]=max(dp[i][j][0],dp[i][j-1][0])
        if(i>0 and j>0 and a[i-1][j-1]!=-1):
            dp[i][j][3]=max(dp[i][j][2]+a[i-1][j-1],dp[i][j-1][3])
            dp[i][j][2]=max(dp[i][j][1]+a[i-1][j-1],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][0]+a[i-1][j-1],dp[i][j-1][1])
        
print max(dp[r][c])

Submission status: Memory Limit Exceeded

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

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

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


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

	#print(dp)

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















if __name__ == "__main__":
	main()

Submission status: Memory 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)
    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]))