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:
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=p=0
        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:
def main():
    import sys
    input = sys.stdin.buffer.readline
    
    r, c, k = map(int, input().split())
    tizu = [0 for _ in range(r * c)]
    for _ in range(k):
        rr, cc, v = map(int, input().split())
        tizu[(rr - 1) * c + (cc - 1)] = v
        
    dp = [[0, 0, 0, 0] for _ in range(c)]
    dp[0][1] = tizu[0]
    
    for i in range(r):
        for j in range(c):
            v = tizu[i * c + j]
            if v > 0:
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, tmp + v, 0, 0]
                if j != 0:
                    tmp0 = dp[j - 1][0]
                    tmp1 = dp[j - 1][1]
                    tmp2 = dp[j - 1][2]
                    lst = [max(dp[j][0], tmp0), max(dp[j][1], tmp1, tmp0 + v), max(tmp2, tmp1 + v), max(dp[j - 1][3], tmp2 + v)]
                    dp[j] = lst
            else:
                tmp = 0
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, 0, 0, 0]
                if j != 0:
                    dp[j] = dp[j - 1]
                    dp[j][0] = max(dp[j][0], tmp)
    print(max(dp[-1]))
    
main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, k = map(int, input().split())
    grid = [[0] * W for _ in range(H)]
    for _ in range(k):
        r, c, v = map(int, input().split())
        grid[r - 1][c - 1] = v

    # dp = [[[0] * 4 for _ in range(W)] for _ in range(H)]
    dp = [[[0] * W for _ in range(H)] for _ in range(4)]
    if grid[0][0] != 0:
        dp[1][0][0] = grid[0][0]
    for h in range(H):
        for w in range(W):
            if grid[h][w] != 0:
                for i in range(4):
                    if h - 1 >= 0:
                        # 取らない
                        dp[0][h][w] = max(dp[0][h][w], dp[i][h - 1][w])
                        # 取る
                        dp[1][h][w] = max(dp[1][h][w], dp[i][h - 1][w] + grid[h][w])
                for j in range(1, 4):
                    if w - 1 >= 0:
                        # 取らない
                        dp[j][h][w] = max(dp[j][h][w], dp[j][h][w - 1])
                        # 取る
                        dp[j][h][w] = max(dp[j][h][w], dp[j - 1][h][w - 1] + grid[h][w])
    res = 0
    for h in range(H):
        for w in range(W):
            for i in range(4):
                res = max(res, dp[i][h][w])
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

R,C,K = IL()
data = [[0 for i in range(C)] for j in range(R)]
for _ in range(K):
  j,i,v = IL()
  data[j-1][i-1] = v

dp = [[[0,0,0,0] for i in range(C+1)] for j in range(R+1)]
for j in range(R):
  for i in range(C):
    v = data[j][i] 
    if v == 0:
      dp[j+1][i+1][0] = max(dp[j+1][i][1],dp[j+1][i][0],max(dp[j][i+1])+v)
      dp[j+1][i+1][1] = max(dp[j+1][i][1],dp[j+1][i][0],max(dp[j][i+1])+v)
      dp[j+1][i+1][2] = max(dp[j+1][i][2],dp[j+1][i][1],max(dp[j][i+1])+v)
      dp[j+1][i+1][3] = max(dp[j+1][i][3],dp[j+1][i][2],max(dp[j][i+1])+v)
    else:
      dp[j+1][i+1][0] = dp[j][i][0]
      dp[j+1][i+1][1] = max(dp[j+1][i][1],dp[j+1][i][0]+v,max(dp[j][i+1])+v)
      dp[j+1][i+1][2] = max(dp[j+1][i][2],dp[j+1][i][1]+v,max(dp[j][i+1])+v)
      dp[j+1][i+1][3] = max(dp[j+1][i][3],dp[j+1][i][2]+v,max(dp[j][i+1])+v)
