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
*data, = map(int, sys.stdin.read().split()[::-1])
def inp():
    global data
    return data.pop()
R, C, k = inp(), inp(), inp()
grid = [[0] * C for _ in range(R)]

for _ in range(k):
    ri, ci, v = inp(), inp(), inp()
    grid[ri - 1][ci - 1] = v


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


mx = 0
for row in range(1, R + 1):
    for col in range(1, C + 1):
        level = 0
        while level < 4:
            dp[row][col][0] = max(dp[row][col][0], dp[row - 1][col][level])
            dp[row][col][1] = max(dp[row][col][1], dp[row - 1][col][level] + grid[row - 1][col - 1])
            level += 1
        level = 1
        while level < 4:
            dp[row][col][level] = max(
                dp[row][col][level],
                dp[row][col - 1][level - 1] + grid[row - 1][col - 1],
                dp[row][col - 1][level]
            )
            mx = max(mx, dp[row][col][level])
            level += 1

print(mx)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
(r,c,_),*d = [list(map(int, s.split())) for s in open(0)]

dp = np.zeros([r+1,4], dtype=int)
d = sorted(d, key=lambda x:(x[1], x[0]))
for i, _, v in d:
    dp[i,0] = max(dp[i,0], np.max(dp[:i]))
    dp[i,1:] = np.maximum(dp[i,1:], dp[i,:-1] + v)
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

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


dp = [[[0 for i in range(3 + 1)] for c in range(C + 1)] for r in range(R + 1)]
dp2 = [[0 for c in range(C + 1)] for r in range(R + 1)]
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):
            best = dp[r][c - 1][numTakenThisRow]  # came from left
            if not numTakenThisRow:
                # came from top, didn't take curr
                best = max(best, dp2[r - 1][c])
            else:
                # came from left, took curr
                best = max(best, dp[r][c - 1][numTakenThisRow - 1] + grid[r][c])
                # came from top, took curr
                if numTakenThisRow == 1:
                    best = max(best, dp2[r - 1][c] + grid[r][c])
            dp[r][c][numTakenThisRow] = best
        dp2[r][c] = max(dp[r][c][i] for i in range(3 + 1))
print(dp2[R - 1][C - 1])

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    dp = [[[0]*4 for _ in range(C+1)] for _ 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]
            dp[i+1][j+1][1] = dp[i+1][j][1]
            dp[i+1][j+1][2] = dp[i+1][j][2]
            dp[i+1][j+1][3] = dp[i+1][j][3]
            v = max(dp[i][j+1])
            dp[i+1][j+1][0] = max(dp[i+1][j+1][0], v)
            if (i, j) in a:
                u = a[(i, j)]
                dp[i+1][j+1][1] = max(dp[i+1][j+1][1],  dp[i+1][j][0] + u,  v + u)
                dp[i+1][j+1][2] = max(dp[i+1][j+1][2], dp[i+1][j][1] + u )
                dp[i+1][j+1][3] = max(dp[i+1][j+1][3], dp[i+1][j][2] + u )

    return max(dp[R][C])

if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    dp = [[[0]*4 for _ in range(C+1)] for _ 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]
            dp[i+1][j+1][1] = dp[i+1][j][1]
            dp[i+1][j+1][2] = dp[i+1][j][2]
            dp[i+1][j+1][3] = dp[i+1][j][3]
            v = max(dp[i][j+1])
            dp[i+1][j+1][0] = max(dp[i+1][j+1][0], v)
            if (i, j) in a:
                u = a[(i, j)]
                dp[i+1][j+1][1] = max([dp[i+1][j+1][1],  dp[i+1][j][0] + u,  v + u])
                dp[i+1][j+1][2] = max([dp[i+1][j+1][2], dp[i+1][j][1] + u ])
                dp[i+1][j+1][3] = max([dp[i+1][j+1][3], dp[i+1][j][2] + u ])

    return max(dp[R][C])

if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
R,C,k=map(int,input().split())
rcv = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(k):
  r,c,v = map(int,input().split())
  rcv[r][c] = v
