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 numpy as np

r,c,k=[int(x) for x in input().split()]
cl=[[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    x,y,z=[int(x) for x in input().split()]
    cl[x][y]=z
dp=np.zeros((r+1,c+1,4),dtype=np.int64)
for i in range(1,r+1):
    for j in range(1,c+1):
        #0,1,2でそれぞれ最大化を考える
        for k in range(4):
            #前の行のk=3か、前の列からスライド
            if k==0:
                dp[i][j][k]=max(dp[i-1][j][3],dp[i][j-1][0])
            #k-1か前の列からスライド
            elif k==1:
                dp[i][j][k]=max(dp[i][j][0]+cl[i][j],dp[i][j-1][1])
            elif k==2:
                dp[i][j][k]=max(dp[i][j-1][1]+cl[i][j],dp[i][j-1][2],dp[i][j][1])
            elif k==3:
                dp[i][j][k]=max(dp[i][j-1][2]+cl[i][j],dp[i][j-1][3],dp[i][j][2])
print(dp[r][c][3])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    R, C, K = NMI()
    item = {(0,0):0}

    for i in range(K):
        r_, c_, v_, = NMI()
        item[(r_ - 1,c_ - 1)] = v_

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

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

                if k == 3:
                    dp[0][i + 1][j] = max(a, dp[0][i + 1][j])
                    continue
                dp[0][i + 1][j] = max(a, a + v, dp[0][i + 1][j])
                dp[k][i][j + 1] = max(a, dp[k][i][j + 1])
                dp[k + 1][i][j + 1] = max(a + v, dp[k + 1][i][j + 1])

    print(max(dp[0][R][C], dp[1][R][C], dp[2][R][C], dp[3][R][C]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

R, C, K = MI()
item = {(0,0):0}

for i in range(K):
    r_, c_, v_, = MI()
    item[(r_ - 1,c_ - 1)] = v_

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

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

            if k == 3:
                dp[0][i + 1][j] = max(a, dp[0][i + 1][j])
                continue
            dp[0][i + 1][j] = max(a, a + v, dp[0][i + 1][j])
            dp[k][i][j + 1] = max(a, dp[k][i][j + 1])
            dp[k + 1][i][j + 1] = max(a + v, dp[k + 1][i][j + 1])

print(max(dp[0][R][C], dp[1][R][C], dp[2][R][C], dp[3][R][C]))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]
def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)

    for i in range(K):
        r_, c_, v_, = NMI()
        grid[r_ - 1][c_ - 1] = v_

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

    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                a = dp[i][j][k]
                try:
                    v = grid[i][j]
                except:
                    v = 0

                if k == 3:
                    dp[i + 1][j][0] = max(a, dp[i + 1][j][0])
                    continue
                dp[i + 1][j][0] = max(a, a + v, dp[i + 1][j][0])
                dp[i][j + 1][k] = max(a, dp[i][j + 1][k])
                dp[i][j + 1][k + 1] = max(a + v, dp[i][j + 1][k + 1])

    print(max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]
def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)

    for i in range(K):
        r_, c_, v_, = NMI()
        grid[r_ - 1][c_ - 1] = v_

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

    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                try:
                    v = grid[i][j]
                except:
                    v = 0

                if k == 3:
                    dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                    continue
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
                dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
                dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

    print(max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    R, C, K = NMI()
    item = {(0,0):0}

    for i in range(K):
        r_, c_, v_, = NMI()
        item[(r_ - 1,c_ - 1)] = v_

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

    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                if (i,j) in item:
                    v = item[(i,j)]
                else:
                    v = 0

                if k == 3:
                    dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                    continue
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
                dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
                dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

    print(max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    R, C, K = NMI()
    item = {(0,0):0}

    for i in range(K):
        r_, c_, v_, = NMI()
        item[(r_ - 1,c_ - 1)] = v_

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

    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                if (i,j) in item:
                    v = item[(i,j)]
                else:
                    v = 0

                if k == 3:
                    dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                    continue
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
                dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
                dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

    ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]
def main():
    R, C, K = MI()
    grid = make_grid_int(R, C, 0)

    for i in range(K):
        r_, c_, v_, = MI()
        grid[r_ - 1][c_ - 1] = v_


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

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

    ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    R, C, K = MI()
    item = [[0] * (C + 1) for i in range(R + 1)]

    for i in range(K):
        r_, c_, v_, = MI()
        item[r_ - 1][c_ - 1] = v_

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

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

    ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    R, C, K = MI()
    item = [[0] * (C + 1) for i in range(R + 1)]

    for i in range(K):
        r_, c_, v_, = MI()
        item[r_ - 1][c_ - 1] = v_

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

    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                v = item[i][j]

                if k == 3:
                    dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                    continue
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
                dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
                dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

    ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

R, C, K = MI()
item = [[0] * (C + 1) for i in range(R + 1)]

for i in range(K):
    r_, c_, v_, = MI()
    item[r_ - 1][c_ - 1] = v_

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

for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            v = item[i][j]

            if k == 3:
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                continue
            dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
            dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
            dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import pprint

r,c,k=[int(x) for x in input().split()]
cl=[[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    x,y,z=[int(x) for x in input().split()]
    cl[x][y]=z
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):
        #0,1,2でそれぞれ最大化を考える
        for k in range(4):
            if k==0:
                dp[i][j][k]=max(dp[i-1][j][3],dp[i][j-1][0])
            elif k==1:
                dp[i][j][k]=max(dp[i][j][0]+cl[i][j],dp[i][j-1][1])
            elif k==2:
                dp[i][j][k]=max(dp[i][j-1][1]+cl[i][j],dp[i][j-1][2],dp[i][j][1])
            elif k==3:
                dp[i][j][k]=max(dp[i][j-1][2]+cl[i][j],dp[i][j-1][3],dp[i][j][2])
print(dp[r][c][3])

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

R, C, K = MI()
item = {(0,0):0}

for i in range(K):
    r_, c_, v_, = MI()
    item[(r_ - 1,c_ - 1)] = v_

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

for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            if (i,j) in item:
                v = item[(i,j)]
            else:
                v = 0

            if k == 3:
                dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0])
                continue
            dp[i + 1][j][0] = max(dp[i][j][k], dp[i][j][k] + v, dp[i + 1][j][0])
            dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k])
            dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1])