print(max(dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
import heapq
r,c,k = map(int,input().split())
#rcv = [[int(i) for i in input().split()] for j in range(k)]

dp = [[0 for i in range(c+1)] for j in range(r+1)]

val = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(k):
    r1,c1,v1 = map(int,input().split())
    val[r1][c1] = v1
    
#print(val)
    
for i in range(1,r+1):
    li = []
    heapq.heapify(li)
    num = 0
    for j in range(1,c+1):
        #print(val[i][j])
        if val[i][j] > 0:
            if num == 3:
                tmp = heapq.heappop(li)
                if tmp < val[i][j]:
                    if dp[i-1][j]+val[i][j] < dp[i][j-1]+val[i][j]-tmp:
                        dp[i][j] = dp[i][j-1]+val[i][j]-tmp
                        heapq.heappush(li,val[i][j])
                    else:
                        num = 1
                        li = [val[i][j]]
                        heapq.heapify(li)                        
                        dp[i][j] = dp[i-1][j]+val[i][j]
                else:
                    if dp[i-1][j]+val[i][j] < dp[i][j-1]:
                        dp[i][j] = dp[i][j-1]
                        heapq.heappush(li,tmp)
                    else:
                        num = 1
                        li = [val[i][j]]
                        heapq.heapify(li)
                        dp[i][j] = dp[i-1][j]+val[i][j]
            else:
                if dp[i-1][j]+val[i][j] < dp[i][j-1]+val[i][j]:
                    num += 1
                    heapq.heappush(li,val[i][j])
                    dp[i][j] = dp[i][j-1]+val[i][j]
                else:
                    num = 1
                    li = [val[i][j]]
                    heapq.heapify(li)
                    dp[i][j] = dp[i-1][j]+val[i][j]
        else:
            dp[i][j] = max(dp[i-1][j],dp[i][j-1])
        
print(dp[r][c])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
import copy
# from fractions import gcd

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

r, c, t = map(int, input().split())
A = [list(map(int, input().split())) for i in range(t)]
p = dict()
for i in range(t):
    p[(A[i][0], A[i][1])] = A[i][2]

dp = [[[0]*7 for i in range(c+1)] for j in range(r+1)]

for i in range(1, r+1):
    for j in range(1, c+1):
        if (i, j) == (0, 0):
            continue
        dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2],
                          dp[i][j-1][0], dp[i][j-1][4], dp[i][j-1][5])
        dp[i][j][1] = dp[i][j-1][1]
        if (i, j-1) in p:
            tmp = max(dp[i][j-1][0], dp[i][j-1][4], dp[i][j-1][5]) + p[(i, j-1)]
            dp[i][j][1] = max(dp[i][j][1], tmp)
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + p[(i, j - 1)] if (i, j - 1) in p else -1)
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + p[(i, j - 1)] if (i, j - 1) in p else -1)

        dp[i][j][4] = dp[i-1][j][4]
        if (i-1, j) in p:
            tmp = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2]) + p[(i-1, j)]
            dp[i][j][4] = max(dp[i][j][4], tmp)
        # dp[i][j][4] = max(dp[i-1][j][4], dp[i-1][j][0] + p[(i-1, j)] if (i-1, j) in p else -1)
        dp[i][j][5] = max(dp[i-1][j][5], dp[i-1][j][4] + p[(i-1, j)] if (i-1, j) in p else -1)
        dp[i][j][6] = max(dp[i-1][j][6], dp[i-1][j][5] + p[(i-1, j)] if (i-1, j) in p else -1)

        # for k in range(7):
            # dp[i][j][k] = max(dp[i][j-1][k], dp[i-1][j][k])
            # if k in [1,2,3,5,6,7,9,10,11,13,14,15] and (i, j-1) in p and (k == 1 or dp[i][j-1][k-1] != 0):
            #     dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + p[(i, j-1)])
            # if k >= 4 and (i-1, j) in p and (k == 4 or dp[i-1][j][k-4] != 0):
            #     dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-4] + p[(i-1, j)])

# for i in range(1, r+1):
#     for j in range(1, c+1):
#         print(dp[i][j])

if (r, c) not in p:
    print(max(dp[-1][-1]))
    exit()

vmax = 0
for k in range(7):
    if k in [0,1,2,4,5]:
        vmax = max(vmax, dp[-1][-1][k]+p[(r, c)])
    else:
        vmax = max(vmax, dp[-1][-1][k])

print(vmax)

Submission status: Wrong Answer

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


