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:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    r, c, k = ns()

    mat = [[0 for _ in range(c + 1)] for __ in range(r + 1)]
    for _ in range(k):
        x, y, v = ns()
        x, y = x - 1, y - 1
        mat[x][y] = v

    dp = [[[0 for ___ in range(4)] for _ in range(c + 1)] for __ in range(r + 1)]

    for i in range(r):
        for j in range(c):
            for n in range(2, -1, -1):
                dp[i][j][n + 1] = max(dp[i][j][n + 1], dp[i][j][n] + mat[i][j])

            dp[i + 1][j][0] = max(dp[i][j])
            for n in range(4):
                dp[i][j + 1][n] = max(dp[i][j + 1][n], dp[i][j][n])

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

dp = [[[0]*4 for _ in range(C+1)] for _ 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] + g[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:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    r, c, k = ns()

    mat = {}
    for _ in range(k):
        x, y, v = ns()
        x, y = x - 1, y - 1
        mat[(x, y)] = v

    dp = [[[0 for ___ in range(4)] for _ in range(c + 1)] for __ in range(r + 1)]
    dp[0][0][1] = mat[(0, 0)] if (0, 0) in mat.keys() else 0

    for i in range(r):
        for j in range(c):
            dp[i + 1][j][0] = max(dp[i][j])
            if (i + 1, j) in mat.keys():
                if mat[(i + 1, j)] != 0:
                    dp[i + 1][j][1] = dp[i + 1][j][0] + mat[(i + 1, j)]

            for m in range(4):
                dp[i][j + 1][m] = max(dp[i][j + 1][m], dp[i][j][m])

            if (i, j + 1) in mat.keys():
                if mat[(i, j + 1)] > 0:
                    for m in range(2, -1, -1):
                        if dp[i][j][m] != 0 or m == 0:
                            dp[i][j + 1][m + 1] = max(dp[i][j + 1][m + 1], dp[i][j][m] + mat[(i, j + 1)])

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

def sum3(masu):
    ls = []
    sumls = []
    for i in range(len(masu)):
        if len(ls) < 3:
            ls.append(masu[i])
            sumls.append(sum(ls))
        else:
            if min(ls) < masu[i]:
                ls.remove(min(ls))
                ls.append(masu[i])
            sumls.append(sum(ls))
    return sumls

DP[0] = sum3(masu1[0])

for i in range(1,R):
    for j in range(C):
        for k in range(j,C):
            if j != 0:
                DP[i][k] = max(DP[i][k],DP[i-1][j]+sum3(masu1[i][j:])[k-j])
            else:
                DP[i][k] = DP[i-1][j]+sum3(masu1[i][j:])[k-j]
print(DP[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    r, c, k = ns()

    mat = [[0 for _ in range(c + 1)] for __ in range(r + 1)]
    for _ in range(k):
        x, y, v = ns()
        x, y = x - 1, y - 1
        mat[x][y] = v

    dp = [[[0 for ___ in range(4)] for _ in range(c + 1)] for __ in range(r + 1)]
    dp[0][0][1] = mat[0][0]

    for i in range(r):
        for j in range(c):
            dp[i + 1][j][0] = max(dp[i][j])
            if mat[i + 1][j] != 0:
                dp[i + 1][j][1] = dp[i + 1][j][0] + mat[i + 1][j]

            for m in range(4):
                dp[i][j + 1][m] = max(dp[i][j + 1][m], dp[i][j][m])

            if mat[i][j + 1] > 0:
                for m in range(2, -1, -1):
                    if dp[i][j][m] != 0 or m == 0:
                        dp[i][j + 1][m + 1] = max(dp[i][j + 1][m + 1], dp[i][j][m] + mat[i][j + 1])

    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())
math=[[0]*C for _ in range(R)]
for _ in range(K):
  r, c, v=map(int, input().split())
  math[r-1][c-1]=v
  
dp=[[[0]*4 for __ in range(C)] for _ in range(R)]
if math[0][0]>0:
  dp[0][0][1]=math[0][0]+0
  
for j in range(1, C):
  dp[0][j][0]=dp[0][j-1][0]+0
  dp[0][j][1]=max(dp[0][j-1][1], dp[0][j-1][0]+math[0][j])
  dp[0][j][2]=max(dp[0][j-1][2], dp[0][j-1][1]+math[0][j])
  dp[0][j][3]=max(dp[0][j-1][3], dp[0][j-1][2]+math[0][j])
    
for i in range(1, R):
  dp[i][0][0]=max(dp[i-1][0])
  dp[i][0][1]=dp[i][0][0]+math[i][0]
  for j in range(1, C):
    dp[i][j][0]=max(dp[i][j-1][0], max(dp[i-1][j]))
    dp[i][j][1]=max(dp[i][j-1][1], dp[i][j-1][0]+math[i][j], max(dp[i-1][j])+math[i][j])
    dp[i][j][2]=max(dp[i][j-1][2], dp[i][j-1][1]+math[i][j])
    dp[i][j][3]=max(dp[i][j-1][3], dp[i][j-1][2]+math[i][j])
    
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

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


DP = [[[-float('inf')] * 4 for _ in range(C + 1)] for __ in range(R + 1)]

for y in range(R+1):
    for x in range(C+1):
        if y == 0:
            DP[y][x][0] = 0
            continue
        if x == 0:
            DP[y][x][0] = 0
            continue
        if Items[y][x] == 0:
            for k in range(4):
                DP[y][x][0] = max(DP[y][x][0], DP[y - 1][x][k])
                if k != 0:
                    DP[y][x][k] = DP[y][x-1][k]
            DP[y][x][0] = max(DP[y][x][0], DP[y][x-1][0])
        else:
            for k in range(4):
                DP[y][x][0] = max(DP[y][x][0], DP[y - 1][x][k])
            DP[y][x][0] = max(DP[y][x][0], DP[y][x - 1][0])
            for k in range(1, 4):
                DP[y][x][k] = max(DP[y][x-1][k-1] + Items[y][x], DP[y][x-1][k])
                if k == 1:
                    DP[y][x][k] = max(DP[y][x][k], max(DP[y-1][x]) + Items[y][x])

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

Submission status: Time Limit Exceeded

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

v_dict = dict()
for _ in range(k):
    y, x, v = map(int, input().split())
    v_dict[(y, x)] = v

for y in range(1, r+1):
    for x in range(1, c+1):
        p = (y, x)
        prev_y_max = max(dp[y-1][x][:])
        dp[y][x][0] = max(prev_y_max, dp[y][x-1][0])
        if p in v_dict:
            v = v_dict[p]
            dp[y][x][1] = max(prev_y_max + v, dp[y][x-1][0] + v, dp[y][x-1][1])
            dp[y][x][2] = max(dp[y][x-1][1] + v, dp[y][x-1][2])
            dp[y][x][3] = max(dp[y][x-1][2] + v, dp[y][x-1][3])
        else:
            for i in range(1, 4):
                dp[y][x][i] = dp[y][x-1][i]
print(max(dp[r][c]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

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

tb = np.zeros((R+1,C+1),dtype=np.int32)
dp = np.zeros((C+1,4),dtype=np.int64)
prev = np.zeros(C+1,dtype=np.int64)

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

for i in range(1,R+1):
    for j in range(1,C+1):
        for k in range(4):
            dp[j][k] = dp[j-1][k]
            if k > 0:
                dp[j][k] = max(dp[j][k],dp[j-1][k-1]+tb[i][j])
            if k == 0:
                dp[j][k] = max(dp[j][k],prev[j])
            elif k == 1:
                dp[j][k] = max(dp[j][k],prev[j]+tb[i][j])
    prev = np.max(dp,axis=1)

print(prev[C])

Submission status: Time Limit Exceeded

Solution code:
import sys
from pprint import pprint

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

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

for i in range(1,R+1):
    cell=[0]*4
    for j in range(C):
        cell[0]=max(cell[0],grid[i-1][j])
        for k in range(2,-1,-1):
            cell[k+1]=max(cell[k+1],cell[k]+grid[i][j])
        grid[i][j]=max(cell)

print(grid[-1][-1])

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=[[0 for j in range(c+1)] for k in range(r+1)]
dp_1=[[0 for j in range(c+1)] for k in range(r+1)]
dp_2=[[0 for j in range(c+1)] for k in range(r+1)]
dp_3=[[0 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):
    dp_1[i][j]=max(dp_3[i-1][j]+rck[i][j],dp_0[i][j-1]+rck[i][j],dp_0[i][j],dp_1[i][j-1])
    dp_2[i][j]=max(dp_3[i-1][j]+rck[i][j],dp_1[i][j-1]+rck[i][j],dp_1[i][j],dp_2[i][j-1])
    dp_3[i][j]=max(dp_3[i-1][j]+rck[i][j],dp_2[i][j-1]+rck[i][j],dp_2[i][j],dp_3[i][j-1])
    
    
    
    
    #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][k-1],dp[i][j-1][k])
    #print(dp[i][j])
print(dp_3[r][c])

Submission status: Time 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]*5 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][1]=dp[rr][cc-1][1]
				dp[rr][cc][2]=dp[rr][cc-1][2]
				dp[rr][cc][3]=dp[rr][cc-1][3]
				dp[rr][cc][4]=max(dp[rr][cc-1][4],dp[rr-1][cc][4])
			else:
				dp[rr][cc][1]=max(dp[rr-1][cc][4]+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-1][2]+G[rr][cc],dp[rr][cc-1][3])
				dp[rr][cc][4]=max(dp[rr][cc][1:4])
	#print(dp)
	print(dp[-1][-1][-1])















if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

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

for i in range(k):
    x,y,z = map(int,stdin.readline().split())
    x -= 1
    y -= 1
    v[x][y] = z

dp = [[[0] * 4 for j in range(c+1)] for s in range(r+1)]

for i in range(r):
    dp1 = dp[i]
    dp2 = dp[i + 1]
    for j in range(c):
        dp2[j + 1][0] = dp2[j][0]
        for s in range(1,4):
            dp2[j + 1][0] = max(dp2[j + 1][0],dp1[j + 1][s])
        for s in range(4):
            dp2[j + 1][1] = max([dp2[j + 1][1],dp2[j][1],dp1[j + 1][s] + v[i][j]])
        for s in range(2,4):
            dp2[j + 1][s] = max([dp2[j + 1][s],dp2[j][s],dp2[j][s-1] + v[i][j]])

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

Submission status: Time 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]*5 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]=max(dp[rr][cc-1][0],dp[rr-1][cc][4])
				dp[rr][cc][1]=dp[rr][cc-1][1]
				dp[rr][cc][2]=dp[rr][cc-1][2]
				dp[rr][cc][3]=dp[rr][cc-1][3]
				dp[rr][cc][4]=max(dp[rr][cc][:4])
			else:
				dp[rr][cc][0]=max(dp[rr][cc-1][0],dp[rr-1][cc][4])
				dp[rr][cc][1]=max(dp[rr][cc][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-1][2]+G[rr][cc],dp[rr][cc-1][3])
				dp[rr][cc][4]=max(dp[rr][cc][1:4])
	#print(dp)
	print(dp[-1][-1][-1])