ans = max(dp[R][C][0], dp[R][C][1], dp[R][C][2], dp[R][C][3])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
import numpy as np

R, C, K = map(int, input().split())
a = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    a[r-1][c-1] = v
#DP = [[[0 for _ in range(4)]  for _ in range(C+1)] for _ in range(R+1)]
DP = np.zeros((R+1, C+1, 4), np.int64)
@jit
def solve():
    for i in range(R):
        for j in range(C):
            # 拾う遷移
            # kが2から下がるようにしないとダメ
            for k in range(2, -1, -1):
                DP[i][j][k+1] = max(DP[i][j][k+1], DP[i][j][k] + a[i][j])
            # 移動の遷移
            for k in range(4):
                if i + 1 < R:
                    DP[i+1][j][0] = max(DP[i+1][j][0], DP[i][j][k])
                if j + 1 < C:
                    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)
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3
import sys

def main():
    s = sys.stdin.readlines()
    R, C, K = map(int, s[0].split())
    V =[[0] * C for _ in range(R)]
    for e in s[1:K+1]:
        r, c, v = map(int, e.split())
        V[r-1][c-1] = v
    dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
    for i in range(R):
        for j in range(C):
            for k in range(2, -1, -1):
                dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k] + V[i][j])
            for k in range(4):
                if i+1 < R:
                    dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
                if j+1 < C:
                    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)

main()

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
a = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    a[r-1][c-1] = 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):
        # 拾う遷移
        # kが2から下がるようにしないとダメ
        for k in range(2, -1, -1):
            DP[i][j][k+1] = max(DP[i][j][k+1], DP[i][j][k] + a[i][j])
        # 移動の遷移
        for k in range(4):
            if i + 1 < R:
                DP[i+1][j][0] = max(DP[i+1][j][0], DP[i][j][k])
            if j + 1 < C:
                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
 
def main():
    R, C, K = map(int, input().split())
    V =[[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        V[r-1][c-1] = v
    dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
    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] + V[i][j])
            for k in range(4):
                if i+1 < R:
                    dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
                if j+1 < C:
                    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)
 
main()

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
a = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    a[r-1][c-1] = 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):
        # 拾う遷移
        # kが2から下がるようにしないとダメ
        for k in range(2, -1, -1):
            DP[i][j][k+1] = max(DP[i][j][k+1], DP[i][j][k] + a[i][j])
        # 移動の遷移
        for k in range(4):
            DP[i+1][j][0] = max(DP[i+1][j][0], DP[i][j][k])
            DP[i][j+1][k] = max(DP[i][j+1][k], DP[i][j][k])
