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 defaultdict
import sys


def input(): return sys.stdin.readline().rstrip()


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

dp = [{(i, j): 0 for i in range(r+1) for j in range(c+1)} for _ in range(4)]

# dp[N][i][j] := (i,j) にN個目でたどりつく方法
d = {(i, j): 0 for i in range(r+1) for j in range(c+1)}

for _ in range(K):
    r_, c_, v = map(int, input().split())
    d[(r_, c_)] += v
for i in range(1, r+1):
    for j in range(1, c+1):
        v = d[(i, j)]
        maxi = max(dp[k][(i-1, j)] for k in range(4))
        if v > 0:
            dp[0][(i, j)] = max(maxi, dp[0][(i, j-1)])
            dp[1][(i, j)] = max(dp[1][(i, j-1)], dp[0][(i, j-1)]+v, maxi+v)
            dp[2][(i, j)] = max(dp[2][(i, j-1)], dp[1][(i, j-1)]+v)
            dp[3][(i, j)] = max(dp[3][(i, j-1)], dp[2][(i, j-1)]+v)

        elif v == 0:
            dp[0][(i, j)] = max(dp[0][(i, j-1)], maxi)
            dp[1][(i, j)] = dp[1][(i, j-1)]
            dp[2][(i, j)] = dp[2][(i, j-1)]
            dp[3][(i, j)] = dp[3][(i, j-1)]


ans = max(dp[k][(r, c)] for k in range(4))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys


def input(): return sys.stdin.readline().rstrip()


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

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

# dp[N][i][j] := (i,j) にN個目でたどりつく方法
d = {(i, j): 0 for i in range(r+1) for j in range(c+1)}

for _ in range(K):
    r_, c_, v = map(int, input().split())
    d[(r_, c_)] += v
for i in range(1, r+1):
    for j in range(1, c+1):
        v = d[(i, j)]
        if v > 0:
            maxi = max(dp[k][i-1][j] for k in range(4))
            dp[0][i][j] = max(maxi, dp[0][i][j-1])
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+v, maxi+v)
            dp[2][i][j] = max(dp[2][i][j-1], dp[1][i][j-1]+v)
            dp[3][i][j] = max(dp[3][i][j-1], dp[2][i][j-1]+v)

        elif v == 0:
            maxi = max(dp[k][i-1][j] for k in range(4))
            dp[0][i][j] = max(dp[0][i][j-1], maxi)
            dp[1][i][j] = dp[1][i][j-1]
            dp[2][i][j] = dp[2][i][j-1]
            dp[3][i][j] = dp[3][i][j-1]


ans = max(dp[k][r][c] for k in range(4))

print(ans)

Submission status: Time Limit Exceeded

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

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

grids = [[0]*C for _ in range(R)]

for r,c,v in rcv:
    grids[r-1][c-1] = v

from collections import defaultdict
dp = defaultdict(int)

for r in range(R):
    for c in range(C):
        value = grids[r][c]
        for i in range(1,4):
            dp[r,c,i] = dp[r,c-1,i]
            if i == 1:
                dp[r,c,i] = max(dp[r,c,i], max(dp[r-1,c,j] for j in range(1,4)) + value)
            dp[r,c,i] = max(dp[r,c,i], dp[r,c-1,i-1] + value)

print(max(dp[R-1,C-1,j] for j in range(4)))

Submission status: Time Limit Exceeded

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

def main():
    R, C, K = map(int, readline().rstrip().split())
    A = [[0] * (C + 2) for _ in range(R + 2)]
    DP = [[[0] * 4 for _ in range(C + 2)] for _ in range(R + 2)]
    for _ in range(K):
        r, c, v = map(int, readline().rstrip().split())
        A[r][c] = v
    for r in range(1, R + 1):
        for c in range(1, C + 1):
            for k in range(2, -1, -1):
                DP[r][c][k + 1] = max(DP[r][c][k + 1], DP[r][c][k] + A[r][c])
            for k in range(4):
                DP[r + 1][c][0] = max(DP[r + 1][c][0], DP[r][c][k])
                DP[r][c + 1][k] = max(DP[r][c + 1][k], DP[r][c][k])
    ans = 0
    for k in range(4):
        ans = max(DP[R][C][k], ans)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
(R,C,K),*rcv = [list(map(int, s.split())) for s in open(0)]

grids = [[0]*C for _ in range(R)]

for r,c,v in rcv:
    grids[r-1][c-1] = v

from collections import defaultdict
dp = defaultdict(int)