def main():
    r, c, k = map(int, input().split())
    values = [[0] * c for _ in range(r)]
    for _ in range(k):
        y, x, v = map(int, input().split())
        values[y-1][x-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 y in range(r):
        for x in range(c):
            value = values[y][x]
            for table1, table2 in zip([DP0, DP1, DP2], [DP1, DP2, DP3]):
                # そのまま右に
                if table1[y][x] > table1[y][x+1]:
                    table1[y][x+1] = table1[y][x]

                # 1個とって右に行く
                if table1[y][x] + value > table2[y][x+1]:
                    table2[y][x+1] = table1[y][x] + value

            # 下に行く
            DP0[y+1][x] = max(DP0[y][x+1], DP1[y][x+1], DP2[y][x+1], DP3[y][x+1])

    ans = DP0[r][c-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# R, C: H, W

H, W, K = map(int, input().split())
YXV = [list(map(int, input().split())) for _ in range(K)]
# H, W, K = 3000, 3000, 2*10**5
# YXV = [(i//H, i%W, i) for i in range(K)]

item = [[0 for _ in range(W+2)] for _ in range(H+2)]
for y, x, v in YXV:
    item[y][x] = v

# [print(item_i) for item_i in item]

dp = [[[0]*4 for _ in range(W+2)] for _ in range(H+2)]
dp[1][1][1] = item[1][1] 
for ty in range(1, H+1):
    for tx in range(1, W+1):
        # アイテムを拾わない
        # print(f"pos: ({tx}, {ty})", end="\r")
        for k in range(4):
            dp[ty+1][tx][0] = max(
                dp[ty+1][tx][0],
                dp[ty][tx][k]
            )
            dp[ty][tx+1][k] = max(
                dp[ty][tx+1][k],
                dp[ty][tx][k]
            )
        # print((x, y), item[y][x])
        if item[ty][tx] == 0:
            continue
        # アイテムを拾う
        for k in range(4):
            dp[ty+1][tx][0] = max(
                dp[ty+1][tx][0],
                dp[ty][tx][k] + item[ty+1][tx]
            )
        for k in range(3):
            dp[ty][tx+1][k+1] = max(
                dp[ty][tx+1][k+1],
                dp[ty][tx][k] + item[ty][tx+1]
            )

# for dp_i in dp:
#     print(dp_i)

print(max(dp[H][W]))

Submission status: Wrong Answer

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)]
	# 初期
	if items[0][0] != 0:
		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] = DP[r - 1][0][1] + 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: 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]*4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for k in range(C):
        dp[i][k][0] = max(dp[i][k-1][0],max(dp[i-1][k]))
        dp[i][k][1] = dp[i][k-1][1]
        dp[i][k][2] = dp[i][k-1][2]
        dp[i][k][3] = dp[i][k-1][3]
        if grid[i][k] != 0:
            dp[i][k][1] = max(dp[i][k-1][0]+grid[i][k],max(dp[i-1][k])+grid[i][k],dp[i][k][1])
            dp[i][k][2] = max(dp[i][k-1][1]+grid[i][k],dp[i][k][2])
            dp[i][k][3] = max(dp[i][k-1][2]+grid[i][k],dp[i][k][3])
print(max(dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
R, C, ite = map(int, input().split())
items = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(ite):
    r, c, v = map(int, input().split())
    items[r-1][c-1] = v

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
dp[0][0][1] = items[0][0]
for j in range(C):
    for i in range(R):
        for k in range(4):
            if i == 0 and j == 0:
                continue

            elif i == 0:
                if items[i][j] > 0:  
                    if k == 0:
                        dp[i][j][k] = dp[i][j-1][k]
                    else:
                        dp[i][j][k] = max(
                            dp[i][j-1][k],
                            dp[i][j-1][k-1] + items[i][j]
                        )
                else:
                    dp[i][j][k] = dp[i][j-1][k]

            elif j == 0:
                if items[i][j] > 0:  
                    if k == 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],
                        )
                    if k == 1:
                        dp[i][j][k] = max(
                            dp[i-1][j][0] + items[i][j],
                            dp[i-1][j][1] + items[i][j],
                            dp[i-1][j][2] + items[i][j],
                            dp[i-1][j][3] + items[i][j],
                        )
                else:
                    if k == 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],
                        )
            else:
                if items[i][j] > 0:
                    if k == 0:          
                        dp[i][j][k] = max(
                            dp[i-1][j][0],
                            dp[i-1][j][1],
                            dp[1-1][j][2],
                            dp[i-1][j][3],
                            dp[i][j-1][k]
                        )
                    elif k == 1:
                        dp[i][j][k] = max(
                            dp[i-1][j][0] + items[i][j],
                            dp[i-1][j][1] + items[i][j],
                            dp[1-1][j][2] + items[i][j],
                            dp[i-1][j][3] + items[i][j],
                            dp[i][j-1][k],
                            dp[i][j-1][k-1] + items[i][j]
                        )
                    else:
                        dp[i][j][k] = max(
                            dp[i][j-1][k-1] + items[i][j],
                            dp[i][j-1][k]
                        )
                else:
                    if k == 0:
                        dp[i][j][k] = max(
                            dp[i-1][j][0],
                            dp[i-1][j][1],
                            dp[1-1][j][2],
                            dp[i-1][j][3],
                            dp[i][j-1][k]
                        )
                    else:
                        dp[i][j][k] = dp[i][j-1][k]


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

