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: Wrong Answer

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    r, c, k = ns()

    mat = [[0] * (c + 1)] * (r + 1)
    for _ in range(k):
        x, y, v = ns()
        x, y = x - 1, y - 1
        mat[x][y] = v

    dp = [[0] * 4] * (c + 1)

    for i in range(r):
        ndp = [[0] * 4] * (c + 1)
        for j in range(c):
            for n in range(2, -1, -1):
                dp[j][n + 1] = max(dp[j][n + 1], dp[j][n] + mat[i][j])

            ndp[j][0] = max(dp[j])
            for n in range(4):
                dp[j + 1][n] = max(dp[j + 1][n], dp[j][n])
        dp = ndp

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 入力
R, C, K = map(int, input().split())
point_map = [[0 for _ in range(C+1)] for _ in range(R+1)]
max_point = [[[0 for _ in range(4)] for _ in range(C + 1)]
             for _ in range(R + 1)]
maxmax_point = [[0 for _ in range(C + 1)] for _ in range(R + 1)]


for i in range(K):
    _r, _c, _k = map(int, input().split())
    point_map[_r][_c] = _k

for i in range(1, R+1):
    itemss = [[]]
    im = i-1
    for j in range(1, C + 1):
        jm = j-1
        point = point_map[i][j]
        max_point[i][j][0] = max(
            max_point[i][jm][0], maxmax_point[im][j])
        if point == 0:
            max_point[i][j][1] = max_point[i][jm][1]
            max_point[i][j][2] = max_point[i][jm][2]
            max_point[i][j][3] = max_point[i][jm][3]
        else:
            itemss = [[]] + [items + [point]
                             for items in itemss]  # items[1] アイテム一つ
            itemss_len = len(itemss)

            max_point[i][j][1] = max(
                max_point[i][jm][0] + point, maxmax_point[i - 1][j]+point)
            max_point[i][j][2] = max_point[i][jm][1] + point
            max_point[i][j][3] = max_point[i][jm][2] + point

            if itemss_len > 4:
                items = itemss[4][:]  # 4 こアイテムがあるやつ
                del_item = min(items)
                items.remove(del_item)
                sa = max_point[i][jm][3]-del_item
                if sa > max_point[i][jm][3]:
                    max_point[i][j][3] = sa + point
                    itemss = itemss[:3] + items
                else:
                    itemss = itemss[:4]
        # [print(i, j, n, max_point[i][j][n])
        # for n in range(min(len(itemss), 4))]
        maxmax_point[i][j] = max(max_point[i][j])
        # print(i, j, maxmax_point[i][j])

print(maxmax_point[R][C])

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
goods = [[0] * (C + 1) for i in range(R + 1)]
for i in range(1, K + 1):
    _r, _c, _v = map(int, input().split())
    goods[_r][_c] = _v
dp = [[[0] * 4 for i in range(C + 1)] for j in range(R + 1)]
dp[1][1][1] = goods[1][1]
for i in range(1, R + 1):
    for j in range(1, C + 1):
        dp[i][j][0] = max(max(dp[i-1][j]), dp[i][j-1][0])
        if goods[i][j] != 0:
            dp[i][j][1] = max(dp[i][j][1], max(max(dp[i - 1][j]), dp[i][j - 1][0]) + goods[i][j], dp[i][j-1][1])
            dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + goods[i][j], dp[i][j-1][2])
            dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + goods[i][j], dp[i][j-1][3])
        else:
            dp[i][j][1] = max(dp[i][j][1], dp[i][j-1][1])
            dp[i][j][2] = max(dp[i][j][2], dp[i][j-1][1])
            dp[i][j][3] = max(dp[i][j][3], dp[i][j-1][1])
print(max(dp[R][C]))
#print(dp)

Submission status: Wrong Answer

Solution code:
def main():
    (h,w,k),*t=[[*map(int,t.split())]for t in open(0)]
    dp=[[0]*(w+1)for _ in range(h+1)]
    for y,x,v in t:
        dp[y][x]=v
    for i in range(1,h+1):
        x=y=z=0
        p=dp[i-1][1]
        for j in range(1,w+1):
            v=dp[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<=dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
    print(dp[h][w])
main()

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())