if __name__ == "__main__":
	main()

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):
    ri,ci,vi = map(int,input().split())
    v[ri-1][ci-1] = vi

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


if v[0][0]:
    dp[0][0][1] = v[0][0]

for h in range(1,r):
    dp[h][0][0] = max(dp[h-1][0])
    if v[h][0]:
        dp[h][0][1] = dp[h][0][0] + v[h][0]

for w in range(1,c):
    for i in range(4):
        dp[0][w][i] = dp[0][w-1][i]
    if v[0][w]:
        for i in range(3,0,-1):
            dp[0][w][i] = max(dp[0][w][i],dp[0][w][i-1] + v[0][w])

for h in range(1,r):
    for w in range(1,c):
        for cnt in range(4):
            dp[h][w][cnt] = max(dp[h][w-1][cnt],max(dp[h-1][w]))
        if v[h][w]:
            for cnt in range(3,0,-1):
                dp[h][w][cnt] = max(dp[h][w][cnt],dp[h][w][cnt-1] + v[h][w])

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

def main():
    R, C, K = map(int, input().split())
    D = {}
    for _ in [None] * K:
        r, c, v = map(int, input().split())
        D[(r - 1, c - 1)] = v

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

    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[i][r][c] > -1:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                    if c < C - 1:
                        dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                    if i < 3 and v:
                        if r < R - 1:
                            dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                        if c < C - 1:
                            dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v)

    v = D.get((R - 1, C - 1), 0)
    ans = dp[3][R - 1][C - 1]
    for i in range(3):
        ans = max(ans, dp[i][R - 1][C - 1] + v)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

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