for r in range(R):
    for c in range(C):
        value = grids[r][c]
        for i in range(1,4):
            dp[r,c,i] = dp[r,c-1,i]
            if i == 1:
                dp[r,c,i] = max(dp[r,c,i], max(dp[r-1,c,j] for j in range(1,4)) + value)
            dp[r,c,i] = max(dp[r,c,i], dp[r,c-1,i-1] + value)

print(max(dp[R-1,C-1,j] for j in range(4)))

Submission status: Time Limit Exceeded

Solution code:
# R, C: Y, H
H, W, K = map(int, input().split())
RCV = [list(map(int, input().split())) for _ in range(K)]
# H, W, K = 3000, 3000, 2*10**5
# RCV = [[i//W, i%W, i] for i in range(K)]

# print("pre")

item = [[0]*(W+1) for _ in range(H+1)]
for r, c, v in RCV:
    item[r][c] = v
# for item_i in item:
#     print(item_i)

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

# 3000**2*4*2 + 2*10**5*2*4
for y in range(1, H+1):
    for x in range(1, W+1):
        for k in range(4):
            dp[y][x][k] = dp[y][x-1][k]
        for k in range(4):
            dp[y][x][0] = max(
                dp[y-1][x][k],
                dp[y][x][0]
            )
        if item[y][x] == 0:
            continue
        for k in range(1, 4):
            dp[y][x][k] = max(
                dp[y][x-1][k-1] + item[y][x],
                dp[y][x][k]
            )
        for k in range(4):
            dp[y][x][1] = max(
                dp[y-1][x][k] + item[y][x],
                dp[y][x][1]
            )
# for dp_i in dp:
#     print(dp_i)

