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 main():

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

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

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

    return max(dp[R][C])


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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int,input().split())
V = [[]for i in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    V[r-1].append([c,v])
cost = [0]*(C+1)
for r in range(R):
    V[r].sort()
    hoge = [0,0,0,0]
    for c,v in V[r]:
        for i in range(3,0,-1):
            hoge[i] = max(cost[c]+v,hoge[i-1]+v,hoge[i])
        hoge[0] = cost[c]
        cost[c] = max(hoge)
    for i in range(1,C+1):
        cost[i] = max(cost[i],cost[i-1])

print(cost[-1])

Submission status: Time Limit Exceeded

Solution code:
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
    dp0 = [[0]*(C+1) for _ in range(R+1)]
    dp1 = [[0]*(C+1) for _ in range(R+1)]
    dp2 = [[0]*(C+1) for _ in range(R+1)]
    dp3 = [[0]*(C+1) for _ in range(R+1)]
    for r in range(1,R+1):
        m = 0
        for c in range(1,C+1):
            m = max(m,dp0[r-1][c], dp1[r-1][c], dp2[r-1][c], dp3[r-1][c])
            dp0[r][c] = max(m, dp0[r][c-1])
            dp1[r][c] = max(dp1[r][c-1], dp0[r][c]+V[r-1][c-1])
            dp2[r][c] = max(dp2[r][c-1], dp1[r][c-1]+V[r-1][c-1])
            dp3[r][c] = max(dp3[r][c-1], dp2[r][c-1]+V[r-1][c-1])
    # print(dp0)
    # print(dp1)
    # print(dp2)
    # print(dp3)
    print( max( max(dp0[R]), max(dp1[R]), max(dp2[R]), max(dp3[R])))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

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

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

    return max(dp[R][C])


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

Submission status: Time Limit Exceeded

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

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

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

for r in range(1,R+1):
    for c in range(1,C+1):
        v = V[r][c]
        dprc  = dp[r][c]
        dprcL = dp[r][c-1]
        if v==0:
            dprc[0] = max(max(dp[r-1][c]), dprcL[0])
            for n in range(1,4): dprc[n] = dprcL[n]
        else:
            dprc[0] = max(max(dp[r-1][c])  ,             dprcL[0])
            dprc[1] = max(max(dp[r-1][c])+v, dprcL[0]+v, dprcL[1])
            dprc[2] = max(                   dprcL[1]+v, dprcL[2])
            dprc[3] = max(                   dprcL[2]+v, dprcL[3])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
r, c, k = map(int, input().split())
it = [[0] * c for _ in range(r)]
for _ in range(k):
    rr, cc, vv = map(int, input().split())
    rr -= 1
    cc -= 1
    it[rr][cc] = vv

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

dp[0][0][1] = it[0][0]
for i in range(r):
    for j in range(c):
        if i != 0:
            dp[i][j][0] = max(dp[i-1][j])
            dp[i][j][1] = max(dp[i-1][j]) + it[i][j]
        if j != 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i][j-1][0])
            for k in range(1, 4):
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k],
                                  dp[i][j-1][k-1]+it[i][j])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

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

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

for i in range(R):
    for j in range(C):
        if i != 0 and j != 0:
            DP[i][j][0] = max(DP[i-1][j][0],DP[i-1][j][1],DP[i-1][j][2],DP[i-1][j][3],DP[i][j-1][0])
            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]
            if Gift[i][j]:
                DP[i][j][3] = max(DP[i][j][3],DP[i][j][2] + Gift[i][j])
                DP[i][j][2] = max(DP[i][j][2],DP[i][j][1] + Gift[i][j])
                DP[i][j][1] = max(DP[i][j][1],DP[i][j][0] + Gift[i][j])
        elif i == 0:
            DP[i][j][0] = DP[i][j-1][0]
            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]
            if Gift[i][j]:
                DP[i][j][3] = max(DP[i][j][3],DP[i][j][2] + Gift[i][j])
                DP[i][j][2] = max(DP[i][j][2],DP[i][j][1] + Gift[i][j])
                DP[i][j][1] = max(DP[i][j][1],DP[i][j][0] + Gift[i][j])
        elif j == 0:
            DP[i][j][0] = max(DP[i-1][j][0],DP[i-1][j][1],DP[i-1][j][2],DP[i-1][j][3])
            if Gift[i][j]:
                DP[i][j][3] = max(DP[i][j][3],DP[i][j][2] + Gift[i][j])
                DP[i][j][2] = max(DP[i][j][2],DP[i][j][1] + Gift[i][j])
                DP[i][j][1] = max(DP[i][j][1],DP[i][j][0] + Gift[i][j])
        else:
            DP[i][j][1] = Gift[i][j]

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

