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: Accepted

Solution code:
import sys
input = sys.stdin.readline
R, L, K = map(int, input().split())
V = [0]*(R*L)
max2 = lambda x, y: x if x > y else y
for _ in range(K):
    r, c, v = map(int, input().split())
    V[(c-1)*R+(r-1)] = v
A, B, C, D = [[0]*(R+1) for _ in range(4)]
for i in range(L):
    for j in range(R):
        A[j] = max(A[j-1],B[j-1],C[j-1],D[j-1])
        v = V[i*R+j]
        if v != 0:
            D[j], C[j], B[j] = max2(C[j]+v,D[j]), max2(B[j]+v,C[j]), max2(A[j]+v,B[j])
print(max(A[-2],B[-2],C[-2],D[-2]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
R, L, K = map(int, input().split())
V = [0]*(R*L)
for _ in range(K):
    r, c, v = map(int, input().split())
    V[(c-1)*R+(r-1)] = v
A, B, C, D = [[0]*(R+1) for _ in range(4)]
for i in range(L):
    for j in range(R):
        A[j] = max(A[j-1],B[j-1],C[j-1],D[j-1])
        v = V[i*R+j]
        if v != 0:
            D[j], C[j], B[j] = max(C[j]+v,D[j]), max(B[j]+v,C[j]), max(A[j]+v,B[j])
print(max(A[-2],B[-2],C[-2],D[-2]))

Submission status: Accepted

Solution code:
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
# input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

h,w,k=map(int,input().split())
# R=[]
A=[[0]*w for i in range(h)]
dp=[[[0 for j in range(w)] for i in range(h)] for i in range(4)]

for s in sys.stdin.readlines():
    x,y,v=map(int,s.split())
    x-=1;y-=1
    A[x][y]=v
    # dp[x][y][1]=v
    # dp[x][y][2]=v
    # dp[x][y][3]=v;
dp[1][0][0]=A[0][0]
for i in range(h):
    for j in range(w):
        for k in range(4):
            here=dp[k][i][j]
            if i<h-1:
                dp[0][i+1][j] = max(here,dp[0][i+1][j])
                dp[1][i+1][j] = max(here+A[i+1][j], dp[1][i+1][j])
            if j<w-1:
                dp[k][i][j+1] = max(dp[k][i][j+1], here)
                if k<=2:
                    dp[k+1][i][j+1]= max(dp[k+1][i][j+1],here+ A[i][j+1])
ans=0
for i in range(4):
    ans=max(ans, dp[i][-1][-1])
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
R, L, K = map(int, input().split())
V = [[0]*R for _ in range(L)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[c-1][r-1] = v
A, B, C, D = [[0]*(R+1) for _ in range(4)]
for i in range(L):
    for j in range(R):
        A[j] = max(A[j-1],B[j-1],C[j-1],D[j-1])
        v = V[i][j]
        if v != 0:
            D[j], C[j], B[j] = max(C[j]+v,D[j]), max(B[j]+v,C[j]), max(A[j]+v,B[j])
print(max(A[-2],B[-2],C[-2],D[-2]))

Submission status: Accepted

Solution code:
def max2(x,y):
    return x if x > y else y

R, L, K = map(int, input().split())
V = [[0]*R for _ in range(L)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[c-1][r-1] = v
A, B, C, D = [[0]*(R+1) for _ in range(4)]
for i in range(L):
    for j in range(R):
        A[j] = max(A[j-1], B[j-1], C[j-1], D[j-1])
        v = V[i][j]
        if v != 0:
            D[j], C[j], B[j] = max2(C[j] + v, D[j]), max2(B[j] + v, C[j]), max2(A[j] + v, B[j])

print(max(A[-2], B[-2], C[-2], D[-2]))

Submission status: Accepted

Solution code:
R, L, K = map(int, input().split())
V = [[0]*R for _ in range(L)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[c-1][r-1] = v
A, B, C, D = [[0]*(R+1) for _ in range(4)]
for i in range(L):
    for j in range(R):
        A[j] = max(A[j-1], B[j-1], C[j-1], D[j-1])
        v = V[i][j]
        if v != 0:
            D[j], C[j], B[j] = max(C[j] + v, D[j]), max(B[j] + v, C[j]), max(A[j] + v, B[j])

print(max(A[-2], B[-2], C[-2], D[-2]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

R, C, K = map(int, readline().split())
data = [[0]*(C+1) for _ in range(R+1)]
for _ in range(K):
    r,c,k = map(int,readline().split())
    data[r][c] = k
    
dp = [0]*(C+1)

for i in range(1,R+1):
    di = data[i]
    ndp = [0]*(C+1)
    kdp = [0]*4
    for j in range(1,C+1):
        kdp[0] = max(kdp[0],dp[j])
        for k in range(2,-1,-1):
            kdp[k+1] = max(kdp[k+1],kdp[k]+di[j])
        ndp[j] = max(kdp)
    dp = ndp
print(dp[C])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

dp = [0] * (C+1)
dp[0] = 0

for i in range(1,R+1):
    ndp = [0] * (C+1)
    row = V[i]
    ldp = [0] * 4
    for j in range(1,C+1):
        # ldp[0] = max(ldp[0], dp[j])
        ldp[0] = dp[j]
        for k in range(2,-1,-1):
            ldp[k+1] = max(ldp[k+1], ldp[k] + row[j]) # 取る場合と取らない場合
        ndp[j] = max(ldp)
    dp = ndp
print(dp[C])

Submission status: Accepted

Solution code:
def max2(x,y):
    return x if x > y else y

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

dp = [[0]*4 for _ in range(R)]
for j in range(R):
    if V[0][j] != 0:
        dp[j][1] = max2(dp[j - 1][1], dp[j - 1][0]) + V[0][j]
    if j != 0:
        dp[j][0] = max2(dp[j - 1][1], dp[j - 1][0])
for i in range(1,C):
    for j in range(R):
        v = V[i][j]
        if v != 0:
            if dp[j][2] != 0:
                dp[j][3] = max2(dp[j][2] + v, dp[j][3])
            if dp[j][1] != 0:
                dp[j][2] = max2(dp[j][1] + v, dp[j][2])
            dp[j][1] = max2(v, dp[j][1])
        if j != 0:
            if v != 0:
                dp[j][1] = max2(max(dp[j - 1][k] for k in range(4)) + v, dp[j][1])
            dp[j][0] = max2(max(dp[j - 1][k] for k in range(4)), dp[j][0])
print(max(dp[-1]))

Submission status: Accepted

Solution code:
R,C,K= map(int,input().split(" "))
original=[[0]*(C+1) for i in range(R+1)]
for _ in range(K):
    r,c,v= map(int,input().split(" "))
    original[r][c]=v
d1=[[0]*(C+1) for i in range(R+1)]
d2=[[0]*(C+1) for i in range(R+1)]
d3=[[0]*(C+1) for i in range(R+1)]
for i in range(1,R+1):
    for j in range(1,C+1):
        curr= original[i][j]
        d1[i][j]=max(d1[i-1][j]+curr,d2[i-1][j]+curr,d3[i-1][j]+curr,d1[i][j-1],curr)
        d2[i][j]=max(d2[i][j-1],d1[i][j-1]+curr)
        d3[i][j]=max(d3[i][j-1],d2[i][j-1]+curr)
print(max(d1[-1][-1],d2[-1][-1],d3[-1][-1]))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m,k = inpl()
value = [[0 for _ in range(m)] for i in range(n)]
for _ in range(k):
    a,b,c = inpl()
    value[a-1][b-1] = c
dp = [0] * (4*(m+1)*(n+1))
def ind(i,j,k): 
    return 4*(m+1)*i + 4*j + k
for i in range(n):
    for j in range(m):
        for k in range(3)[::-1]:
            dp[ind(i,j,k+1)] = max(dp[ind(i,j,k+1)], dp[ind(i,j,k)] + value[i][j])
        for k in range(4):
            dp[ind(i+1,j,0)] = max(dp[ind(i+1,j,0)], dp[ind(i,j,k)])
            dp[ind(i,j+1,k)] = max(dp[ind(i,j+1,k)], dp[ind(i,j,k)])
# print(dp)
mx = 0
for k in range(4):
    mx = max(mx, dp[ind(n-1,m-1,k)])
print(mx)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
R,C,k=map(int,input().split())
A=[[0]*C for i in range(R)]
for i in range(k):
  r,c,v=map(int,input().split())
  A[r-1][c-1]=v
dp0=[[0]*(C+1) for i in range(R+1)]
dp1=[[0]*(C+1) for i in range(R+1)]
dp2=[[0]*(C+1) for i in range(R+1)]
dp3=[[0]*(C+1) for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    V=A[i-1][j-1]
    dp0[i][j]=max(dp0[i-1][j],dp1[i-1][j],dp2[i-1][j],dp3[i-1][j],dp0[i][j-1])
    dp1[i][j]=max(dp0[i][j]+V,dp1[i][j-1])
    dp2[i][j]=max(dp1[i][j-1]+V,dp2[i][j-1])
    dp3[i][j]=max(dp2[i][j-1]+V,dp3[i][j-1])
print(max(dp0[-1][-1],dp1[-1][-1],dp2[-1][-1],dp3[-1][-1]))

Submission status: Accepted

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

r, c, k = map(int, input().split())
vals = [[0]*c for _ in range(r)]
for _ in range(k):
    x, y, v = map(int, input().split())
    vals[x-1][y-1] = v
dp = [[0]*4 for j in range(c)]
for j in range(c):
    if j > 0:
        for k in range(4):
            dp[j][k] = dp[j-1][k]
    if vals[0][j] > 0:
        for k in reversed(range(3)):
            if dp[j][k+1] < dp[j][k] + vals[0][j]:
                dp[j][k+1] = dp[j][k] + vals[0][j]
for i in range(1, r):
    tmp = [[max(dp[j]), 0, 0, 0] for j in range(c)]
    for j in range(c):
        if j > 0:
            for k in range(4):
                if tmp[j][k] < tmp[j-1][k]:
                    tmp[j][k] = tmp[j-1][k]
        if vals[i][j] > 0:
            for k in reversed(range(3)):
                if tmp[j][k+1] < tmp[j][k] + vals[i][j]:
                    tmp[j][k+1] = tmp[j][k] + vals[i][j]
    dp = tmp
print(max(dp[-1]))

Submission status: Accepted

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

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
#print(item)

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

for i in range(R):
    for j in range(C):
        for k in range(1,4):
            #print(i,j,k)
            if k == 1:
                if i != 0:
                    if j != 0:
                        dp[j][k] = max(dp[j-1][k], dp[j-1][k-1] + item[i][j], max(preview[j]) + item[i][j])
                    else:
                        dp[j][k] = max(0, 0 + item[i][j], max(preview[j]) + item[i][j])
                else:
                    if j != 0:
                        dp[j][k] = max(dp[j-1][k], dp[j-1][k-1] + item[i][j])
                    else:
                        dp[j][k] = max(0, 0 + item[i][j])


            else:
                if j != 0:
                    dp[j][k] = max(dp[j-1][k], dp[j-1][k-1] + item[i][j])
                else:
                    dp[j][k] = max(0, 0 + item[i][j])

            #print(dp)
    preview = dp
print(max(dp[C-1]))

Submission status: Accepted

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

#import sys
#input = sys.stdin.buffer.readline

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

    dp0 = [[0]*(C+1) for i in range(R+1)]
    dp1 = [[0]*(C+1) for i in range(R+1)]
    dp2 = [[0]*(C+1) for i in range(R+1)]
    dp3 = [[0]*(C+1) for i in range(R+1)]
    for i in range(R):
        for j in range(C):
            dp3[i][j] = max(dp3[i][j], dp2[i][j]+A[i][j])
            dp2[i][j] = max(dp2[i][j], dp1[i][j]+A[i][j])
            dp1[i][j] = max(dp1[i][j], dp0[i][j]+A[i][j])

            dp3[i][j+1] = max(dp3[i][j+1], dp3[i][j])
            dp2[i][j+1] = max(dp2[i][j+1], dp2[i][j])
            dp1[i][j+1] = max(dp1[i][j+1], dp1[i][j])
            dp0[i][j+1] = max(dp0[i][j+1], dp0[i][j])

            dp0[i+1][j] = max(dp0[i+1][j], dp0[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp1[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp2[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp3[i][j])
    #print(dp)
    print(max([dp0[R-1][C-1], dp1[R-1][C-1], dp2[R-1][C-1], dp3[R-1][C-1]]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import io, os
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

import sys
input = sys.stdin.buffer.readline

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

    dp0 = [[0]*(C+1) for i in range(R+1)]
    dp1 = [[0]*(C+1) for i in range(R+1)]
    dp2 = [[0]*(C+1) for i in range(R+1)]
    dp3 = [[0]*(C+1) for i in range(R+1)]
    for i in range(R):
        for j in range(C):
            dp3[i][j] = max(dp3[i][j], dp2[i][j]+A[i][j])
            dp2[i][j] = max(dp2[i][j], dp1[i][j]+A[i][j])
            dp1[i][j] = max(dp1[i][j], dp0[i][j]+A[i][j])

            dp3[i][j+1] = max(dp3[i][j+1], dp3[i][j])
            dp2[i][j+1] = max(dp2[i][j+1], dp2[i][j])
            dp1[i][j+1] = max(dp1[i][j+1], dp1[i][j])
            dp0[i][j+1] = max(dp0[i][j+1], dp0[i][j])

            dp0[i+1][j] = max(dp0[i+1][j], dp0[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp1[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp2[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp3[i][j])
    #print(dp)
    print(max([dp0[R-1][C-1], dp1[R-1][C-1], dp2[R-1][C-1], dp3[R-1][C-1]]))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

r, c, k = LI()
maze = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
    R, C, V = LI()
    maze[R][C] = V

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 i in range(1, r+1):
    for j in range(1, c+1):
        v = maze[i][j]
        dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + v, dp2[i-1][j] + v, dp3[i-1][j] + v)
        dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1] + v)
        dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1] + v)
ans = max(dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])
print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

r, c, k = LI()
maze = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
    R, C, V = LI()
    maze[R][C] = V

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 i in range(1, r+1):
    for j in range(1, c+1):
        dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + maze[i][j], dp2[i-1][j] + maze[i][j], dp3[i-1][j] + maze[i][j])
        dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1] + maze[i][j])
        dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1] + maze[i][j])
ans = max(dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])
print(ans)

Submission status: Accepted

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

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

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

for x in range(1, 4):
  dp[0][x] = items[0][0]
  for j in range(1, C):
    dp[j][x] = max(dp[j-1][x-1] + items[0][j], dp[j-1][x])

#print(items)
#print(dp)

for i in range(1, R):
  for j in range(C):
    for x in range(4):
      dp[j][x] = dp[j][3]
  for x in range(1, 4):
    dp[0][x] += items[i][0]
    for j in range(1, C):
      dp[j][x] = max(dp[j-1][x-1] + items[i][j], dp[j-1][x], dp[j][3] + items[i][j])
  #print(dp)

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

Submission status: Accepted

Solution code:
import numba
from numba import jit
import numpy as np
 
#(i,j)→(i+1,j)のように動くときはアイテムを入手できる回数がリセットされる
#dp[列][今、何個アイテムを取ったか]でdpテーブルを作る
@jit
def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
 
R, C, K = map(int, input().split())
item_lis = list(list(map(int, input().split())) for i in range(K))
item_map = np.zeros((R, C), np.int64)
for i,j,k in item_lis:
  item_map[i-1,j-1] = k
 
print(main(R, C))

Submission status: Accepted

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]
for i in range(1,R+1):
    for k in range(1,C+1):
        tmp = max(dp[0][i-1][k],dp[1][i-1][k],dp[2][i-1][k],dp[3][i-1][k])
        target = grid[i-1][k-1]
        dp[0][i][k] = max(dp[0][i][k-1],tmp)
        dp[1][i][k] = dp[1][i][k-1]
        dp[2][i][k] = dp[2][i][k-1]
        dp[3][i][k] = dp[3][i][k-1]
        if target != 0:
            dp[1][i][k] = max(dp[0][i][k-1]+target,tmp+target,dp[1][i][k])
            if dp[1][i][k-1] != 0:
                dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
            if dp[2][i][k-1] != 0:
                dp[3][i][k] = max(dp[2][i][k-1]+target,dp[3][i][k])
print(max(dp[0][-1][-1],dp[1][-1][-1],dp[2][-1][-1],dp[3][-1][-1]))

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[0]*(c+1) for j in range(4)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[i][cc]=max(dp[i][cc-1],dp[3][cc])
			else:
				dp[0][cc]=max(dp[3][cc],dp[0][cc-1])
				dp[1][cc]=max(dp[0][cc]+G[rr][cc],dp[1][cc-1])
				dp[2][cc]=max(dp[1][cc-1]+G[rr][cc],dp[2][cc-1],dp[1][cc])
				dp[3][cc]=max(dp[2][cc-1]+G[rr][cc],dp[3][cc-1],dp[2][cc])
	#print(dp)
	print(dp[-1][-1])















if __name__ == "__main__":
	main()

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[[0]*(c+1) for i in range(r+1)] for j in range(4)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[i][rr][cc]=max(dp[i][rr][cc-1],dp[3][rr-1][cc])
			else:
				dp[0][rr][cc]=max(dp[3][rr-1][cc],dp[0][rr][cc-1])
				dp[1][rr][cc]=max(dp[0][rr][cc]+G[rr][cc],dp[1][rr][cc-1])
				dp[2][rr][cc]=max(dp[1][rr][cc-1]+G[rr][cc],dp[2][rr][cc-1],dp[1][rr][cc])
				dp[3][rr][cc]=max(dp[2][rr][cc-1]+G[rr][cc],dp[3][rr][cc-1],dp[2][rr][cc])
	#print(dp)
	print(dp[-1][-1][-1])















if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit
R,C,K=map(int,input().split())
vl=np.zeros((R,C), np.int64)
for _ in range(K):
  r,c,v=map(int,input().split())
  vl[r-1][c-1]=v
@njit
def main():
  dp=np.zeros((R+1,C+1,4), np.int64)
  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]+vl[i-1][j-1])
  return dp[R][C][3]
print(main())

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
H,W,K=map(int,input().split())
G=[[0]*W for i in range(H)]

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

dp=[[[-1]*W for i in range(H)] for i in range(4)]

dp[0][0][0]=0
if G[0][0]!=0:
    dp[1][0][0]=G[0][0]

for i in range(H):
    for j in range(W):
        m=max(dp[k][i][j] for k in range(4))
        if i+1<H:
            dp[1][i+1][j]=max(dp[1][i+1][j],m+G[i+1][j])
            dp[0][i+1][j]=max(dp[0][i+1][j],m)
        if j+1<W:
                dp[0][i][j+1]=0
                dp[1][i][j+1]=max(dp[1][i][j+1],dp[1][i][j],dp[0][i][j]+G[i][j+1])
                dp[2][i][j+1]=max(dp[2][i][j+1],dp[2][i][j],dp[1][i][j]+G[i][j+1])
                dp[3][i][j+1]=max(dp[3][i][j+1],dp[3][i][j],dp[2][i][j]+G[i][j+1])
ans=max(dp[i][H-1][W-1] for i in range(4))
print(ans)

Submission status: Accepted

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

R, C, K = map(int, input().split())
G = [[None for j in range(C)] for i in range(R)]

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

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

for i in range(R):
    for j in range(C):
        if G[i][j] is not None:
            dp[j][0], dp[j][1] = max(dp[j]), max(dp[j]) + G[i][j]
            dp[j][2], dp[j][3] = 0, 0
        else:
            dp[j][0] = max(dp[j])
            dp[j][1], dp[j][2], dp[j][3] = 0, 0, 0
    for j in range(1, C):
        if G[i][j] is not None:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j - 1][0] + G[i][j], dp[j][1])
            if dp[j - 1][1] != 0:
                dp[j][2] = max(dp[j - 1][2], dp[j - 1][1] + G[i][j], dp[j][2])
            if dp[j - 1][2] != 0:
                dp[j][3] = max(dp[j - 1][3], dp[j - 1][2] + G[i][j], dp[j][3])
        else:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j][1])
            dp[j][2] = max(dp[j - 1][2], dp[j][2])
            dp[j][3] = max(dp[j - 1][3], dp[j][3])