# dp: 各場所までの合計の最大値
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
dp[1][1][1] = rcv[1][1]
#print(dp[1][1][1])
for r in range(1,R+1):
  for c in range(1,C+1):
    dp[r][c][1] = max(dp[r][c][1],dp[r][c-1][0]+rcv[r][c],dp[r][c-1][1])
    dp[r][c][2] = max(dp[r][c-1][1]+rcv[r][c],dp[r][c-1][2])
    dp[r][c][3] = max(dp[r][c-1][2]+rcv[r][c],dp[r][c-1][3])
    
    if r < R:
      dp[r+1][c][0] = max(dp[r][c])
      dp[r+1][c][1] = max(dp[r][c])+rcv[r+1][c]
print(max(dp[R][C]))
#print(*dp)

Submission status: Time Limit Exceeded

Solution code:
# pypy
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from collections import defaultdict
import gc

R, C, K = map(int, readline().split())
rcv = list(map(int, read().split()))
rr = rcv[::3]
cc = rcv[1::3]
vv = rcv[2::3]
rc2v = defaultdict(int)
for _r, _c, _v in zip(rr, cc, vv):
    rc2v[(_r-1, _c-1)] = _v

del rcv, rr, cc, vv
gc.collect()


dp = [[[0]*4 for c in range(C)] for r in range(R)]
dp[0][0][1] = rc2v[(0, 0)]

for c in range(1, C):
    for k in range(1, 4):
        dp[0][c][k] = max(dp[0][c-1][k], dp[0][c-1][k-1]+rc2v[(0, c)])

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

for r in range(1, R):
    for c in range(1, C):
        dp[r][c][0] = max(dp[r][c-1][0], max(dp[r-1][c]))
        for k in range(1, 4):
            dp[r][c][k] = max(dp[r][c-1][k], dp[r][c-1][k-1] + rc2v[(r, c)])
            
        dp[r][c][1] = max(dp[r][c][1], max(dp[r-1][c]) + rc2v[(r, c)])

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

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    return max(dp[R][C])

if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

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

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for r in range(1, R + 1):
    for c in range(1, C + 1):
        m = max(dp[r - 1][c])
        for n in range(4):
            dp[r][c][n] = max(m,
                              dp[r][c - 1][n])
        if table[r][c] is not None:
            for n in range(3):
                dp[r][c][n + 1] = max(dp[r][c][n + 1],
                                      m + table[r][c],
                                      dp[r][c - 1][n] + table[r][c])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import io
import os

input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

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


dp = [[[0 for i in range(3 + 1)] for c in range(C + 1)] for r in range(R + 1)]
for r in range(R):
    for c in range(C):
        for numTakenThisRow in range(3 + 1):

            best = dp[r][c - 1][numTakenThisRow]  # came from left
            if not numTakenThisRow:
                # came from top, didn't take curr
                for i in range(3 + 1):
                    best = max(best, dp[r - 1][c][i])
            else:
                # came from left, took curr
                best = max(best, dp[r][c - 1][numTakenThisRow - 1] + grid[r][c])
                # came from top, took curr
                if numTakenThisRow == 1:
                    for i in range(3 + 1):
                        best = max(best, dp[r - 1][c][i] + grid[r][c])
            dp[r][c][numTakenThisRow] = best
print(max(dp[R - 1][C - 1][i] for i in range(3 + 1)))

Submission status: Time Limit Exceeded