Submission status: Time Limit Exceeded

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

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

rck = {}
for i in range(K):
    tmp = list(map(int, input().split()))
    rck[(tmp[0], tmp[1])] = tmp[2]


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

# print(rck)
dp = [0] * (C+1)
for r_i in range(1, R+1):
    tmp0 = 0
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    dp_new = [0]*(C+1)
    for c_i in range(1, C+1):
        tmp0 = max(dp[c_i], tmp0)
        tmp1 = max(dp[c_i], tmp1)
        tmp2 = max(dp[c_i], tmp2)
        tmp3 = max(dp[c_i], tmp3)

        index = (r_i, c_i)
        if index in rck:
            tmp3 = max(tmp3, tmp2+rck[index])
            tmp2 = max(tmp2, tmp1+rck[index])
            tmp1 = max(tmp1, tmp0+rck[index])
        dp_new[c_i] = max(tmp0, tmp1, tmp2, tmp3)
    # print(dp_new)
    dp = dp_new
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

def smax(i,j):
    if i>j:
        return i
    else:
        return j

def smax2(L):
    return smax(smax(L[0],L[1]),smax(L[2],L[3]))

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

    D = [[0]*C for i in range(R)]

    for i in range(K):
        r,c,v = list(map(int,input().split()))
        r,c = r-1,c-1
        D[r][c] = v
        
    DP = [[[0]*4 for j in range(C)] for i in range(R)]
    
    for i in range(R):
        for j in range(C):
            if i!=0 and j!=0:
                DP[i][j][0] = smax(smax2(DP[i-1][j]), DP[i][j-1][0])
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], smax(DP[i][j-1][1],smax2(DP[i-1][j])+D[i][j]))
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            elif i!=0 and j==0:
                DP[i][j][0] = smax2(DP[i-1][j])
                DP[i][j][1] = smax2(DP[i-1][j])+D[i][j]
            elif i==0 and j!=0:
                DP[i][j][0] = DP[i][j-1][0]
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], DP[i][j-1][1])
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            else:
                DP[i][j][0] = 0
                DP[i][j][1] = D[i][j]

    print(smax2(DP[R-1][C-1]))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
*data, = map(int, sys.stdin.read().split()[::-1])
def inp():
    global data
    return data.pop()
R, C, k = inp(), inp(), inp()
grid = [[0] * C for _ in range(R)]

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


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


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

print(mx)

Submission status: Time Limit Exceeded

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

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

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

for r in range(1,R+1):
    for c in range(1,C+1):
        v = V[r][c]
        if v==0:
            dp[r][c][0] = max(max(dp[r-1][c]), dp[r][c-1][0])
            for n in range(1,4):
                dp[r][c][n] = dp[r][c-1][n]
        else:
            dp[r][c][0] = max(max(dp[r-1][c]), dp[r][c-1][0])
            dp[r][c][1] = max(max(dp[r-1][c])+v, dp[r][c-1][0]+v, dp[r][c-1][1])
            dp[r][c][2] = max(dp[r][c-1][1]+v, dp[r][c-1][2])
            dp[r][c][3] = max(dp[r][c-1][2]+v, dp[r][c-1][3])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    R, C, K = read_values()
    V2 = [0] * (R * C)
    for _ in range(K):
        r, c, v = read_values()
        r -= 1
        c -= 1
        V2[r * C + c] = v
    
    dp = [[V(max, 0) for _ in range(4)] for _ in range(R * C)]
    for i in range(R * C):
        r = i // C
        c = i % C
        if c + 1 < C:
            # not take
            for k in range(4):
                dp[i + 1][k].ud(dp[i][k].v)

            # take 
            for k in range(3):
                dp[i + 1][k + 1].ud(dp[i][k].v + V2[i])

        # next r
        if r + 1 < R:
            for k in range(3):
                dp[i + C][0].ud(dp[i][k].v + V2[i])
            dp[i + C][0].ud(dp[i][3].v)

    print(max(k.v + (V2[-1] if i  < 3 else 0) for i, k in enumerate(dp[-1])))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

import sys
sys.setrecursionlimit(300000)


