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:
def run():
    r,c,k = map(int,input().split())
    t = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        a,b,z = map(int,input().split())
        t[a-1][b-1] = z
    dp = [[[0 for i in range(c)]for i in range(r)]for i in range(4)]
    dp[0][0][0] = 0
    dp[1][0][0] = t[0][0]
    dp[2][0][0] = t[0][0]
    dp[3][0][0] = t[0][0]
    for i in range(r-1):
        dp[0][i+1][0] = dp[1][i][0]
        for j in range(1,4):
            dp[j][i+1][0] = dp[1][i][0]+t[i+1][0]
    for i in range(c-1):
        dp[0][0][i+1] = 0
        for j in range(1,4):
            dp[j][0][i+1] = max(dp[1][0][i],t[0][i+1])
        for j in range(2,4):
            dp[j][0][i+1] = max(dp[2][0][i],dp[1][0][i]+t[0][i+1])
        dp[3][0][i+1] = max(dp[3][0][i],dp[2][0][i]+t[0][i+1])
    for i in range(r-1):
        for j in range(c-1):
            for s in range(0,4):
                dp[s][i+1][j+1] =  max(dp[0][i+1][j],dp[3][i][j+1])
            for s in range(1,4):
                dp[s][i+1][j+1] = max(dp[0][i+1][j]+t[i+1][j+1],dp[1][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            for s in range(2,4):
                dp[s][i+1][j+1] = max(dp[1][i+1][j]+t[i+1][j+1],dp[2][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            dp[3][i+1][j+1] = max(dp[2][i+1][j]+t[i+1][j+1],dp[3][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
    print(dp[3][r-1][c-1])

if __name__ == "__main__":
    run()

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 = defaultdict(int)
    data = [NMI() for _ in range(K)]
    for r, c, v in data:
        grid[(r-1)*3001 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 30010 + c * 10 + i

    dp = defaultdict(int)
    for rc in range(R*(C+1) + C+1):
        r = rc // (C+1)
        c = rc % (C+1)
        if r > R or c > C: continue

        for i in range(4):
            now_point = get_point(r, c, i)
            r_point = get_point(r+1, c, 0)
            c_point_plus = get_point(r, c+1, i+1)
            c_point_non = get_point(r, c+1, i)

            now = dp[now_point]
            val = grid[r*3001 + c]

            if i == 3:
                # 取らずに下
                dp[r_point] = max(now, dp[r_point])
                continue

            # 取って下
            dp[r_point] = max(now + val, dp[r_point])
            # 取らずに下
            dp[r_point] = max(now, dp[r_point])
            # 取って横
            dp[c_point_plus] = max(now + val, dp[c_point_plus])
            # 取らずに横
            dp[c_point_non] = max(now, dp[c_point_non])

    dp_RC = [dp[get_point(R, C, 0) + i] for i in range(4)]
    print(max(dp_RC))


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 = defaultdict(int)
    data = [NMI() for _ in range(K)]
    for r, c, v in data:
        grid[(r-1)*3001 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 30010 + c * 10 + i

    dp = defaultdict(int)
    for rc in range(R*3001 + C+1):
        r = rc // 3001
        c = rc % 3001
        if r > R or c > C: continue

        for i in range(4):
            now_point = get_point(r, c, i)
            r_point = get_point(r+1, c, 0)
            c_point_plus = get_point(r, c+1, i+1)
            c_point_non = get_point(r, c+1, i)

            now = dp[now_point]
            val = grid[r*3001 + c]

            if i == 3:
                # 取らずに下
                dp[r_point] = max(now, dp[r_point])
                continue

            # 取って下
            dp[r_point] = max(now + val, dp[r_point])
            # 取らずに下
            dp[r_point] = max(now, dp[r_point])
            # 取って横
            dp[c_point_plus] = max(now + val, dp[c_point_plus])
            # 取らずに横
            dp[c_point_non] = max(now, dp[c_point_non])

    dp_RC = [dp[get_point(R, C, 0) + i] for i in range(4)]
    print(max(dp_RC))


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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1)*3001 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 30010 + c * 10 + i

    dp = defaultdict(int)
    for rc in range(R*3001 + C+1):
        r = rc // 3001
        c = rc % 3001
        if r > R or c > C: continue

        for i in range(4):
            now_point = get_point(r, c, i)
            r_point = get_point(r+1, c, 0)
            c_point_plus = get_point(r, c+1, i+1)
            c_point_non = get_point(r, c+1, i)

            now = dp[now_point]
            val = grid[r*3001 + c]

            if i == 3:
                # 取らずに下
                dp[r_point] = max(now, dp[r_point])
                continue

            # 取って下
            dp[r_point] = max(now + val, dp[r_point])
            # 取らずに下
            dp[r_point] = max(now, dp[r_point])
            # 取って横
            dp[c_point_plus] = max(now + val, dp[c_point_plus])
            # 取らずに横
            dp[c_point_non] = max(now, dp[c_point_non])

    dp_RC = [dp[get_point(R, C, 0) + i] for i in range(4)]
    print(max(dp_RC))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from numba import jit


@jit
def main():

    R, C, K = map(int, sys.stdin.readline().rstrip().split())

    V = [[0 for _ in range(C)] for _ in range(R)]
    point = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    point[0][0][0] = 0
    for i in range(K):
        r, c, v = map(int, sys.stdin.readline().rstrip().split())
        V[r - 1][c - 1] = v

    # print(V)

    point[0][0][1] = V[0][0]
    point[0][0][0] = 0

    for r in range(R):
        for c in range(C):

            # 列方向に動く
            if c > 0:
                point[r][c][3] = max(point[r][c - 1][2] + V[r][c], point[r][c - 1][3])
                point[r][c][2] = max(point[r][c - 1][1] + V[r][c], point[r][c - 1][2])
                point[r][c][1] = max(point[r][c - 1][0] + V[r][c], point[r][c - 1][1])
                point[r][c][0] = point[r][c - 1][0]

            # 行方向に動く
            m = max(point[r - 1][c])
            if r > 0:
                point[r][c][1] = max(m + V[r][c], point[r][c][1])
                point[r][c][0] = m

    # print(point)
    print(max(point[R - 1][C - 1]))


main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():

    R, C, K = map(int, sys.stdin.readline().rstrip().split())

    V = [[0 for _ in range(C)] for _ in range(R)]
    point = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    point[0][0][0] = 0
    for i in range(K):
        r, c, v = map(int, sys.stdin.readline().rstrip().split())
        V[r - 1][c - 1] = v

    # print(V)

    point[0][0][1] = V[0][0]
    point[0][0][0] = 0

    for r in range(R):
        for c in range(C):

            # 列方向に動く
            if c > 0:
                point[r][c][3] = max(point[r][c - 1][2] + V[r][c], point[r][c - 1][3])
                point[r][c][2] = max(point[r][c - 1][1] + V[r][c], point[r][c - 1][2])
                point[r][c][1] = max(point[r][c - 1][0] + V[r][c], point[r][c - 1][1])
                point[r][c][0] = point[r][c - 1][0]

            # 行方向に動く
            m = max(point[r - 1][c])
            if r > 0:
                point[r][c][1] = max(m + V[r][c], point[r][c][1])
                point[r][c][0] = m

    # print(point)
    print(max(point[R - 1][C - 1]))


main()

Submission status: Time Limit Exceeded

Solution code:

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()

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

    dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                # 次の行に移動する場合
                if i + 1 <= R:
                    # 移動先のアイテムを取らない場合
                    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] + G[i + 1][j])
                # 次の列に移動する場合
                if j + 1 <= C:
                    # 移動先のアイテムを取らない場合
                    dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
                    # 現在のkが3未満のときのみ, 移動先のアイテムを取ることが可能
                    if k < 3:
                        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k] + G[i][j + 1])

    ans = 0
    for k in range(4):
        ans = max(ans, dp[-1][-1][k])
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():

    R, C, K = map(int, sys.stdin.readline().rstrip().split())

    V = [[0 for _ in range(C)] for _ in range(R)]
    point = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    point[0][0][0] = 0
    for i in range(K):
        r, c, v = map(int, sys.stdin.readline().rstrip().split())
        V[r - 1][c - 1] = v

    # print(V)

    point[0][0][1] = V[0][0]
    point[0][0][0] = 0

    for r in range(R):
        for c in range(C):

            # 列方向に動く
            if c > 0:
                point[r][c][3] = max(point[r][c - 1][2] + V[r][c], point[r][c - 1][3])
                point[r][c][2] = max(point[r][c - 1][1] + V[r][c], point[r][c - 1][2])
                point[r][c][1] = max(point[r][c - 1][0] + V[r][c], point[r][c - 1][1])
                point[r][c][0] = point[r][c - 1][0]

            # 行方向に動く
            m = max(point[r - 1][c])
            if r > 0:
                point[r][c][1] = max(m + V[r][c], point[r][c][1])
                point[r][c][0] = max(m, point[r][c][0])

    # print(point)
    print(max(point[R - 1][C - 1]))


main()

Submission status: Time Limit Exceeded

Solution code:

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

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

dp[0][0][1]=L[0][0]
dp[0][0][2]=L[0][0]
dp[0][0][3]=L[0][0]
#print(dp)
for h in range(R):
    for w in range(C):
        if h==0 and w==0:
            continue
        for k in range(4):
            if h==0:
                if k==0:
                    dp[h][w][k]=dp[h][w-1][k]
                else:
                    dp[h][w][k]=max(L[h][w]+dp[h][w-1][k-1], dp[h][w-1][k])

            elif w==0:
                if k==0:
                    dp[h][w][k]=dp[h-1][w][3]
                else:
                    dp[h][w][k]=max(L[h][w]+dp[h-1][w][3], dp[h-1][w][k])

            else:
                if k==0:
                    dp[h][w][k]=max(dp[h-1][w][3], dp[h][w-1][k])
                else:
                    dp[h][w][k]=max(L[h][w]+max(dp[h-1][w][3], dp[h][w-1][k-1]), dp[h][w-1][k])
            #print(dp[0][0], dp[0][1])
print(dp[R-1][C-1][3])
#print(dp)
#print(L)
#print(dp[0][0][1])

Submission status: Time Limit Exceeded

Solution code:

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

    dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                # 次の行に移動する場合
                if i + 1 <= R:
                    # 移動先のアイテムを取らない場合
                    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] + G[i + 1][j])
                # 次の列に移動する場合
                if j + 1 <= C:
                    # 移動先のアイテムを取らない場合
                    dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
                    # 現在のkが3未満のときのみ, 移動先のアイテムを取ることが可能
                    if k < 3:
                        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k] + G[i][j + 1])

    ans = 0
    for k in range(4):
        ans = max(ans, dp[-1][-1][k])
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def main():
    r, c, k = map(int, input().split())
    d = dict()
    for _ in range(k):
        t1, t2, t3 = map(int, input().split())
        d[(t1-1, t2-1)] = t3
    dp = [[0]*4 for _ in [0]*c]
    if (0, 0) in d:
        dp[0][1] = d[(0, 0)]
    for x in range(r):
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        if (x, y+1) in d:
                            dp[y+1][z+1] = max(dp[y+1]
                                               [z+1], dp[y][z]+d[(x, y+1)])
                    dp[y+1][z] = max(dp[y+1][z], dp[y][z])
            if x != r-1:
                if (x+1, y) in d:
                    dp2[y][1] = max(dp2[y][1],
                                    max(dp[y])+d[(x+1, y)])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        if x != r-1:
            dp = dp2
    print(max(dp[c-1]))


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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1)*3001 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 3001**2 + c * 3001 + i

    dp = defaultdict(int)
    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now_point = get_point(r, c, i)
                r_point = get_point(r+1, c, 0)
                c_point_plus = get_point(r, c+1, i+1)
                c_point_non = get_point(r, c+1, i)

                now = dp[now_point]
                val = grid[r*3001 + c]

                if i == 3:
                    # 取らずに下
                    dp[r_point] = max(now, dp[r_point])
                    continue

                # 取って下
                dp[r_point] = max(now + val, dp[r_point])
                # 取らずに下
                dp[r_point] = max(now, dp[r_point])
                # 取って横
                dp[c_point_plus] = max(now + val, dp[c_point_plus])
                # 取らずに横
                dp[c_point_non] = max(now, dp[c_point_non])

    dp_RC = [dp[get_point(R, C, 0) + i] for i in range(4)]
    print(max(dp_RC))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():

    R, C, K = map(int, sys.stdin.readline().rstrip().split())

    V = [[0 for _ in range(C)] for _ in range(R)]
    point = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
    point[0][0][0] = 0
    for i in range(K):
        r, c, v = map(int, sys.stdin.readline().rstrip().split())
        V[r - 1][c - 1] = v

    # print(V)

    point[0][0][1] = V[0][0]
    point[0][0][0] = 0

    for r in range(R):
        for c in range(C):

            # 列方向に動く
            if c > 0:
                point[r][c][3] = max(point[r][c - 1][2] + V[r][c], point[r][c - 1][3])
                point[r][c][2] = max(point[r][c - 1][1] + V[r][c], point[r][c - 1][2])
                point[r][c][1] = max(point[r][c - 1][0] + V[r][c], point[r][c - 1][1])
                point[r][c][0] = point[r][c - 1][0]

            # 行方向に動く
            if r > 0:
                point[r][c][1] = max(max(point[r - 1][c]) + V[r][c], point[r][c][1])
                point[r][c][0] = max(max(point[r - 1][c]), point[r][c][0])

    # print(point)
    print(max(point[R - 1][C - 1]))