A = [[0 for _ in range(M + 1)] for _ in range(N + 1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    A[r - 1][c - 1] = v

# dp[r, c, k] = 到達 (r, c)，且 (r, c) 以左 N 格拾取的物件數為 K 時，所能達到的最大價值
dp = [[[0 for _ in range(5)] for _ in range(M + 1)] for _ in range(N + 1)]

dp[0][0][1] = A[0][0]

for r in range(N):
    for c in range(M):
        for k in range(4):
            dp[r][c + 1][k + 1] = max(
                dp[r][c + 1][k + 1], dp[r][c][k] + A[r][c + 1]
            )  # go right and pick
            dp[r][c + 1][k] = max(
                dp[r][c + 1][k], dp[r][c][k]
            )  # go right and don't pick
            dp[r + 1][c][1] = max(
                dp[r + 1][c][1], dp[r][c][k] + A[r + 1][c]
            )  # go down and pick
            dp[r + 1][c][0] = max(
                dp[r + 1][c][0], dp[r][c][k]
            )  # go down and don't pick

print(max(dp[N - 1][M - 1]))

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())

A = [[0 for _ in range(M + 1)] for _ in range(N + 1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    A[r - 1][c - 1] = v

# dp[r, c, k] = 到達 (r, c)，且 (r, c) 以左 N 格拾取的物件數為 K 時，所能達到的最大價值
dp = [[[0 for _ in range(5)] for _ in range(M + 1)] for _ in range(N + 1)]

dp[0][0][1] = A[0][0]

for r in range(N):
    for c in range(M):
        for k in range(4):
            dp[r][c + 1][k + 1] = max(
                dp[r][c + 1][k + 1], dp[r][c][k] + A[r][c + 1]
            )  # go right and pick
            dp[r][c + 1][k] = max(
                dp[r][c + 1][k], dp[r][c][k]
            )  # go right and don't pick
            dp[r + 1][c][1] = max(
                dp[r][c + 1][1], dp[r][c][k] + A[r + 1][c]
            )  # go down and pick
            dp[r + 1][c][0] = max(
                dp[r + 1][c][0], dp[r][c][k]
            )  # go down and don't pick

print(max(dp[N - 1][M - 1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
goods = [[0] * (C + 1) for i in range(R + 1)]
for i in range(1, K + 1):
    _r, _c, _v = map(int, input().split())
    goods[_r][_c] = _v
dp = [[[0] * 4 for i in range(C + 1)] for j in range(R + 1)]
dp[1][1][1] = goods[1][1]
for i in range(1, R + 1):
    for j in range(1, C + 1):
        for k in range(1, 4):
            if goods[i][j] != 0:
                dp[i][j][k] = 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][k-1])
                dp[i][j][k] += goods[i][j]
            else:
                dp[i][j][k] = 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][k])

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

Submission status: Wrong Answer

Solution code:
from copy import copy

C,R,K=map(int,input().split())
dp=[[0]*(R+1) for i1 in range(4)]
M=[[0]*R for i in range(C)]
for i in range(K):
    r,c,v=map(int,input().split())
    M[r-1][c-1]=v
for i,l in enumerate(M):
    for num in range(1,4):
        for j in range(R):
            dp[num][j]= max(dp[num][j-1],dp[num-1][j],dp[num-1][j-1]+M[i][j])
    dp[0]=dp[-1][1:-1]+[0,dp[-1][0]]

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

Submission status: Wrong Answer

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

v = [[0 for _ in range(c)]for __ in range(r)]

for _ in range(k):
    ri,ci,vi = map(int,input().split())
    v[ri-1][ci-1] = vi

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

if v[0][0]:
    dp[1][0][0] = v[0][0]

for h in range(1,r):
    ma = 0
    for i in range(4):
        ma = max(ma,dp[i][h-1][0])
    if v[h][0]:
        dp[1][h][0] = dp[0][h][0] + v[h][0]

for w in range(1,c):
    for i in range(4):
        dp[i][0][w] = dp[i][0][w-1]
    if v[0][w]:
        for i in range(3,0,-1):
            dp[i][0][w] = max(dp[i][0][w],dp[i-1][0][w] + v[0][w])

for h in range(1,r):
    for w in range(1,c):
        ma = 0
        for i in range(4):
            ma = max(ma,dp[i][h-1][w])
        for cnt in range(4):
            dp[cnt][h][w] = max(dp[cnt][h][w-1],ma)
        if v[h][w]:
            for cnt in range(3,0,-1):
                dp[cnt][h][w] = max(dp[cnt][h][w],dp[cnt-1][h][w] + v[h][w])

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

Submission status: Wrong Answer

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 for _ in range(R)] for _ in range(4)]
for i in range(R):
    for k in range(C):
        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][k]
        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])
            dp[2][i][k] = max(dp[1][i][k-1]+target,dp[2][i][k])
            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: Wrong Answer