Solution code:
import sys; input = sys.stdin.buffer.readline
INF = float("inf")

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	R, C, K = getlist()
	items = [0] * (R * C)
	for i in range(K):
		r, c, v = getlist()
		r -= 1; c -= 1
		items[r * C + c] = v

	DP = [-INF] * (4 * R * C)

	# DP = [[[-INF] * 4 for i in range(C)] for j in range(R)]
	# 初期
	DP[0] = 0
	DP[1] = items[0]

	for r in range(1, R):
		DP[4 * C * r] = max(DP[4 * C * (r - 1)], DP[4 * C * (r - 1) + 1])
		DP[4 * C * r + 1] = max(DP[4 * C * (r - 1) + 1], DP[4 * C * (r - 1)]) + items[r * C]

	# for r in range(1, R):
	# 	DP[r][0][0] = max(DP[r - 1][0][0], DP[r - 1][0][1])
	# 	DP[r][0][1] = max(DP[r - 1][0][1], DP[r - 1][0][0]) + items[r][0]

	for c in range(1, C):
		DP[4 * c] = DP[4 * (c - 1)]
		for itr in range(1, 4):
			DP[4 * c + itr] = max(DP[4 * (c - 1) + itr - 1] + items[c], DP[4 * (c - 1) + itr])

	# for c in range(1, C):
	# 	DP[0][c][0] = DP[0][c - 1][0]
	# 	for itr in range(1, 4):
	# 		DP[0][c][itr] = max(DP[0][c - 1][itr - 1] + items[0][c], DP[0][c - 1][itr])

	for r in range(1, R):
		for c in range(1, C):
			ind = 4 * C * (r - 1) + 4 * c
			prev = max(DP[ind], DP[ind + 1], DP[ind + 2], DP[ind + 3])
			val = items[r * C + c]
			DP[4 * C * r + 4 * c] = max(DP[4 * C * r + 4 * (c - 1)], prev)
			DP[4 * C * r + 4 * c + 1] = max(max(DP[4 * C * r + 4 * (c - 1)], prev) + val, DP[4 * C * r + 4 * (c - 1) + 1])
			for itr in range(2, 4):
				DP[4 * C * r + 4 * c + itr] = max(DP[4 * C * r + 4 * (c - 1) + itr - 1] + val, DP[4 * C * r + 4 * (c - 1) + itr])


	# for r in range(1, R):
	# 	for c in range(1, C):
	# 		prev = max(DP[r - 1][c])
	# 		val = items[r][c]
	# 		DP[r][c][0] = max(DP[r][c - 1][0], prev)
	# 		DP[r][c][1] = max(max(DP[r][c - 1][0], prev) + val, DP[r][c - 1][1])
	# 		for itr in range(2, 4):
	# 			DP[r][c][itr] =  max(DP[r][c - 1][itr - 1] + val, DP[r][c - 1][itr])


	ans = max(DP[-1], DP[-2], DP[-3], DP[-4])
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

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

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


# dp = [[[0 for j in range(R+1)] for i in range(C+1)]

# print(rck)
dp = [0] * (C+1)
for r in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    V[r-1].sort()
    for c, v in V[r-1]:
        tmp0 = max(dp[c], tmp0)
        tmp1 = max(dp[c], tmp1)
        tmp2 = max(dp[c], tmp2)
        tmp3 = max(dp[c], tmp3)

        tmp3 = max(tmp3, tmp2+v)
        tmp2 = max(tmp2, tmp1+v)
        tmp1 = max(tmp1, tmp0+v)

        dp[c] = max(tmp0, tmp1, tmp2, tmp3)
    for i in range(1, C+1):
        dp[i] = max(dp[i], dp[i-1])
    # print(dp)

print(dp[-1])

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]*c for i in range(4)]

# DP[j][k] : kはその行での個数
# R*C*4
# maxをinlineにしている。
max_tops = [0]*c
for i in range(r):
    for j in range(c):
        # 初期化
        for k in range(4):
            DP[k][j] = DP[k][j-1] if j!=0 else 0
        # 上から降ってくるやつの最大
        max_top = max_tops[j]
        if (i,j) in d:
            v = d[(i,j)]
            # 上からの場合
            if DP[0][j] < max_top:
                DP[0][j] = max_top
            if DP[1][j] < max_top + v:
                DP[1][j] = max_top + v
            for k in range(1, 4):
                # 左から来るときだけ考える
                if j != 0:
                    if k!=0 and DP[k][j] < DP[k-1][j-1] + v:
                        DP[k][j] =  DP[k-1][j-1] + v
        else:
            if DP[0][j] < max_top:
                DP[0][j] = max_top
        max_tops[j] = max(DP[k][j] for k in range(4))