ans = 0
for k in range(4):
    ans = max(ans, DP[R-1][C-1][k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3
import sys

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

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    input = sys.stdin.readline
    R, C, k = map(int, input().split())
    s = [[0] * (C + 1) for i in range(R + 1)]
    dp = [[[0] * 4 for i in range(C + 1)] for j in range(R + 1)]
    for i in range(k):
        r, c, v = map(int, input().split())
        s[r][c] = v
    for i in range(R):
        for j in range(C):
            m = max(dp[i][j + 1][0], dp[i][j + 1][1], dp[i][j + 1][2], dp[i][j + 1][3])
            for k in range(4):
                if k == 0:
                    dp[i + 1][j + 1][k] = max(dp[i + 1][j][k], m)
                else:
                    dp[i + 1][j + 1][k] = max(dp[i + 1][j][k], dp[i + 1][j][k - 1] + s[i + 1][j + 1])
            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], s[i + 1][j + 1] + m)
    print(max(dp[R][C]))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
r,c,k=map(int, input().split())
v={}

for _ in range(k):
    ri,ci,vi=map(int, input().split())
    v[(ri-1,ci-1)]=vi
    
dp=[[0]*4 for _ in range(c+1)]
for j in range(c):
    if (0,j) in v:
        for t in range(1,4):
            dp[j][t]=max(dp[j-1][t-1]+v[(0,j)],dp[j-1][t])
    else:
        for t in range(1,4):
            dp[j][t]=dp[j-1][t]
import copy
for i in range(1,r):
    dp2=[[dp[x][3] for _ in range(4)] if (i,x) not in v else [dp[x][3]]+[dp[x][3]+v[(i,x)] for _ in range(3)] for x in range(c+1)]
    for j in range(c):
        if (i,j) in v:
            for t in range(1,4):
                dp2[j][t]=max(dp2[j-1][t-1]+v[(i,j)],dp2[j-1][t],dp2[j][t])
        else:
            for t in range(1,4):
                dp2[j][t]=max(dp2[j-1][t],dp2[j][t])
    dp=dp2

print(max(dp[-2]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import jit

@jit
def main():
    R, C, K = map(int, input().split())
    scores = [[0] * (C + 1) for _ in range(R+1)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        scores[r][c] = v
    dp = [[[0] *4 for _ in range(C+1)] for _ in range(R+1)]

    scores = np.array(scores)
    dp = np.array(dp)

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

    answer = dp[R][C][3]
    print(answer)

main()

Submission status: Time Limit Exceeded

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

    answer = dp[R][C][3]
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

answer = dp[R][C][3]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=lambda:sys.stdin.readline().rstrip()
r,c,k=map(int,input().split())
dp=[[(c+1)*[0]for _ in range(r+1)]for _ in range(4)]
p=[c*[0]for _ in range(r)]
for _ in range(k):
  rr,cc,u=map(int,input().split())
  rr-=1
  cc-=1
  p[rr][cc]=u
ans=0
for j in range(1,r+1):
  for k in range(1,c+1):
    dp[0][j][k]=max(dp[0][j-1][k],dp[0][j][k-1])
    for i in range(1,4):
      dp[i][j][k]=max(dp[i][j][k],dp[0][j-1][k],dp[1][j-1][k],dp[2][j-1][k],dp[3][j-1][k],dp[i][j][k-1])
      if p[j-1][k-1]:
        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-1]+p[j-1][k-1],max(dp[0][j-1][k],dp[1][j-1][k],dp[2][j-1][k],dp[3][j-1][k])+p[j-1][k-1])
      ans=max(ans,dp[i][j][k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=lambda:sys.stdin.readline().rstrip()
r,c,k=map(int,input().split())
dp=[[(c+1)*[0]for _ in range(r+1)]for _ in range(4)]
p=[c*[0]for _ in range(r)]
for _ in range(k):
  rr,cc,u=map(int,input().split())
  rr-=1
  cc-=1
  p[rr][cc]=u
ans=0
for j in range(1,r+1):
  for k in range(1,c+1):
    dp[0][j][k]=max(dp[0][j-1][k],dp[0][j][k-1])
    for i in range(1,4):
      dp[i][j][k]=max(dp[i][j][k],dp[0][j-1][k],dp[1][j-1][k],dp[2][j-1][k],dp[3][j-1][k],dp[i][j][k-1])
      if p[j-1][k-1]:
        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-1]+p[j-1][k-1],max(dp[0][j-1][k],dp[1][j-1][k],dp[2][j-1][k],dp[3][j-1][k])+p[j-1][k-1])
print(max(max(max(j)for j in i)for i in dp))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

R, C, K = map(int,input().split())
MAP = np.zeros((R, C), dtype=np.int64)
DP = np.zeros((R+1, C+1, 4), dtype=np.int64)

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

for i in range(R):
    for j in range(C):
        tmp = DP[i][j+1][3]
        DP[i+1][j+1] = DP[i+1][j]
        DP[i+1][j+1][DP[i+1][j+1] < tmp] = tmp
        v = MAP[i][j]
        if v == 0:
            continue
        for k in range(2,-1,-1):
            DP[i+1][j+1][k+1] = max(DP[i+1][j+1][k + 1], DP[i+1][j+1][k] + v)

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]

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