Solution code:
r,c,k = map(int,input().split())
dp = [[[0 for _ in range(4)] for _ in range(c)] for _ in range(r)]
g = [[0]*c for _ in range(r)]
for _ in range(k):
    a,b,v = map(int,input().split())
    g[a-1][b-1] = v
if g[0][0] != 0:
    dp[0][0][1] = g[0][0]
for i in range(1,r):
    dp[i][0][1] = max(dp[i-1][0]) + g[i][0]
for j in range(1,c):
    dp[0][j][0] =dp[0][j-1][0]
    for m in range(1,4):
        if dp[0][j-1][m-1]!=0:
            dp[0][j][m] = max(dp[0][j-1][m-1] + g[0][j],dp[0][j-1][m]) 
        else:
            dp[0][j][m] = dp[0][j-1][m]

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

Submission status: Wrong Answer

Solution code:
def main(R,C,K,V):
    dp = [0]*(4*R*C)
    dp[1+4*0+4*C*0] = V[0][0]
    for k in range(4*R*C):
        n = k%4
        j = k//4%C
        i = k//(4*C)
        if n!=0:
            if i != 0 and n==1:
                dp[k] = max(dp[k-4*C-n:k-4*C-n+4])+V[i][j]
            if j != 0:
                dp[k] = max(dp[k],dp[k-4],dp[k-5]+V[i][j])
        if n==0:
            if i != 0:
                dp[k] = max(dp[k-4*C-n:k-4*C-n+4])

    print(max(dp[-5:-1]))

if __name__ == '__main__':
    R,C,K = map(int,input().split())
    rcv = [list(map(int,input().split())) for _ in range(K)]
    V = [[0]*C for _ in range(R)]
    for i in range(K):
        r,c,v = rcv[i]
        r -= 1
        c -= 1
        V[r][c] = v
    main(R,C,K,V)

Submission status: Wrong Answer

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]*5 for i in range(c+1)] for j in range(r+1)]
	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:
				dp[rr][cc][1]=dp[rr][cc-1][1]
				dp[rr][cc][2]=dp[rr][cc-1][2]
				dp[rr][cc][3]=dp[rr][cc-1][3]
				dp[rr][cc][4]=max(dp[rr][cc][1:4])
			else:
				dp[rr][cc][1]=max(dp[rr-1][cc][4]+G[rr][cc],dp[rr][cc-1][1])
				dp[rr][cc][2]=max(dp[rr][cc-1][1]+G[rr][cc],dp[rr][cc-1][2])
				dp[rr][cc][3]=max(dp[rr][cc-1][2]+G[rr][cc],dp[rr][cc-1][3])
				dp[rr][cc][4]=max(dp[rr][cc][1:4])
	#print(dp)
	print(dp[-1][-1][-1])















if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque


class Top3:
    def __init__(self, ls):
        self.ls = ls

    def append(self, x):
        # xを突っ込む 3つのままに保つ
        # ついでに取れなくなったもののスコアを吐き出す
        self.ls.append(x)
        mi = min(self.ls)
        self.ls.remove(mi)
        return mi

    @property
    def sum(self):
        return sum(self.ls)


'''
dp[i][j]...(0,0)からスタートして(i,j)に到達する経路のなかでの最大の価値
dp[i][j]=max(dp[i][j-1],dp[i-1][j]) + v[i][j]

同じ行のアイテムは3つしか拾えないのはどうしようか...
各行でアイテムの価値をsortedlistに突っ込んでおくのは？ top3つ持ってればいいからそんなデータ構造いらない

dpだけじゃなくて行方向にいままでどんな要素を選んだか記録する必要あり
ritmとする。
横方向の移動があったときだけ中身を継承する(これはスコア算出に使う)
'''