ans = max(DP[i][-1] for i in range(4))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def solve():
  R, C, K = map(int, input().split())
  item = [[0]*C for _ in range(R)]
  for i in range(K):
    r,c,v = map(int, input().split())
    item[r-1][c-1] = v
  dp = [[[0]*5 for _ in range(C+1)] for _ in range(R+1)]
  for i in range(1,R+1):
    for j in range(1,C+1):
      for k in range(4): #横から取らない
        dp[i][j][k] = dp[i][j-1][k]
      #上から取らない
      dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][-1])
      if item[i-1][j-1]>0:
        #上からとる
        dp[i][j][1] = max(dp[i][j][1],dp[i-1][j][-1]+item[i-1][j-1])
        for k in range(1,4): #横から取る
          dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+item[i-1][j-1])
      dp[i][j][4] = max(dp[i][j][:4])
  ans = max(dp[-1][-1])
  return ans
print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
import collections
D=collections.defaultdict(int)
r,c,k=map(int,input().split())
for _ in range(k):
	ri,ci,v=map(int,input().split())
	ri-=1
	ci-=1
	D[ri*10**5+ci]=v

DP0=[[0]*(c+1) for i in range(r+1)]
DP1=[[0]*(c+1) for i in range(r+1)]
DP2=[[0]*(c+1) for i in range(r+1)]
DP3=[[0]*(c+1) for i in range(r+1)]
for i in range(r+1):
	for j in range(c+1):
		v=D[i*10**5+j]
		if v!=0:
			DP1[i][j]=max(DP0[i-1][j],DP1[i-1][j],DP2[i-1][j],DP3[i-1][j])+v

		DP0[i][j]=max(DP0[i][j],DP0[i-1][j],DP1[i-1][j],DP2[i-1][j],DP3[i-1][j])
		if v!=0:
			DP1[i][j]=max(DP1[i][j],DP0[i-1][j]+v,DP1[i-1][j]+v,DP2[i-1][j]+v,DP3[i-1][j]+v)
		DP0[i][j]=max(DP0[i][j],DP0[i][j-1])
		DP1[i][j]=max(DP1[i][j],DP1[i][j-1])
		DP2[i][j]=max(DP2[i][j],DP2[i][j-1])
		DP3[i][j]=max(DP3[i][j],DP3[i][j-1])
		if v!=0:
			DP1[i][j]=max(DP1[i][j],DP0[i][j-1]+v)
			DP2[i][j]=max(DP2[i][j],DP1[i][j-1]+v)
			DP3[i][j]=max(DP3[i][j],DP2[i][j-1]+v)
		