print(max(dp[H][W]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from copy import deepcopy
    input = sys.stdin.readline
    R, C, K = [int(x) for x in input().strip().split()]
    G = [[0] * (C+1) for r in range(R+1)]
    for k in range(K):
        r, c, v = [int(x) for x in input().strip().split()]
        G[r][c] = v
    # for r in range(R+1):
    #     print(G[r])
    ans = [[[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):
            ans[r][c][0] = max(ans[r-1][c][-1], ans[r][c-1][0])
            for i in range(1, 4):
                ans[r][c][i] = max(ans[r-1][c][3], ans[r][c-1][i], ans[r-1][c][3]+G[r][c], ans[r][c-1][i-1]+G[r][c])

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from copy import deepcopy
    input = sys.stdin.readline
    R, C, K = [int(x) for x in input().strip().split()]
    G = [[0] * (C+1) for r in range(R+1)]
    for k in range(K):
        r, c, v = [int(x) for x in input().strip().split()]
        G[r][c] = v
    # for r in range(R+1):
    #     print(G[r])
    prev = [[0] * 4 for _ in range(C+1)]
    for r in range(1, R+1):
        cur = [[0] * 4 for _ in range(C+1)]
        for c in range(1, C+1):
            cur[c][0] = max(prev[c][0], cur[c-1][0])
            for i in range(1, 4):
                cur[c][i] = max(prev[c][i], cur[c-1][i], prev[c][3]+G[r][c], cur[c-1][i-1]+G[r][c])
        prev = deepcopy(cur)
        # print(prev)

    print(max(cur[-1]))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
from collections import deque

sys.setrecursionlimit(10**6)
input = sys.stdin.readline
#read = sys.stdin.buffer.read

def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP0 = copy.deepcopy(field)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(DP0[i][j-1] + field[i][j], DP1[i][j-1])
        DP0[i][j] = DP0[i][j-1]

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(DP0[i][j-1] + field[i][j], DP1[i][j-1], max(DP0[i-1][j], DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])
            DP0[i][j] = max(DP0[i][j-1], DP0[i-1][j], DP1[i-1][j], DP2[i-1][j], DP3[i-1][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1], DP0[R-1][C-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

dp = []

for y in range(R):
    nowdp = []
    dp.append(nowdp)
    for x in range(C):
        tmp = []
        uemax = 0
        if y > 0:
            uemax = max(dp[y-1][x])
        if vss[y][x] is None:
            if x > 0:
                tmp.append(max(dp[y][x-1][0], uemax))
                tmp.append(dp[y][x-1][1])
                tmp.append(dp[y][x-1][2])
                tmp.append(dp[y][x-1][3])
            else:
                tmp.append(uemax)
                tmp.append(0)
                tmp.append(0)
                tmp.append(0)
        else:
            v = vss[y][x]
            if x > 0:
                tmp.append(max(dp[y][x-1][0], uemax))
                tmp.append(max(dp[y][x-1][0]+v, dp[y][x-1][1], uemax+v))
                tmp.append(max(dp[y][x-1][1]+v, dp[y][x-1][2]))
                tmp.append(max(dp[y][x-1][2]+v, dp[y][x-1][3]))
            else:
                tmp.append(uemax)
                tmp.append(uemax + v)
                tmp.append(0)
                tmp.append(0)
        nowdp.append(tmp)

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
R, C, K = map(int, input().split())
vss = [[None]*C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    vss[r-1][c-1] = v

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

for y in range(R):
    for x in range(C):
        uemax = 0
        if y > 0:
            uemax = max(dp[y-1][x])
        if vss[y][x] is None:
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = dp[y][x-1][1]
                dp[y][x][2] = dp[y][x-1][2]
                dp[y][x][3] = dp[y][x-1][3]
            else:
                dp[y][x][0] = uemax
                dp[y][x][1] = 0
                dp[y][x][2] = 0
                dp[y][x][3] = 0
        else:
            v = vss[y][x]
            if x > 0:
                dp[y][x][0] = max(dp[y][x-1][0], uemax)
                dp[y][x][1] = max(dp[y][x-1][0]+v, dp[y][x-1][1], uemax+v)
                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:
                dp[y][x][0] = uemax
                dp[y][x][1] = uemax + v
                dp[y][x][2] = 0
                dp[y][x][3] = 0

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

Submission status: Time Limit Exceeded

Solution code:
it = lambda: list(map(int, input().strip().split()))
INF = float('inf')


def solve():
    R, C, K = it()
    grid = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = it()
        grid[r - 1][c - 1] = v
    
    dp = [[[-INF] * 4 for _ in range(C + 1)] for _ in range(R + 1)]

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


if __name__ == '__main__':
    ans = solve()
    print(ans)

Submission status: Time Limit Exceeded

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

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

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

for row in range(1, R+1):
    for col in range(1, C+1):
        for i in range(4):
            dp[row][col][0] = max(dp[row][col][0], dp[row-1][col][i])
            dp[row][col][1] = max(dp[row][col][1], dp[row-1][col][i]+m[row][col])
            dp[row][col][i] = max(dp[row][col][i], dp[row][col-1][i])
            if i >= 1:
                dp[row][col][i] = max(dp[row][col][i], dp[row][col-1][i-1]+m[row][col])
print(max(dp[R][C]))

Submission status: Time Limit Exceeded

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

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

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

for row in range(1, R+1):
    for col in range(1, C+1):
        for i in range(4):
            dp[row][col][0] = max(dp[row][col][0], dp[row-1][col][i])
            dp[row][col][1] = max(dp[row][col][1], dp[row-1][col][i]+m[row][col])
            dp[row][col][i] = max(dp[row][col][i], dp[row][col-1][i])
            if i >= 1:
                dp[row][col][i] = max(dp[row][col][i], dp[row][col-1][i-1]+m[row][col])
print(max(dp[R][C]))

Submission status: Time Limit Exceeded

Solution code:
R, C, K = list(map(int, input().split()))
value = [[0]*C for _ in range(R)]
# 価値テーブルの作成
for i in range(K):
    r, c, v = list(map(int, input().split()))
    value[r-1][c-1] = v
# dp<n>[行][列] = その列でn個取ったときの価値の最大値
dp0 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
# print(dp0)
for r in range(1, R+1):
    for c in range(1, C+1):
        # print(r, c)
        v = value[r-1][c-1]
        dp0[r][c] = max(dp0[r-1][c], dp1[r-1][c], dp2[r-1][c], dp3[r-1][c], dp0[r][c-1])
        dp1[r][c] = max(dp1[r][c-1], dp0[r-1][c] + v, dp1[r-1][c] + v,dp2[r-1][c] + v, dp3[r-1][c] + v, dp0[r][c-1] + v)
        dp2[r][c] = max(dp2[r][c-1], dp1[r][c-1] + v)
        dp3[r][c] = max(dp3[r][c-1], dp2[r][c-1] + v)
print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

@jit
def solver(R, C, K, value):
    # dp[行][列][その列で取った個数] = 価値の最大値
    dp = [[[0]*4 for _ in range(C+1)] for _ in range(R+1)]
    for r in range(R):
        for c in range(C):
            # アイテムを拾った個数と価値の対応を更新
            for i in range(3, 0, -1):
                dp[r][c][i] = max(dp[r][c][i], dp[r][c][i-1] + value[r][c])
            # 移動先に反映
            for k in range(4):
                dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
                dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
    return max(dp[R-1][C-1])


R, C, K = list(map(int, input().split()))
value = [[0]*C for _ in range(R)]
# 価値テーブルの作成
for i in range(K):
    r, c, v = list(map(int, input().split()))
    value[r-1][c-1] = v

print(solver(R, C, K, value))

Submission status: Time Limit Exceeded

Solution code:
import sys

R, C, K = map(int, input().split())
item = [[0]*C for _ in range(R)]
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    item[r-1][c-1] = v

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

DP[0][0][1] = item[0][0]

for i in range(R):
    for j in range(C):
        for k in range(4):
            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])+item[i][j])  # 取る

            if j > 0:  # 前の列から
                DP[i][j][k] = max(DP[i][j][k], DP[i][j-1][k])  # 取らない
                if k >= 1:
                    DP[i][j][k] = max(DP[i][j][k], DP[i][j-1][k-1]+item[i][j])  # 取る

print(max(DP[R-1][C-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, 0, 0, 0] for _ in range(C)] for _ in range(R)]

DP[0][0][1] = item[0][0]

for i in range(R):
    for j in range(C):
        for k in range(4):
            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])+item[i][j])  # 取る

            if j > 0:  # 前の列から
                DP[i][j][k] = max(DP[i][j][k], DP[i][j-1][k])  # 取らない
                if k >= 1:
                    DP[i][j][k] = max(DP[i][j][k], DP[i][j-1][k-1]+item[i][j])  # 取る

print(max(DP[R-1][C-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, 0, 0, 0] for _ in range(C)] for _ in range(R)]

for i in range(R):
    for j in range(C):
        if i == 0 and j == 0:
            if item[i][j]:
                DP[i][j][1] = item[i][j]

        elif i == 0:
            if item[i][j]:
                DP[i][j][1] = max(DP[i][j-1][0]+item[i][j], DP[i][j-1][1])
                DP[i][j][2] = max(DP[i][j-1][1]+item[i][j], DP[i][j-1][2])
                DP[i][j][3] = max(DP[i][j-1][2]+item[i][j], DP[i][j-1][3])
            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]

        elif j == 0:
            if item[i][j]:
                DP[i][j][1] = max(DP[i-1][j]) + item[i][j]
            else:
                DP[i][j][0] = max(DP[i - 1][j])

        else:
            if item[i][j]:
                DP[i][j][0] = max(DP[i][j-1][0], max(DP[i-1][j]))
                DP[i][j][1] = max(DP[i][j-1][0]+item[i][j], DP[i][j-1][1], max(DP[i-1][j])+item[i][j])
                DP[i][j][2] = max(DP[i][j-1][1]+item[i][j], DP[i][j-1][2])
                DP[i][j][3] = max(DP[i][j-1][2]+item[i][j], DP[i][j-1][3])
            else:
                DP[i][j][0] = max(DP[i][j-1][0], max(DP[i-1][j]))
                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-1][C-1]))

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