R, C, K = ints()
RCV = read_tuple(K)
# values = defaultdict(lambda: 0)
values = [[0] * C for _ in ra(R)]
for r, c, v in RCV:
    values[r - 1][c - 1] = v

dp = [[-1] * (C + 5) for _ in ra(R + 5)]
dp[0][0] = values[0][0]
for i in ra(1, R):
    dp[i][0] = values[i][0] + dp[i - 1][0]

top3 = Top3(ls=[values[0][0], 0, 0])
for j in ra(1, C):
    dp[0][j] = dp[0][j - 1] + values[0][j] - \
        top3.append(values[0][j])

for i in ra(1, R):
    top3 = Top3(ls=[values[i][0], 0, 0])  # 行のitemをもつ
    for j in ra(1, C):
        top = dp[i - 1][j] + values[i][j]
        left = dp[i][j - 1] + values[i][j] - \
            top3.append(values[i][j])  # top3を考慮した横のスコア
        # top3.append(values[i][j])
        # left = top3.sum + dp[i][j - 1]

        # もしtopが大きければtop3は採用しない
        if top >= left:
            dp[i][j] = top
            top3 = Top3(ls=[values[i][j], 0, 0])
        else:
            dp[i][j] = left
            # appendしたtop3を次も継続して使う

print(dp[R - 1][C - 1])
# print(*values, sep='\n')
# print(*dp, sep='\n')

Submission status: Wrong Answer

Solution code:
"""
dp[i][j]　=> i行目j列目がゴールの場合の最大値

"""

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

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

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

for i in range(R):
    memo = [0,0,0]
    for j in range(C):
        v = items[i][j]

        if i==0 and j == 0:
            dp[0][0] = v
            memo[2] = v
        
        if j != 0:
            #左から入る場合の価値を求める。
            dp[i][j] = dp[i][j-1]
            memo.sort()
            if v >= memo[0]:
                dp[i][j] += v-memo[0]
                memo[0] = v
        if i != 0:
            #上から入る場合の価値を求める。
            tmp2 = dp[i-1][j] + v
            if tmp2 > dp[i][j]:
                dp[i][j] = tmp2
                memo = [0,0,v]

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

Submission status: Wrong Answer

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

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

for i in range(R):
    for j in range(C):
        for n in range(4):
            if n!=0:
                dp[i][j][n] = max(dp[i-1][j][3]+V[i][j],dp[i][j-1][n],dp[i][j-1][n-1]+V[i][j])
            if n==0:
                dp[i][j][n] = dp[i-1][j][3]

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

Submission status: Wrong Answer

Solution code:
"""
dp[i][j]　=> i行目j列目がゴールの場合の最大値

"""

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

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

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

for i in range(R):
    memo = [0,0,0]
    for j in range(C):
        memo.sort()
        v = items[i][j]

        if i==0 and j == 0:
            dp[0][0] = v
            memo[2] = v
        
        if j != 0:
            #左から入る場合の価値を求める。
            dp[i][j] = dp[i][j-1]
            if v > memo[0]:
                dp[i][j] += v-memo[0]
                memo[0] = v
        if i != 0:
            #上から入る場合の価値を求める。
            tmp2 = dp[i-1][j] + v
            if tmp2 > dp[i][j]:
                dp[i][j] = tmp2
                memo = [0,0,v]

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

Submission status: Wrong Answer

Solution code:
#dp[i][j][k]=(i,j)にアイテムをk個以下使って移動するときの最大値
#入力
r,c,k=list(map(int,input().split()))

rck=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(k):
     r_num,c_num,k_num=list(map(int,input().split()))
     rck[r_num][c_num]=k_num

#dpテーブルと初期条件
dp=[[[0 for i in range(4)] for j in range(c+1)] for k in range(r+1)]
#dp
for i in range(1,r+1):
  for j in range(1,c+1):
    for k in range(1,4):
      dp[i][j][k]=max(dp[i-1][j][3]+rck[i][j],dp[i][j-1][k-1]+rck[i][j])
    #print(dp[i][j])