for i in range(k):
    x,y,z = map(int,stdin.readline().split())
    x -= 1
    y -= 1
    v[x][y] = z

dp = [[[0] * 4 for j in range(c+1)] for s in range(r+1)]

for i in range(r):
    for j in range(c):
        dp[i + 1][j + 1][0] = dp[i + 1][j][0]
        for s in range(1,4):
            dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0],dp[i][j + 1][s])
        for s in range(4):
            dp[i + 1][j + 1][1] = max([dp[i + 1][j + 1][1],dp[i+1][j][1],dp[i][j + 1][s] + v[i][j]])
        for s in range(2,4):
            dp[i + 1][j + 1][s] = max([dp[i + 1][j + 1][s],dp[i + 1][j][s],dp[i + 1][j][s-1] + v[i][j]])

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

Submission status: Time Limit Exceeded

Solution code:
def main(R,C,K,V):
    dp = [[[0,0,0,0] 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):
            for n in range(4):
                if n!=0:
                    if i != 0 and n==1:
                        dp[i][j][n] = max(dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])+V[i][j]
                    if j != 0:
                        dp[i][j][n] = max(dp[i][j][n],dp[i][j-1][n],dp[i][j-1][n-1]+V[i][j])
                if n==0:
                    if i != 0:
                        dp[i][j][n] = max(dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])

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