def solve(R: int, C: int, N: int, r: "List[int]", c: "List[int]", v: "List[int]"):
    rr = sorted(list(set(r)))
    cc = sorted(list(set(c)))
    #print(rr, cc)
    rmap, cmap = {}, {}
    for i, vv in enumerate(rr):
        rmap[vv] = i
    for i, vv in enumerate(cc):
        cmap[vv] = i

    h, w = len(rr), len(cc)
    x = {}
    for i in range(N):
        rx, cx = rmap[r[i]], cmap[c[i]]
        if not rx in x:
            x[rx] = []
        x[rx].append((cx, v[i]))

    dp = [[0] * (w + 1) for _ in range(h + 1)]
    #print(x)
    for i in range(h):
        for j in range(w + 1):
            dp[i + 1][j] = max(dp[i][j], dp[i + 1][j])
        if not i in x:
            #dp[i + 1] = dp[i]
            continue
        ww = x[i]
        ww.sort()
        for j in range(len(ww)):
            jj = ww[j][0]
            tmp = [0, 0, 0]
            s = 0
            for k in range(j, len(ww)):
                kk = ww[k][0]
                if tmp[-1] < ww[k][1]:
                    s += ww[k][1] - tmp[-1] 
                    tmp[-1] = ww[k][1]
                    tmp.sort(reverse=True)
                dp[i + 1][kk] = max(dp[i + 1][kk], dp[i][jj] + s)

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


        #for jj in range(w):
        #    tmp = [0, 0, 0]
        #    s = 0
        #    for j in range(jj, w):
        #        if i in x and j in x[i]:
        #            if tmp[-1] < x[i][j]:
        #                s += x[i][j] - tmp[-1] 
        #                tmp[-1] = x[i][j]
        #                tmp.sort(reverse=True)
        #        dp[i + 1][j] = max(dp[i + 1][j], dp[i][jj] + s)

    #print(dp)
    ret = 0
    for v in dp[h]:
        ret = max(ret, v)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    R = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    r = [int()] * (K)  # type: "List[int]"
    c = [int()] * (K)  # type: "List[int]"
    v = [int()] * (K)  # type: "List[int]"
    for i in range(K):
        r[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens)) - 1
        v[i] = int(next(tokens))
    solve(R, C, K, r, c, v)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict

    r, c, k = map(int, readline().split())
    items = defaultdict(int)
    for _ in range(k):
        row, col, value = map(int, readline().split())
        h = row * 10007 + col
        items[h] = value

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

    for row in range(1, r + 1):
        dp_row = dp[row]
        for col in range(1, c + 1):
            h = row * 10007 + col
            value = items[h]
            dp_row[col][0] = max(dp[row - 1][col])
            dp_row[col][1] = max(dp_row[col][0] + value,dp_row[col - 1][1], dp_row[col - 1][0] + value)
            dp_row[col][2] = max(dp_row[col - 1][2], dp_row[col - 1][1] + value)
            dp_row[col][3] = max(dp_row[col - 1][3], dp_row[col - 1][2] + value)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
r,c,k = map(int, input().split())
rcv = [list(map(int, input().split())) for i in range(k)]
d = dict()
for rr,cc,vv in rcv:
    d[(rr-1,cc-1)] = vv

DP_now = [[0]*4 for i in range(c)]
DP_prev = [[0]*4 for i in range(c)]

max_tops = [0]*c
for i in range(r):
    for j in range(c):
        max_top = max_tops[j]
        if (i,j) in d:
            v = d[(i,j)]
            DP_now[j][0] = max_top
            DP_now[j][1] = max_top + v
            for k in range(4):
                if j != 0:
                    if DP_now[j][k] < DP_now[j-1][k]:
                        DP_now[j][k] = DP_now[j-1][k]
                    if k!=0 and DP_now[j][k] < DP_now[j-1][k-1] + v:
                        DP_now[j][k] =  DP_now[j-1][k-1] + v
        else:
            DP_now[j][0] = max_top
            if j>0:
                DP_now[j] = DP_now[j-1][:]
            if DP_now[j][0] < max_top:
                DP_now[j][0] = max_top
        max_tops[j] = max(DP_now[j])
    DP_prev = DP_now
    DP_now = [[0]*4 for i in range(c)]
ans = max(DP_prev[-1])
print(ans)

Submission status: Time Limit Exceeded

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

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

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

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

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

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

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

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


if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
RCV=[list(map(int,input().split())) for i in range(K)]
V=[[0]*(C+1) for i in range(R+1)]
for r,c,v in RCV:
    V[r][c]=v
d=[[[0]*4 for i in range(C+1)] for i in range(R+1)]
for i in range(R):
    for j in range(C):
        m=max(d[i][j+1])
        d[i+1][j+1][0]=m
        for k in range(3):
            d[i+1][j+1][k+1]=max(d[i+1][j][k+1],m+V[i+1][j+1],d[i+1][j][k]+V[i+1][j+1])