main()

Submission status: Time Limit Exceeded

Solution code:
r,c,k = map(int,input().split())
t = [[0 for i in range(c)] for i in range(r)]
for i in range(k):
    a,b,z = map(int,input().split())
    t[a-1][b-1] = z
dp = [[[0 for i in range(c)]for i in range(r)]for i in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = t[0][0]
dp[2][0][0] = t[0][0]
dp[3][0][0] = t[0][0]
for i in range(r-1):
    dp[0][i+1][0] = dp[1][i][0]
    for j in range(1,4):
        dp[j][i+1][0] = dp[1][i][0]+t[i+1][0]
for i in range(c-1):
    dp[0][0][i+1] = 0
    for j in range(1,4):
        dp[j][0][i+1] = max(dp[1][0][i],t[0][i+1])
    for j in range(2,4):
        dp[j][0][i+1] = max(dp[2][0][i],dp[1][0][i]+t[0][i+1])
    dp[3][0][i+1] = max(dp[3][0][i],dp[2][0][i]+t[0][i+1])
for i in range(r-1):
    for j in range(c-1):
        for s in range(0,4):
            dp[s][i+1][j+1] =  max(dp[0][i+1][j],dp[3][i][j+1])
        for s in range(1,4):
            dp[s][i+1][j+1] = max(dp[0][i+1][j]+t[i+1][j+1],dp[1][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
        for s in range(2,4):
            dp[s][i+1][j+1] = max(dp[1][i+1][j]+t[i+1][j+1],dp[2][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
        dp[3][i+1][j+1] = max(dp[2][i+1][j]+t[i+1][j+1],dp[3][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
print(dp[3][r-1][c-1])

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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1)*3001 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 3001**2 + c * 3001 + i

    dp = defaultdict(int)
    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now = dp[get_point(r, c, i)]
                val = grid[r*3001 + c]

                if i == 3:
                    # 取らずに下
                    dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                    continue

                # 取って下
                dp[get_point(r+1, c, 0)] = max(now + val, dp[get_point(r+1, c, 0)])
                # 取らずに下
                dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                # 取って横
                dp[get_point(r, c+1, i+1)] = max(now + val, dp[get_point(r, c+1, i+1)])
                # 取らずに横
                dp[get_point(r, c+1, i)] = max(now, dp[get_point(r, c+1, i)])

    dp_RC = [dp[get_point(R, C, i)] for i in range(4)]
    print(max(dp_RC))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

dp=np.array([[[0]*4]*C for _ in range(R)])

dp[0][0][1]=L[0][0]
dp[0][0][2]=L[0][0]
dp[0][0][3]=L[0][0]
#print(dp)
for h in range(R):
    for w in range(C):
        if h==0 and w==0:
            continue
        for k in range(4):
            if h==0:
                if k==0:
                    dp[h][w][k]=dp[h][w-1][k]
                else:
                    dp[h][w][k]=max(L[h][w]+dp[h][w-1][k-1], dp[h][w-1][k])

            elif w==0:
                if k==0:
                    dp[h][w][k]=dp[h-1][w][3]
                else:
                    dp[h][w][k]=max(L[h][w]+dp[h-1][w][3], dp[h-1][w][k])

            else:
                if k==0:
                    dp[h][w][k]=max(dp[h-1][w][3], dp[h][w-1][k])
                else:
                    dp[h][w][k]=max(L[h][w]+max(dp[h-1][w][3], dp[h][w-1][k-1]), dp[h][w-1][k])
            #print(dp[0][0], dp[0][1])
print(dp[R-1][C-1][3])
#print(dp)
#print(L)
#print(dp[0][0][1])

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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1)*10**4 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 10**8 + c * 10**4 + i

    dp = defaultdict(int)
    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now = dp[get_point(r, c, i)]
                val = grid[r*10**4 + c]

                if i == 3:
                    # 取らずに下
                    dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                    continue

                # 取って下
                dp[get_point(r+1, c, 0)] = max(now + val, dp[get_point(r+1, c, 0)])
                # 取らずに下
                dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                # 取って横
                dp[get_point(r, c+1, i+1)] = max(now + val, dp[get_point(r, c+1, i+1)])
                # 取らずに横
                dp[get_point(r, c+1, i)] = max(now, dp[get_point(r, c+1, i)])

    dp_RC = [dp[get_point(R, C, i)] for i in range(4)]
    print(max(dp_RC))


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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1, c-1)] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 10**8 + c * 10**4 + i

    dp = defaultdict(int)
    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                point = r * 10**8 + c * 10**4 + i
                now = dp[point]
                val = grid[(r, c)]

                if i == 3:
                    # 取らずに下
                    dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                    continue

                # 取って下
                dp[get_point(r+1, c, 0)] = max(now + val, dp[get_point(r+1, c, 0)])
                # 取らずに下
                dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                # 取って横
                dp[get_point(r, c+1, i+1)] = max(now + val, dp[get_point(r, c+1, i+1)])
                # 取らずに横
                dp[get_point(r, c+1, i)] = max(now, dp[get_point(r, c+1, i)])

    dp_RC = [dp[get_point(R, C, i)] for i in range(4)]
    print(max(dp_RC))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

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


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

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

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

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

            dp[i][r][c] = max(dp[i][r][c], dp[i][r][c-1])
            if i > 0:
                dp[i][r][c] = max(dp[i][r][c], dp[i-1][r][c-1] + v)

ans = 0
for i in range(4):
    ans = max(ans, dp[i][R][C])

print(ans)

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 = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1, c-1)] = v

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

    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now = dp[r][c][i]
                val = grid[(r, c)]

                if i == 3:
                    # 取らずに下
                    dp[r + 1][c][0] = max(now, dp[r + 1][c][0])
                    continue

                # 取って下
                dp[r+1][c][0] = max(now + val, dp[r+1][c][0])
                # 取らずに下
                dp[r+1][c][0] = max(now, dp[r+1][c][0])
                # 取って横
                dp[r][c+1][i+1] = max(now + val, dp[r][c+1][i+1])
                # 取らずに横
                dp[r][c+1][i] = max(now, dp[r][c+1][i])

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    r, c, k = map(int, input().split())
    d = [[0]*c for _ in [0]*r]
    for _ in range(k):
        t1, t2, t3 = map(int, input().split())
        d[t1-1][t2-1] = t3
    dp = [[[0]*4 for _ in [0]*c] for _ in [0]*r]
    dp[0][0][1] = d[0][0]
    for x in range(r):
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        dp[x][y+1][z+1] = max(dp[x][y+1]
                                              [z+1], dp[x][y][z]+d[x][y+1])
                    dp[x][y+1][z] = max(dp[x][y+1][z], dp[x][y][z])
            if x != r-1:
                dp[x+1][y][1] = max(dp[x+1][y][1], max(dp[x][y])+d[x+1][y])
                dp[x+1][y][0] = max(dp[x+1][y][0], max(dp[x][y]))
    print(max(dp[r-1][c-1]))


main()