print(max(dp[-1]))

Submission status: Accepted

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

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

dp[1][0][0]=rcv[0][0]

#左端を縦に
j=0
for i in range(1,R):
    if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に 
i=0
for j in range(1,C):
    if rcv[i][j]==0:
        for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j])

for i in range(1,R):
    for j in range(1,C):
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] )
print(max( dp[i][-1][-1] for i in range(4)) )

Submission status: Accepted

Solution code:
def pri(x):
    for item in x:
        print(item)

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

fuck=[list(map(int,input().split())) for _ in range(k)]

rck=[[0]*(c+1) for _ in range(r+1)]

for [rr,cc,kk] in fuck:
    rck[rr][cc]=kk
#pri(rck)
#dp[y][x][cnt]

dp=[[0]*4 for _ in range(c+1)]
#print(dp)
for y in range(1,r+1):
    for x in range(1,c+1):
        
        dp[x][0]=max(dp[x-1][0],dp[x][0],dp[x][1],dp[x][2],dp[x][3])
        dp[x][1]=max(dp[x][0]+rck[y][x],dp[x][1],dp[x-1][1])
        dp[x][2]=max(dp[x-1][1]+rck[y][x],dp[x][2],dp[x-1][2],dp[x][1])
        dp[x][3]=max(dp[x-1][2]+rck[y][x],dp[x][3],dp[x-1][3],dp[x][2])
        #print(y,x,dp)