print(dp[r][c][3])

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
g = [[-float('inf')]*C for _ in range(R)]
for i in range(K):
    r, c, k = map(int, input().split())
    r -= 1
    c -= 1
    g[r][c] = k

dp = [[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]
for r in range(R):
    for c in range(C):
        # k[0..3] = 0個拾っている状態から3個拾っている状態
        # indexが大きいほど、小さいアイテムとなるので、小さいものから更新していく
        for k in range(2, -1, -1):
            dp[k+1][r][c] = max(
                dp[k+1][r][c],
                dp[k][r][c]+g[r][c]
            )

        for k in range(4):
            dp[0][r+1][c] = max(dp[0][r+1][c], dp[k][r][c])
            dp[k][r][c+1] = max(dp[k][r][c+1], dp[k][r][c])

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

r,c,k=MI()
val=[[0]*c for i in range(r)]
dp=[[[0]*4 for i in range(c)] for j in range(r)]
#print(dp)
for i in range(k):
    x,y,v=MI()
    val[x-1][y-1]=v
#print(val)
dp[0][0][0]=val[0][0]
dp[0][0][-1]=val[0][0]
#print(dp)
for i in range(r):
    for j in range(c):
        #print(dp)
        #print(i,j)
        if i==0 and j==0:
            continue
        if j==0:
            dp[i][j][0]=dp[i-1][j][0]+val[i][j]
            for k in range(3):
                dp[i][j][k+1]=0
        elif i==0:
            dp[i][j][0]=dp[i][j-1][0]+val[i][j]-dp[i][j-1][1]
            dp[i][j][1]=val[i][j]
            dp[i][j][2]=dp[i][j-1][2]
            dp[i][j][3]=dp[i][j-1][3]
            if dp[i][j][1]>dp[i][j][2]:
                dp[i][j][2],dp[i][j][1]=dp[i][j][1],dp[i][j][2]
            if dp[i][j][2]>dp[i][j][3]:
                dp[i][j][3],dp[i][j][2]=dp[i][j][2],dp[i][j][3]
            
        elif dp[i-1][j][0]+val[i][j]>=dp[i][j-1][0]+val[i][j]+dp[i][j-1][1]:
            dp[i][j][0]=dp[i-1][j][0]+val[i][j]
            for k in range(3):
                dp[i][j][k+1]=0
        else:
            dp[i][j][0]=dp[i][j-1][0]+val[i][j]-dp[i][j-1][1]
            dp[i][j][1]=val[i][j]
            dp[i][j][2]=dp[i][j-1][2]
            dp[i][j][3]=dp[i][j-1][3]
            if dp[i][j][1]>dp[i][j][2]:
                dp[i][j][2],dp[i][j][1]=dp[i][j][1],dp[i][j][2]
            if dp[i][j][2]>dp[i][j][3]:
                dp[i][j][3],dp[i][j][2]=dp[i][j][2],dp[i][j][3]
print(dp[-1][-1][0])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

r,c,k=MI()
val=[[0]*c for i in range(r)]
dp=[[[0]*4 for i in range(c)] for j in range(r)]
#print(dp)
for i in range(k):
    x,y,v=MI()
    val[x-1][y-1]=v
#print(val)
dp[0][0][0]=val[0][0]
dp[0][0][-1]=val[0][0]
#print(dp)
for i in range(r):
    for j in range(c):
        #print(dp)
        #print(i,j)
        if i==0 and j==0:
            continue
        if j==0:
            dp[i][j][0]=dp[i-1][j][0]+val[i][j]
            for k in range(3):
                dp[i][j][k+1]=0
        elif i==0:
            dp[i][j][0]=dp[i][j-1][0]+val[i][j]-dp[i][j-1][1]
            dp[i][j][1]=val[i][j]
            dp[i][j][2]=dp[i][j-1][2]
            dp[i][j][3]=dp[i][j-1][3]
            if dp[i][j][1]>dp[i][j][2]:
                dp[i][j][2],dp[i][j][1]=dp[i][j][1],dp[i][j][2]
            if dp[i][j][2]>dp[i][j][3]:
                dp[i][j][3],dp[i][j][2]=dp[i][j][2],dp[i][j][3]
            
        elif dp[i-1][j][0]+val[i][j]>=dp[i][j-1][0]+val[i][j]+dp[i][j-1][1]:
            dp[i][j][0]=dp[i-1][j][0]+val[i][j]
            for k in range(3):
                dp[i][j][k+1]=0
        else:
            dp[i][j][0]=dp[i][j-1][0]+val[i][j]-dp[i][j-1][1]
            dp[i][j][1]=val[i][j]
            dp[i][j][2]=dp[i][j-1][2]
            dp[i][j][3]=dp[i][j-1][3]
            if dp[i][j][1]>dp[i][j][2]:
                dp[i][j][2],dp[i][j][1]=dp[i][j][1],dp[i][j][2]
            if dp[i][j][2]>dp[i][j][3]:
                dp[i][j][3],dp[i][j][2]=dp[i][j][2],dp[i][j][3]
print(dp[-1][-1][0])
#print(dp)
#print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
r,c,k = map(int,input().split())
l = [[0]*c for i in range(r)] 
for i in range(k):
    x,y,v = map(int,input().split())
    l[x-1][y-1] = v
dp = [[0]*4 for i in range(c+1)]

for i in range(r):
    ndp = [[0]*4 for i in range(c+1)]
    for j in range(c):
        M = max(dp[j+1])
        if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,dp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,dp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,dp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0])
            ndp[j+1][1] = dp[j][1]
            ndp[j+1][2] = dp[j][2]
            ndp[j+1][3] = dp[j][3]
    dp = ndp