if __name__ == '__main__':
    R,C,K = map(int,input().split())
    rcv = [list(map(int,input().split())) for _ in range(K)]
    V = [[0]*C for _ in range(R)]
    for i in range(K):
        r,c,v = rcv[i]
        r -= 1
        c -= 1
        V[r][c] = v
    main(R,C,K,V)

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]*4 for _ in range(C+2)] for _ in range(R+2)]
 
for i in range(R+1):
	dpi = dp[i]
	dpi2 = dp[i+1]
	for j in range(C+1):
		for m in range(4):
			ni, nj = i, j+1
			dpi[nj][m] = max(dpi[nj][m], dpi[j][m])
			if m < 3:
				dpi[nj][m+1] = max(dpi[nj][m+1], dpi[j][m]+I[ni][nj])
			ni, nj = i+1, j
			dpi2[nj][0] = max(dpi2[nj][0], dpi[j][m])
			dpi2[nj][1] = max(dpi2[nj][1], dpi[j][m]+I[ni][nj])
ans = 0
for m in range(4):
	ans = max(ans, dp[R][C][m])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    (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] * C for _ in [None] * R] for _ in [None] * 4]

    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[i][r][c] > -1:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                    if c < C - 1:
                        dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                    if i < 3 and v:
                        if r < R - 1:
                            dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                        if c < C - 1:
                            dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v)

    v = D.get((R - 1, C - 1), 0)
    ans = dp[3][R - 1][C - 1]
    for i in range(3):
        ans = max(ans, dp[i][R - 1][C - 1] + v)
    print(ans)

if __name__ == "__main__":
    main()

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] * C for _ in [None] * R] for _ in [None] * 4]

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[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v)

v = D.get((R - 1, C - 1), 0)
for i in range(3):
    dp[i][R - 1][C - 1] += v
print(max(dp[i][R - 1][C - 1] for i in range(4)))

Submission status: Time Limit Exceeded

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    item = [[0 for _ in range(C)] for r in range(R)]
    for _ in range(K):
        r, c, k = map(int, input().split())
        item[r-1][c-1] = k
    
    DP = [[[-1 for _ in range(4)] for r in range(C)] for i in range(R)]
    DP[0][0][0] = 0
    for r in range(R):
        for c in range(C):
            for i in range(4):
                if DP[r][c][i] > -1:
                    if r + 1 < R: 
                        DP[r+1][c][0] = max(DP[r+1][c][0], DP[r][c][i])
                    if c + 1 < C: 
                        DP[r][c+1][i] = max(DP[r][c+1][i], DP[r][c][i])
                    if i < 3 and item[r][c] > 0:
                        if r + 1 < R: DP[r+1][c][0] = max(DP[r+1][c][0], DP[r][c][i] + item[r][c])
                        if c + 1 < C: DP[r][c+1][i+1] = max(DP[r][c+1][i+1], DP[r][c][i] + item[r][c])

    ans = DP[R-1][C-1][3]
    for i in range(3): ans = max(ans, DP[R-1][C-1][i] + item[R-1][C-1])
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def main():
    R, C, K = map(int, input().split())
    D = [[*map(int, input().split())] for _ in [None] * K]
    D = {(r - 1, c - 1) : v for r, c, v in D}

    dp = [[[-1] * 4 for _ in [None] * C] for _ in [None] * 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)

    v = D.get((R - 1, C - 1), 0)
    ans = dp[R - 1][C - 1][3]
    for i in range(3):
        ans = max(ans, dp[R - 1][C - 1][i] + v)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    (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 [None] * C] for _ in [None] * 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)

    v = D.get((R - 1, C - 1), 0)
    ans = dp[R - 1][C - 1][3]
    for i in range(3):
        ans = max(ans, dp[R - 1][C - 1][i] + v)
    print(ans)

if __name__ == "__main__":
    main()

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 [None] * C] for _ in [None] * 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)

v = D.get((R - 1, C - 1), 0)
ans = dp[R - 1][C - 1][3]
for i in range(3):
    ans = max(ans, dp[R - 1][C - 1][i] + v)
print(ans)

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)

import numpy as np

R, C, K = rl()
#I = [[0]*(C+2) for _ in range(R+2)]
I = np.zeros((R+2, C+2), dtype=np.int)
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)]
dp = np.zeros((4,R+2,C+2), dtype=np.int)

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:
#!/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 [None] * C] for _ in [None] * 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)

v = D.get((R - 1, C - 1), 0)
for i in range(3):
    dp[R - 1][C - 1][i] += v
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())
    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 g[i-1][j-1]>0:
                    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:
import sys
input = sys.stdin.buffer.readline
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)))