Submission status: Wrong Answer

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)]
	# 初期
	if items[0][0] != 0:
		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] = DP[r - 1][0][1] + 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(DP[r][c - 1][0], prev) + val
			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: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    
    r, c, k = map(int, input().split())
    tizu = [0 for _ in range(r * c)]
    for _ in range(k):
        rr, cc, v = map(int, input().split())
        tizu[(rr - 1) * c + (cc - 1)] = v
        
    dp = [[0, 0, 0, 0] for _ in range(c)]
    dp[0][1] = tizu[0]
    
    for i in range(r):
        for j in range(c):
            v = tizu[i * c + j]
            if v > 0:
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, tmp + v, 0, 0]
                if j != 0:
                    tmp0 = dp[j - 1][0]
                    tmp1 = dp[j - 1][1]
                    tmp2 = dp[j - 1][2]
                    lst = [max(dp[j][0], tmp0), max(dp[j][1], tmp1, tmp0 + v), max(tmp2, tmp1 + v), max(dp[j - 1][3], tmp2 + v)]
                    dp[j] = lst
            else:
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, 0, 0, 0]
                if j != 0:
                    tmp = dp[j][0]
                    dp[j] = dp[j - 1]
                    dp[j][0] = max(dp[j][0], tmp)
    print(max(dp[-1]))
    
main()

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
R,C,K=f()
g=[[0]*R for _ in range(C)]
for _ in range(K):
  r,c,v=f()
  g[c-1][r-1]=v
# print(g)
from heapq import *
dpo=[(0,0,[])]*(R+1)
for c in range(C):
  dpn=[(0,0,[])]
  for r in range(R):
    cp,cc,cq=dpo[r+1]
    rp,rc,rq=dpn[r]
    t=g[c][r]
    if t<1:
      if cp>rp: dpn+=[(cp,cc,cq)]
      else: dpn+=[(rp,0,[])]
      continue
    if cc>2:
      heappush(cq,t)
      dpn+=[(cp-heappop(cq)+t,cc+1,cq)]
    elif cp>rp:
      heappush(cq,t)
      dpn+=[(cp+t,cc+1,cq)]
    else: dpn+=[(rp+t,1,[t])]
  dpo=dpn
  # print(dpn)
print(dpn[R][0])

Submission status: Wrong Answer

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

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

for i in range(K):
    r, c, k = list(map(int, input().split()))
    item[r][c] = k

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if i == 1 and j == 1:
            dp[i][j][0] = 0
            if item[i][j] > 0:
                dp[i][j][1] = item[i][j]
        
        elif i == 1:
            dp[i][j][0] = 0
            if item[i][j] > 0:
                for p in range(3):
                    if dp[i][j-1][p] != -1:
                        if p > 0:
                            dp[i][j][p+1] = max(dp[i][j-1][p] + item[i][j], dp[i][j-1][p+1])
                        else:
                            dp[i][j][p+1] = max(dp[i][j-1][p] + item[i][j], dp[i][j][0] + item[i][j], dp[i][j-1][p+1])
            else:
                for p in range(3):
                        dp[i][j][p+1] =  dp[i][j-1][p+1]
        elif j == 1:
            #print(dp[i-1][j])
            dp[i][j][0] = max(dp[i-1][j])
            if item[i][j] > 0:
                dp[i][j][1] = dp[i-1][j][0] + item[i][j]
        
        else:
            dp[i][j][0] = max(dp[i][j-1][0], max(dp[i-1][j]))
            if item[i][j] > 0:
                for p in range(3):
                    if dp[i][j-1][p] != -1:
                        if p > 0:
                            dp[i][j][p+1] = max(dp[i][j-1][p] + item[i][j], dp[i][j-1][p+1])
                        else:
                            dp[i][j][p+1] = max(dp[i][j-1][p] + item[i][j], dp[i][j][0] + item[i][j], dp[i][j-1][p+1])
                    # if i == R and j == C-1:
                    #     print(dp[i][j-1][p] + item[i][j], dp[i][j-1][p+1])
            else:
                for p in range(3):
                        dp[i][j][p+1] =  dp[i][j-1][p+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=p=0
        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]=dp[i-1][j]+v
            else:
                dp[i][j]=x+y+z+p
    print(dp[h][w])
main()

Submission status: Wrong Answer