print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
R,C,K = map(int,input().split())
goods = [[0]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = map(int,input().split())
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if not goods[1][1]:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1] != -1:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if not goods[i+1][1]:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if not goods[i][j]:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            DP[i][j][1] = DP[i][j-1][1]
            DP[i][j][2] = DP[i][j-1][2]
            DP[i][j][3] = DP[i][j-1][3]
print(max(DP[R][C]))

Submission status: Wrong Answer

Solution code:
import sys
# import heapq, functools, collections
# import math, random
# from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(items,R,C):  # fix inputs here
    console("----- solving ------")
    console(R,C)

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

    for r,c,v in items:
        grid[r-1][c-1] = v

    # console(np.array(grid))  # PLEASE SILENCE
    console("\n\n")

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

    if grid[0][0] > 0:
        dp[0][0][1] = dp[0][0][0] + grid[0][0]

    for i in range(1,R):
        dp[i][0][0] = max(dp[i-1][0][0], dp[i-1][0][1])
        if grid[i][0] > 0:
            dp[i][0][1] = dp[i][0][0] + grid[i][0]
        maxx[i][0] = max(dp[i][0])

    for j in range(1,C):
        for k in range(1,4):
            dp[0][j][k] = max(dp[0][j-1][k], dp[0][j-1][k-1] + grid[0][j])
        maxx[0][j] = max(dp[0][j])

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp)[:,0,:])  # PLEASE SILENCE
    # console(np.array(dp)[0,:,:])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE
    # console("\n\nPost")

    for i in range(1,R):
        j_1 = 0
        for j in range(1,C):
            dp[i][j][0] = max(maxx[i-1][j], dp[i][j-1][0])
            if grid[i][j] > 0:
                dp[i][j][1] = max(dp[i][j_1][1], dp[i][j][0] + grid[i][j])
                for k in range(2,4):
                    dp[i][j][k] = max(dp[i][j_1][k], dp[i][j_1][k-1] + grid[i][j])
                j_1 = j
                maxx[i][j] = max(dp[i][j])
            else:
                maxx[i][j] = max(maxx[i][j-1], dp[i][j][0])

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,1])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,2])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,3])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE

    # return a string (i.e. not a list or matrix)
    return maxx[R-1][C-1]


def console(*args):  # the judge will not read these print statement
    # print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
inn = sys.stdin.readlines()

for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    r,c,nrows = list(map(int,inn[0].split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    grid = []
    for t in range(1,nrows+1):
        grid.append(list(map(int,inn[t].split())))

    res = solve(grid,r,c)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp0 = dp[0]; dp1 = dp[1]; dp2 = dp[2]; dp3 = dp[3]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 

        dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 

        dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 

ans = 0
for i in range(4):
    for j in range(R + 1):
        ans = max(ans, dp[i][j * C + C - 1])
print(ans)