ans=max(DP0[r-1][c-1],DP1[r-1][c-1],DP2[r-1][c-1],DP3[r-1][c-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():

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

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

    for i in range(R):
        for j in range(C):
            v = max(dp[i][j+1])
            for k in range(4):
                dp[i+1][j+1][k] = dp[i+1][j][k]
            dp[i+1][j+1][0] = max(dp[i+1][j+1][0], v)
            if (i, j) in a:
                dp[i+1][j+1][1] = max([dp[i+1][j+1][1],  dp[i+1][j][0] + a[(i, j)],  v + a[(i, j)]])
                dp[i+1][j+1][2] = max([dp[i+1][j+1][2], dp[i+1][j][1] + a[(i, j)] ])
                dp[i+1][j+1][3] = max([dp[i+1][j+1][3], dp[i+1][j][2] + a[(i, j)] ])

    return max(dp[R][C])


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

R,C,K = map(int,input().split())
DP = []
Gift = []
for i in range(R):
    DP.append([])
    Gift.append([])
    for _ in range(C):
        DP[i].append([0,0,0,0])
        Gift[i].append(0)
for _ in range(K):
    r,c,v = map(int,input().split())
    Gift[r-1][c-1] = v

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
R,C,K = map(int,input().split())
dp = [0 for _ in range(3001)]
ddc = defaultdict(lambda:0)

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

for j in range(1, 3001):
	sol = []
	for i in range(1, 3001):
		dp[i] = max(dp[i-1], dp[i])
		if ddc[(j, i)] >= 1:
			targ = ddc[(j, i)]
			#print(len(sol))
			if len(sol) < 3:
				dp[i] += targ
				sol.append(targ)
			else:
				if min(sol) < targ:
					#print(sol)
					dp[i] += targ-min(sol)
					sol.remove(min(sol))
					sol.append(targ)

print(dp[3000])

Submission status: Time Limit Exceeded

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

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

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for r in range(1, R + 1):
    for c in range(1, C + 1):
        m=max(dp[r-1][c])
        for n in range(4):
            dp[r][c][n] = max(m,
                              dp[r][c - 1][n])
        if (r, c) in D:
            for n in range(3):
                dp[r][c][n + 1] = max(dp[r][c][n + 1],
                                      m + D[(r, c)],
                                      dp[r][c - 1][n] + D[(r, c)])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
(r,c,_),*d = [list(map(int, s.split())) for s in open(0)]

dp = np.zeros([r,4], dtype=int)
d = sorted(d, key=lambda x:(x[1], x[0]))
for j, _, v in d:
    j -= 1
    dp[j,1:] = np.maximum(dp[j,1:], dp[j,:-1] + v)
    dp[j+1:,0] = np.maximum(dp[j+1:,0], np.max(dp[j,1:]))
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
LI = lambda: list(map(int, input().split()))

R, C, K = LI()
rcv = [LI() for _ in range(K)]

INF = 10 ** 16


def main():
    d = {(r, c): v for r, c, v in rcv}
    dp = [[[-INF] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
    dp[0][1][0] = dp[1][0][0] = 0
    for r in range(1, R + 1):
        for c in range(1, C + 1):
            for k in range(4):
                # 左から取らない
                x = dp[r][c - 1][k]
                if k == 0:
                    # 上から取らない
                    x = max(x, max(dp[r - 1][c]))
                if k > 0 and (r, c) in d:
                    v = d[(r, c)]
                    # 左から取る
                    x = max(x, dp[r][c - 1][k - 1] + v)
                    if k == 1:
                        # 上から取る
                        x = max(x, max(dp[r - 1][c]) + v)
                dp[r][c][k] = x
    ans = max(dp[-1][-1])
    print(ans)
                    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


def solve():
    memo = [[[0] * 4 for _ in range(C+1)] for _ in range(R+1)]
    for r in range(1,R+1):
        for c in range(1,C+1):
            v = RC[r-1][c-1]
            memo[r][c][0] = max(memo[r-1][c][3], memo[r][c-1][0])
            memo[r][c][1] = max(memo[r][c][0] + v, memo[r][c-1][1])
            memo[r][c][2] = max(memo[r][c][1], memo[r][c-1][1] + v, memo[r][c-1][2])
            memo[r][c][3] = max(memo[r][c][2], memo[r][c-1][2] + v, memo[r][c-1][3])
    return memo[-1][-1][3]

if __name__ == "__main__":
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
r,c,k=map(int,input().split())
item=[[0]*c for i in range(r)]
for i in range(k):
    x,y,z=map(int,input().split())
    item[x-1][y-1]=z
dp=[[[0,0,0,0] for j in range(c)] for i in range(r)]
if item[0][0]>0:
    dp[0][0]=[0,item[0][0],0,0]
for i in range(r):
    for j in range(c):
        for l in range(4):
            if l<3:
                #した行く分にはOK
                if i!=r-1:
                    dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][l])
                    if item[i+1][j]>0:
                        dp[i+1][j][1]=max(dp[i+1][j][0],dp[i][j][l])+item[i+1][j]
                #右は？
                if j!=c-1:
                    dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j][l])
                    if item[i][j+1]>0:
                        dp[i][j+1][l+1]=max(dp[i][j+1][l+1],dp[i][j][l]+item[i][j+1])
            else:
                #した行く分にはOK
                if i!=r-1:
                    dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][l])
                    if item[i+1][j]>0:
                        dp[i+1][j][1]=max(dp[i+1][j][0],dp[i][j][l])+item[i+1][j]
                #右は？
                if j!=c-1:
                    dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j][l])
print(max(dp[r-1][c-1]))

Submission status: Time Limit Exceeded

Solution code:
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

R,C,K = map(int,input().split())
DP = []
Gift = []
for i in range(R):
    DP.append([])
    Gift.append([])
    for _ in range(C):
        DP[i].append([0,0,0,0])
        Gift[i].append(0)