Solution code:
def solve():
    from collections import deque, defaultdict

    def bfs(s, g):
        scores = []
        move = [(0, 1), (1, 0)]
        queue = deque([[V[s], s]])
        while queue:
            d, p = queue.pop()
            if p == g:
                scores.append(d)
            x, y = p
            for dx, dy in move:
                nx, ny = x + dx, y + dy
                q = (nx, ny)
                if not nx < R + 1 or not ny < C + 1:
                    continue
                queue.append([d + V[q], q])
        return scores

    R, C, k = map(int, input().split())
    V = defaultdict(int)
    for i in range(k):
        rr, cc, vv = map(int, input().split())
        V[(rr, cc)] = vv
    st = (1, 1)
    g = (R, C)

    res = bfs(st, g)
    print(max(res))


solve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import numpy as np
import itertools

def main():
    R, C, K = map(int, input().split())
    matrix = np.zeros((R, C))
    for k in range(K):
        r_i, c_i, v_i = map(int, input().split())
        matrix[r_i-1, c_i-1] = v_i
    matrix = tuple(map(tuple, matrix))
    
    dp = np.zeros((4, R, C))
    for r, c in itertools.product(range(R), range(C)):
        tmp = matrix[r][c]
        if r == 0:
            if tmp == 0:
                dp[0, r, c] = dp[0, r, c-1]
                dp[1, r, c] = dp[1, r, c-1]
                dp[2, r, c] = dp[2, r, c-1]
                dp[3, r, c] = dp[3, r, c-1]
            else:
                dp[0, r, c] = 0
                dp[1, r, c] = max(tmp, dp[1, r, c-1])
                dp[2, r, c] = max(dp[1, r, c-1] + tmp, dp[2, r, c-1])
                dp[3, r, c] = max(dp[2, r, c-1] + tmp, dp[3, r, c-1])
        else:
            if tmp == 0:
                dp[0, r, c] = max([dp[0, r, c-1], dp[0, r-1, c], dp[1, r-1, c], dp[2, r-1, c], dp[3, r-1, c]])
                dp[1, r, c] = dp[1, r, c-1]
                dp[2, r, c] = dp[2, r, c-1]
                dp[3, r, c] = dp[3, r, c-1]
            else:
                dp[0, r, c] = max([dp[0, r, c-1], dp[0, r-1, c], dp[1, r-1, c], dp[2, r-1, c], dp[3, r-1, c]])
                dp[1, r, c] = max([dp[0, r, c-1] + tmp, dp[1, r, c-1], dp[0, r-1, c] + tmp, dp[1, r-1, c] + tmp, dp[2, r-1, c] + tmp, dp[3, r-1, c] + tmp])
                dp[2, r, c] = max(dp[1, r, c-1] + tmp, dp[2, r, c-1])
                dp[3, r, c] = max(dp[2, r, c-1] + tmp, dp[3, r, c-1])
    print(int(max([dp[0, R-1, C-1], dp[1, R-1, C-1], dp[2, R-1, C-1], dp[3, R-1, C-1]])))
    
main()

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split(" "))
board = [ [0]*(C+1) for _ in range(R+1) ]
for _ in range(K) :
    i, j, v = map(int, input().split(" "))
    board[i][j] = v

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

for i in range(1, R+1) :
    for j in range(1, C+1) : 
        if board[i][j] == 0 :
            for k in range(4) :
                score[i][j][k] = score[i][j-1][k]    
        else :
            for k in range(3):
                score[i][j][k+1] = score[i][j-1][k] + board[i][j]
        
        descent = max(score[i-1][j])
        score[i][j][1] = max(descent + board[i][j], score[i][j-1][1], score[i][j-1][0]+board[i][j])
        score[i][j][0] = max(descent, score[i][j-1][0])



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

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=p=0
        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]=dp[i-1][j]+v
            else:
                dp[i][j]=x+y+z+p
    print(dp[h][w])
main()

Submission status: Wrong Answer

Solution code:

R, C, K = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(K)]

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

for r, c, v in X:
    items[r][c] = v

for i in range(R):
    for j in range(C):
        for k in reversed(range(4)):
            # Horizontal
            dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k], dp[i][j + 1][k])
            if items[i + 1][j + 1] > 0 and k < 3:
                dp[i + 1][j + 1][k + 1] = max(
                    dp[i + 1][j + 1][k + 1], dp[i + 1][j][k] + items[i + 1][j + 1])

            # Vertical
            dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i + 1][j][k])
            if items[i + 1][j + 1] > 0:
                dp[i + 1][j + 1][1] = max(
                    dp[i + 1][j + 1][1], dp[i][j + 1][k] + items[i + 1][j + 1])

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