for i in range(R):
    for j in range(C):
        DP[i+1][j+1][0] = max(DP[i+1][j+1][0], max(DP[i][j+1]))
        for k in range(4):
            DP[i+1][j+1][k] = max(DP[i+1][j+1][k], DP[i+1][j][k])
        if MAP[i][j] == 0:
            continue
        DP[i+1][j+1][1] = max(DP[i+1][j+1][1], max(DP[i][j+1]) + MAP[i][j])
        for k in range(3):
            DP[i+1][j+1][k+1] = max(DP[i+1][j+1][k+1], DP[i+1][j][k] + MAP[i][j])

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

R, C, K = map(int,input().split())
MAP = np.zeros((R, C), dtype=np.int64)
DP = np.zeros((R+1, C+1, 4), dtype=np.int64)

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

for i in range(R):
    for j in range(C):
        DP[i+1][j+1][0] = max(DP[i+1][j+1][0], DP[i][j+1].max())
        DP[i+1][j+1] = np.amax([DP[i+1][j+1], DP[i+1][j]], axis=0)
        if MAP[i][j] == 0:
            continue
        DP[i+1][j+1][1] = max(DP[i+1][j+1][1], DP[i][j+1].max() + MAP[i][j])
        DP[i+1][j+1][1:] = np.amax([DP[i+1][j+1][1:], DP[i+1][j][:3] + MAP[i][j]], axis=0)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

R, C, K = map(int,input().split())
MAP = np.zeros((R, C), dtype=np.int64)
DP = np.zeros((R+1, C+1, 4), dtype=np.int64)

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

for i in range(R):
    for j in range(C):
        DP[i+1][j+1][0] = max(DP[i+1][j+1][0], DP[i][j+1].max())
        DP[i+1][j+1][1] = max(DP[i+1][j+1][1], DP[i][j+1].max() + MAP[i][j])
        DP[i+1][j+1] = np.amax([DP[i+1][j+1], DP[i+1][j]], axis=0)
        DP[i+1][j+1][1:] = np.amax([DP[i+1][j+1][1:], DP[i+1][j][:3] + MAP[i][j]], axis=0)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from copy import deepcopy
from collections import defaultdict
def input(): return sys.stdin.readline().rstrip()


def main():
    R, C, K = map(int, input().split())
    items = defaultdict(list)
    for _ in range(K):
        r, c, v = map(int, input().split())
        items[r-1].append([c-1, v])
    dp = np.zeros(C,dtype=int)
    for RR in range(R):
        dptmp1 = deepcopy(dp)
        dptmp2 = deepcopy(dp)
        dptmp3 = deepcopy(dp)
        items[RR].sort(key=lambda x:x[0])
        for item in items[RR]:
            dptmp3[item[0]:] = np.clip(
                dptmp3[item[0]:], dptmp2[item[0]]+item[1], None)
            dptmp2[item[0]:] = np.clip(
                dptmp2[item[0]:], dptmp1[item[0]]+item[1], None)
            dptmp1[item[0]:] = np.clip(
                dptmp1[item[0]:], dp[item[0]]+item[1], None)
        dp=deepcopy(dptmp3)
    print(dp[-1])
            



if __name__ == '__main__':
    main()