r,c,k = (int(x) for x in input().split())
reward = dict()
for _ in xrange(k):
    x,y,v = (int(x) for x in input().split())
    reward[x-1,y-1] = v

dp = [[[0 for _ in xrange(4)] for _ in xrange(c+1)] for _ in xrange(r+1)]
# print(dp[0][0][0])
for row in xrange(r):
    for col in xrange(c):
        v = reward.get((row,col),0)
        for item in xrange(4):
            if item == 0:
                continue
            else:
                dp[row+1][col+1][item] = max(dp[row+1][col][item-1] + v, dp[row+1][col][item])
                if item == 1:
                    dp[row+1][col+1][item] = max(dp[row+1][col+1][item], max(dp[row][col+1]) + v)

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

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

r,c,k = (int(x) for x in input().split())
reward = dict()
for _ in xrange(k):
    x,y,v = (int(x) for x in input().split())
    reward[x-1,y-1] = v

dp = [[[0 for _ in xrange(4)] for _ in xrange(c+1)] for _ in xrange(r+1)]
# print(dp[0][0][0])
for row in xrange(r):
    for col in xrange(c):
        for item in xrange(4):
            if item == 0:
                dp[row+1][col+1][item] = max(dp[row+1][col][item], max((dp[row][col+1][ii] for ii in xrange(4))))
            else:
                dp[row+1][col+1][item] = max(dp[row+1][col][item-1] + reward.get((row,col),0), dp[row+1][col][item])
                if item == 1:
                    dp[row+1][col+1][item] = max(dp[row+1][col+1][item], max((dp[row][col+1][ii] for ii in xrange(4))) + reward.get((row,col),0))

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = list(map(int, input().split()))
value = [[0]*C for _ in range(R)]
# 価値テーブルの作成
for i in range(K):
    r, c, v = list(map(int, input().split()))
    value[r-1][c-1] = v