print(max(d[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
def main():
    R,C,K=map(int,input().split())
    l=[[[0]*4 for i in range(C)] for j in range(R)]
    m=[[0]*C for i in range(R)]
    for i in range(K):
        r,c,v=map(int,input().split())
        m[r-1][c-1]=v
    if m[0][0]:
        l[0][0][1]=m[0][0]
    for i in range(1,C):
        l[0][i][1]=max(l[0][i-1][1],m[0][i])
        l[0][i][2]=max(l[0][i-1][2],m[0][i]+l[0][i-1][1])
        l[0][i][3]=max(l[0][i-1][3],m[0][i]+l[0][i-1][2])
    for i in range(1,R):
        l[i][0][1]=max(l[i-1][0])+m[i][0]
        for j in range(1,C):
            if m[i][j]:
                ma=max(l[i-1][j])
                mi=m[i][j]
                if ma<l[i][j-1][0]:
                    l[i][j][0]=l[i][j-1][0]
                else:
                    l[i][j][0]=ma
                l[i][j][1]=max(l[i][j][0]+mi,l[i][j-1][1])
                if l[i][j-1][1]+mi>l[i][j-1][2]:
                    l[i][j][2]=l[i][j-1][1]+mi
                else:
                    l[i][j][2]=l[i][j-1][2]
                if l[i][j-1][2]+mi>l[i][j-1][3]:
                    l[i][j][3]=l[i][j-1][2]+mi
                else:
                    l[i][j][3]=l[i][j-1][3]
            else:
                l[i][j]=l[i][j-1].copy()
                ma=max(l[i-1][j])
                if l[i][j][0]<ma:
                    l[i][j][0]=ma
    print(max(l[-1][-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
V = [[[] for _ in range(C)] for _ in range(R)]
for _ in range(K):
  r, c, v = map(int, input().split())
  V[r-1][c-1].append(v)
M = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
# M[r][c][count in column (c)]
for r in range(R):
  for c in range(C):
    V[r][c].sort(reverse=True)
    #candidates = [0 for _ in range(4)]
    #print(candidates)
    for n_pick in range(1 + min(3, len(V[r][c]))):
      s = sum(V[r][c][:n_pick])
      M[r][c][n_pick]=max(M[r][c][n_pick],s)
      if r > 0:
        for n_prev in range(4):
          #print('R++', r, c, n_pick, n_prev)
          if M[r-1][c][n_prev] > 0 or n_prev == 0:
            M[r][c][n_pick]=max(M[r][c][n_pick],
              M[r-1][c][n_prev] + s)
      if c > 0:
        for n_prev in range(4 - n_pick):
          #print('C++', r, c, n_pick, n_prev)
          if  M[r][c-1][n_prev]  > 0 or n_prev == 0:
            M[r][c][n_prev+n_pick]=max(M[r][c][n_prev+n_pick],
              M[r][c-1][n_prev] + s)
    #print(candidates)
    #print(r, c, candidates, M)
    #for i in range(4):
    #  M[r][c][i] = max(candidates[i])
    #print(r, c, M)
#print(V, M)
print(max(M[R-1][C-1]))

Submission status: Time Limit Exceeded

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

basho=[[0]*c for i in range(r)]
data=[[[0,0,0,0]for j in range(c+1)] for i in range(r+1)]

for i in range(k):
    a,b,d=map(int,input().split())
    basho[a-1][b-1]=d

for i in range(1,r+1):
    for j in range(1,c+1):
        #print(i,j)
        if(basho[i-1][j-1]==0):
            data[i][j][1]=data[i][j-1][1]
            data[i][j][2]=data[i][j-1][2]
            data[i][j][3]=data[i][j-1][3]
            data[i][j][0]=max(data[i][j-1][0],max(data[i-1][j]))
        else:
            jibun=basho[i-1][j-1]
            data[i][j][0]=max(max(data[i-1][j]),data[i][j-1][0])
            data[i][j][1]=max(max(data[i-1][j])+jibun,data[i][j-1][1],data[i][j-1][0]+jibun)
            data[i][j][2]=max(data[i][j-1][2],data[i][j-1][1]+jibun)
            data[i][j][3]=max(data[i][j-1][3],data[i][j-1][2]+jibun)


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

Submission status: Time Limit Exceeded

Solution code:
R,C,K = map(int,input().split())
V = [[0 for c in range(C+1)] for r in range(R+1)]
r_list = [False for r in range(R+1)]
c_list = [False for c in range(C+1)]
for i in range(K):
	r,c,v = map(int,input().split())
	V[r][c] = v
	r_list[r] = True
	c_list[c] = True


r_map = {}
cnt = 0
for i in range(R+1):
	if r_list[i]:	
		cnt += 1
		r_map[i] = cnt
r_len = cnt

c_map = {}
cnt = 0
for i in range(C+1):
	if c_list[i]:
		cnt += 1
		c_map[i] = cnt
c_len = cnt

new_v = [[0 for i in range(c_len+1)] for j in range(r_len+1)]
for r in range(R+1):
	for c in range(C+1):
		if V[r][c] > 0:
			new_v[r_map[r]][c_map[c]] = V[r][c]

V = new_v
R = r_len
C = c_len

dp = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]
for r in range(1, R+1):
	for c in range(1, C+1):
		v = V[r][c]
		under_val = max(dp[r-1][c])
		dp[r][c][0] = max(under_val, dp[r][c-1][0])
		if v > 0:
			dp[r][c][1] = max(under_val + v, dp[r][c-1][0] + v, dp[r][c-1][1])
			dp[r][c][2] = max(dp[r][c-1][1] + v, dp[r][c-1][2])
			dp[r][c][3] = max(dp[r][c-1][2] + v, dp[r][c-1][3])
		else:
			dp[r][c][1] = dp[r][c-1][1]
			dp[r][c][2] = dp[r][c-1][2]
			dp[r][c][3] = dp[r][c-1][3]
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

DP=[[[0]*4 for _ in range(c+1)] for i in range(r+1)]
for i in range(r+1):
	for j in range(c+1):
		v=D[i*10**5+j]
		if v!=0:
			DP[i][j][1]=max(DP[i-1][j])+v
		for ii in range(4):
			DP[i][j][0]=max(DP[i][j][0],DP[i-1][j][ii])
			if v!=0:
				DP[i][j][1]=max(DP[i][j][1],DP[i-1][j][ii]+v)
			DP[i][j][ii]=max(DP[i][j][ii],DP[i][j-1][ii])
			if v!=0 and ii!=0:
				DP[i][j][ii]=max(DP[i][j][ii],DP[i][j-1][ii-1]+v)
		
ans=max(DP[r-1][c-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

R, C, K = mapint()
dp = [[[0]*4 for _ in range(C)] for _ in range(R)]
from collections import defaultdict
dic = [[0]*C for _ in range(R)]
for _ in range(K):
    r, c, v = mapint()
    dic[r-1][c-1] = v

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

DP = [[[0,0,0,0] for _ in range(C+1)] for _ in range(R+1)]
for r in range(1,R+1):
  # 0
  for c in range(1,C+1):
    DP[r][c][0] = DP[r-1][c][3]
  # 1
  for c in range(1,C+1):
    DP[r][c][1] = max(DP[r][c][0]+items[r][c],DP[r][c-1][1])
  # 2
  for c in range(1,C+1):
    DP[r][c][2] = max(DP[r][c][1],DP[r][c-1][2],DP[r][c-1][1]+items[r][c])
  # 3
  for c in range(1,C+1):
    DP[r][c][3] = max(DP[r][c][2],DP[r][c-1][3],DP[r][c-1][2]+items[r][c])
        
print(DP[R][C][3])

Submission status: Time Limit Exceeded

Solution code:
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
r,c,k=map(int,input().split())
dp = [[[0,0,0,0] for y in range(c)] for z in range(r)] 
m=[]
for i in range(r):
  m.append([])
  for j in range(c):
    m[i].append(0)
for i in range(k):
  ri,ci,v=map(int,input().split())
  m[ri-1][ci-1]=v
dp[0][0][0]=0
dp[0][0][1]=m[0][0]
dp[0][0][2]=m[0][0]
dp[0][0][3]=m[0][0]
for i in range(r):
  for j in range(c):
    if j+1<c:
      dp[i][j+1][0]=max(dp[i][j+1][0],dp[i][j][0])
      dp[i][j+1][1]=max(dp[i][j][1],max(dp[i][j+1][1],dp[i][j][0]+m[i][j+1]))
      dp[i][j+1][2]=max(dp[i][j][2],max(dp[i][j+1][2],dp[i][j][1]+m[i][j+1]))
      dp[i][j+1][3]=max(dp[i][j][3],max(dp[i][j+1][3],dp[i][j][2]+m[i][j+1]))
    if i+1<r:
      dp[i+1][j][0]=dp[i][j][3]
      dp[i+1][j][1]=dp[i][j][3]+m[i+1][j]
      dp[i+1][j][2]=dp[i][j][3]+m[i+1][j]
      dp[i+1][j][3]=dp[i][j][3]+m[i+1][j]
print(dp[r-1][c-1][3])