print(max(dp[-1]))

Submission status: Accepted

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


def read():
    R, C, K = map(int, input().strip().split())
    # アイテムの価値V[r][c]
    V = [defaultdict(int) for i in range(R+1)]
    for i in range(K):
        r, c, v = map(int, input().strip().split())
        V[r][c] = v
    return R, C, K, V


def solve(R, C, K, V, INF=10**9):
    # dp[c][s]: c列目までにs個のアイテムを拾った時の、価値の最大値
    dp = [[-INF for s in range(4)] for c in range(C+1)]
    # 次の行に移るときに使うキャッシュ
    before = [0 for c in range(C+1)]
    for r in range(1, R+1):
        # s=0, s=1, s>1で処理を分ける
        for c in range(1, C+1):
            dp[c][0] = max(dp[c-1][0], before[c])
            dp[c][1] = max(dp[c-1][1], dp[c-1][0] + V[r][c], before[c] + V[r][c])
            for s in range(2, 4):
                dp[c][s] = max(dp[c-1][s], dp[c-1][s-1] + V[r][c])
        # 次の行へ移る
        for c in range(1, C+1):
            before[c] = max(dp[c])
        # dpを初期化
        for c in range(C+1):  
            for s in range(4):
                dp[c][s] = 0
    ans = before[-1]
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    R, C,K = map(int, input().split())
    dp = [[[0]*(C+1) for k in range(R+1)] for _ in range(4)]
    # dp[r][c][j] :=r index c columns already j cnt
    dp[0][0][0] = 0
    D = [[0 for i in range(C+1)] for j in range(R+1)]
    import random
    for i in range(K):
        r, c, v = map(int,input().split())
        D[r][c] = v
    
    for i in range(R+1):
        for j in range(C+1):
            if D[i][j] > 0:
                v = D[i][j]
                M = max(dp[k][i-1][j] for k in range(4))
                dp[0][i][j] = max(dp[0][i][j-1], M)
                dp[1][i][j] = max(dp[0][i][j-1]+v, dp[1][i][j-1], M+v)
                dp[2][i][j] = max(dp[1][i][j-1]+v, dp[2][i][j-1])
                dp[3][i][j] = max(dp[2][i][j-1]+v, dp[3][i][j-1])
    
            else:
                M = max(dp[k][i-1][j] for k in range(4))
                dp[0][i][j] = max(dp[0][i][j-1], M)
                dp[1][i][j] = dp[1][i][j-1]
                dp[2][i][j] = dp[2][i][j-1]
                dp[3][i][j] = dp[3][i][j-1]
    
    
    ans = max(dp[i][-1][-1] for i in range(4))
    print(ans)
main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

if __file__=="test.py":
    f = open("./in_out/input.txt", "r", encoding="utf-8")
    read = f.read
    readline = f.readline

def main():
    R, C, K = map(int, readline().split())
    
    L_INF = int(1e17)
    dp = [[[-L_INF for _ in range(C+1)] for _ in range(R+1)] for _ in range(4)]
    cell = [[0 for _ in range(C+1)] for _ in range(R+1)]
    
    for i in range(K):
        x, y, c = map(int, readline().split())
        cell[x-1][y-1] = c
    
    dp[0][0][1] = dp[0][1][0] = 0
    
    for i in range(1, R + 1):
        for j in range(1, C + 1):
            for k in range(4):
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j])
                dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if k > 0:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + cell[i-1][j-1])
                if k == 1:
                    dp[1][i][j] = max(dp[1][i][j], dp[3][i-1][j] + cell[i-1][j-1])
 
    print(dp[3][R][C])

if __name__ == "__main__":
    main()