# dp[行][列][その列で取った個数] = 価値の最大値
dp = [[[0]*4 for _ in range(C+1)] for _ in range(R+1)]
for r in range(R):
    for c in range(C):
        # アイテムを拾った個数と価値の対応を更新
        for i in range(3, 0, -1):
            dp[r][c][i] = max(dp[r][c][i], dp[r][c][i-1] + value[r][c])
        # 移動先に反映
        for k in range(4):
            dp[r+1][c][0] = max(dp[r+1][c][0], dp[r][c][k])
            dp[r][c+1][k] = max(dp[r][c+1][k], dp[r][c][k])
print(max(dp[R-1][C-1]))

Submission status: Time Limit Exceeded

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

def main():
    r,c,k=map(int,input().split())
    a=np.zeros((r,c),int)
    for _ in range(k):
        R,C,V=map(int,input().split())
        a[R-1][C-1]=V
    dp=np.zeros((r,c,4),int)
    dp[0][0][1]=a[0][0]
    for i in range(r):
        for j in range(c):
            for k in range(4):
                if i>0:#上
                    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]+a[i][j])
                if j>0:#右
                    dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])
                    if k>0:
                        dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+a[i][j])
    print(max(dp[-1][-1]))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * (C+5) for _ in range(R + 5)] for _ in range(4)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        # 行移動
        dp[0][i][j] = max(dp[0][i][j], max(dp[k][i - 1][j] for k in range(4)))
        dp[1][i][j] = max(dp[1][i][j], max(dp[k][i - 1][j] for k in range(4)) + V[i][j])
        for k in range(4):
            # 列移動
            if k > 0:
                dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + V[i][j])
            dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
print(max(dp[k][R][C] for k in range(4)))

Submission status: Time Limit Exceeded

Solution code:
r,c,k=map(int,input().split())
a=[[0]*c for _ in range(r)]
for _ in range(k):
    R,C,V=map(int,input().split())
    a[R-1][C-1]=V
dp=[[[0,0,0,0] for _ in range(c)] for _ in range(r)]
dp[0][0][1]=a[0][0]
for i in range(r):
    for j in range(c):
        for k in range(4):
            if i>0:#上
                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]+a[i][j])
            if j>0:#右
                dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])
                if k>0:
                    dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+a[i][j])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
r,c,k=map(int,input().split())
a=[[0]*c for _ in range(r)]
for _ in range(k):
    R,C,V=map(int,input().split())
    a[R-1][C-1]=V
# dp=[[[0,0,0,0] for _ in range(c)] for _ in range(r)]
dp=np.zeros((r,c,4),int)
dp[0][0][1]=a[0][0]
for i in range(r):
    for j in range(c):
        for k in range(4):
            if i>0:#上
                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]+a[i][j])
            if j>0:#右
                dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k])
                if k>0:
                    dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+a[i][j])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * 4 for _ in range(C + 5)] for _ in range(R + 5)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        # 行移動
        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]) + V[i][j])
        for k in range(4):
            # 列移動
            if k > 0:
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k - 1] + V[i][j])
            dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
print(max(dp[R][C]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * 4 for _ in range(C + 5)] for _ in range(R + 5)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        # 行移動
        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]) + V[i][j])
        for k in range(4):
            # 列移動
            if k > 0:
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k - 1] + V[i][j])
            dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
print(max(dp[R][C]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * 4 for _ in range(C + 5)] for _ in range(R + 5)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        for k in range(4):
            # 行移動
            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])+V[i][j])
            # 列移動
            if k > 0:
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k - 1] + V[i][j])
            dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
print(max(dp[R][C]))

Submission status: Time Limit Exceeded

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

import numpy as np


def next_line():
    return input()


def next_int():
    return int(input())


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


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    r, c, k = map(int, input().split())
    dp = np.full((r+1, c+1, 4), 0)
    v = np.full((r+1, c+1), 0)
    for i in range(k):
        row, col, val = map(int, input().split())
        v[row][col] = val

    for row in range(1, r + 1):
        for col in range(1, c + 1):
            for num in range(0, 4):
                if num == 0:
                    dp[row][col][num] = dp[row-1][col][3]  # simple down
                else:
                    dp[row][col][num] = max(dp[row][col][num-1],  # str
                                            dp[row][col-1][num],  # right x
                                            dp[row-1][col][3] + \
                                            v[row][col],  # down o
                                            dp[row][col-1][num-1] + \
                                            v[row][col],  # right o
                                            )
    # print(v)
    # print(dp)
    print(dp[r][c][3])


if __name__ == '__main__':
    main()