for _ in range(K):
    r,c,v = map(int,input().split())
    Gift[r-1][c-1] = v

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

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
D = dict()
for _ in range(K):
    r, c, v = map(int, input().split())
    D[(r, c)] = v

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for r in range(1, R + 1):
    for c in range(1, C + 1):
        m=max(dp[r-1][c])
        for n in range(4):
            dp[r][c][n] = max(m,
                              dp[r][c - 1][n])
        if (r, c) in D:
            for n in range(3):
                dp[r][c][n + 1] = max(dp[r][c][n + 1],
                                      m + D[(r, c)],
                                      dp[r][c - 1][n] + D[(r, c)])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

items = [[0] * (C + 1) for _ in range(R + 1)]
for r, c, v in X:
    items[r][c] = v

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for i in range(1, R + 1):
    for j in range(1, C + 1):
        # k=0, horizontal and vertical transitions are allowed
        dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][0], dp[i - 1][j][1],
                          dp[i - 1][j][2], dp[i - 1][j][3])

        # k=1~3, horizontal transition and picking up are allowed
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + items[i][j],
                          dp[i][j][2] + items[i][j])

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

        dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][0] + items[i][j],
                          dp[i][j][0] + items[i][j])

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

Submission status: Time Limit Exceeded

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

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

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

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

for r in range(1,R+1):
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP[r-1][c][2]+item,DP[r][c-1][0])
    DP2 = max(DP1,DP[r][c-1][1],DP[r][c-1][0]+item)
    DP3 = max(DP2,DP[r][c-1][2],DP[r][c-1][1]+item)
    DP[r][c][0] = DP1
    DP[r][c][1] = DP2
    DP[r][c][2] = DP3
        
print(DP[R][C][2])

Submission status: Time Limit Exceeded

Solution code:

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

items = [[0] * (C + 1) for _ in range(R + 1)]
for r, c, v in X:
    items[r][c] = v

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for i in range(1, R + 1):
    for j in range(1, C + 1):
        # k=0, horizontal and vertical transitions are allowed
        dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][0], dp[i - 1][j][1],
                          dp[i - 1][j][2], dp[i - 1][j][3])

        # k=1~3, horizontal transition and picking up are allowed
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + items[i][j],
                          dp[i][j][2] + items[i][j])

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

        dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][0] + items[i][j],
                          dp[i][j][0] + items[i][j])

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
D = dict()
for _ in range(K):
    r, c, v = map(int, input().split())
    D[(r, c)] = v

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for r in range(1, R + 1):
    for c in range(1, C + 1):
        for n in range(4):
            dp[r][c][n] = max(max(dp[r - 1][c]),
                              dp[r][c - 1][n])
        if (r, c) in D:
            for n in range(3):
                dp[r][c][n + 1] = max(dp[r][c][n + 1],
                                      max(dp[r - 1][c]) + D[(r, c)],
                                      dp[r][c - 1][n] + D[(r, c)])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 6)


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


'''
    def best3(r, c1, c2):  # c1 inclusive, c2 exclusive
        return sum(sorted(grid[r][c1:c2], reverse=True)[:3])

    @lru_cache(maxsize=None)
    def dp(r, c):  # inclusive
        if r < 0 or c < 0:
            return 0
        best = 0
        for c1 in range(c + 1):
            best = max(best, dp(r - 1, c1) + best3(r, c1, c + 1))
        return best

    print(dp(R - 1, C - 1))

'''
inf = float("inf")

@lru_cache(maxsize=None)
def dp(r, c, numTakenThisRow):
    if numTakenThisRow < 0:
        return -inf
    if r < 0 or c < 0:
        return 0

    best = max(
        dp(r, c - 1, numTakenThisRow),
        dp(r, c - 1, numTakenThisRow - 1) + grid[r][c],
    )
    if numTakenThisRow == 0:
        best = max(best, max(dp(r - 1, c, i) for i in range(3 + 1)),)
    if numTakenThisRow == 1:
        best = max(best, max(dp(r - 1, c, i) + grid[r][c] for i in range(3 + 1)),)

    return best

print(max(dp(R - 1, C - 1, i) for i in range(3 + 1)))