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 heapq

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

cost = [[0 for i in range(C)] for j in range(R)] 

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

pick_up = [[[] for i in range(C)] for j in range(R)] 

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

for h in range(R):
    for w in range(C):
        if w == 0:
            dp[h+1][w] = dp[h][w]+cost[h][w]
            if cost[h][w] != 0:
                heapq.heappush(pick_up[h][w],cost[h][w])
        else:
            if len(pick_up[h][w-1]) < 3:
                dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w])
                if cost[h][w] != 0:
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        heapq.heappush(pick_up[h][w],cost[h][w])
                    else:
                        tmp_array = pick_up[h][w-1]
                        heapq.heappush(tmp_array,cost[h][w])
                        pick_up[h][w] = tmp_array
                else:
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        continue
                    else:
                        pick_up[h][w] = pick_up[h][w-1]
                    
            else:
                if cost[h][w] != 0:
                    tmp_array = pick_up[h][w-1]
                    minus = sum(tmp_array)
                    heapq.heappush(tmp_array,cost[h][w])
                    heapq.heappop(tmp_array)
                    dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]-minus+sum(tmp_array))
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        heapq.heappush(pick_up[h][w],cost[h][w])
                    else:
                        pick_up[h][w] = tmp_array
                    
                else:
                    dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w])
                    if dp[h+1][w] == dp[h][w]:
                        continue
                    else:
                        pick_up[h][w] = pick_up[h][w-1]
    
            
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
def main():
  R,C,K=map(int, input().split())
  M=[[0 for i in range(C)]for i in range(R)]
  for i in range(K):
    r,c,v=map(int, input().split())
    r-=1
    c-=1
    M[r][c]=v
  dp0=[[0 for j in range(C)] for i in range(R)]
  dp1=[[0 for j in range(C)] for i in range(R)]
  dp2=[[0 for j in range(C)] for i in range(R)]
  dp3=[[0 for j in range(C)] for i in range(R)]
  dp1[0][0]+=M[0][0]
  for r in range(R):
    for c in range(C):
      if 0<r:
        dp1[r][c]=max(dp3[r-1][c]+M[r][c],dp1[r][c])
        dp0[r][c]=max(dp3[r-1][c],dp0[r][c])
      if 0<c:
        dp3[r][c]=max(dp2[r][c-1]+M[r][c],dp3[r][c],dp3[r][c-1])
        dp2[r][c]=max(dp1[r][c-1]+M[r][c],dp2[r][c],dp2[r][c-1])
        dp1[r][c]=max(dp0[r][c-1]+M[r][c],dp1[r][c],dp1[r][c-1])
        dp0[r][c]=max(dp0[r][c-1],dp0[r][c])
  ans=0
  for i in range(R):
    for j in range(C):
      ans=max(ans,dp1[i][j],dp2[i][j],dp3[i][j])
  print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
R,C,K=map(int, input().split())
M=[[0 for i in range(C)]for i in range(R)]
print(M)
for i in range(K):
  r,c,v=map(int, input().split())
  r-=1
  c-=1
  M[r][c]=v
dp=[[[0]*(4) for j in range(C)] for i in range(R)]
dp[0][0][1]+=M[0][0]
for r in range(R):
  for c in range(C):
    if 0<r:
      dp[r][c][1]=max(max(dp[r-1][c])+M[r][c],dp[r][c][1])
      dp[r][c][0]=max(max(dp[r-1][c]),dp[r][c][0])
    if 0<c:
      dp[r][c][3]=max(dp[r][c-1][2]+M[r][c],dp[r][c][3],dp[r][c-1][3])
      dp[r][c][2]=max(dp[r][c-1][1]+M[r][c],dp[r][c][2],dp[r][c-1][2])
      dp[r][c][1]=max(dp[r][c-1][0]+M[r][c],dp[r][c][1],dp[r][c-1][1])
      dp[r][c][0]=max(dp[r][c-1][0],dp[r][c][0])
print(max(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)]

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

dp0 = [[0] * (C + 1) for _ in range(R + 1)]
dp1 = [[0] * (C + 1) for _ in range(R + 1)]
dp2 = [[0] * (C + 1) for _ in range(R + 1)]
dp3 = [[0] * (C + 1) for _ in range(R + 1)]

for r in range(1, R + 1):
    for c in range(1, C + 1):
        v = board[r][c]
        dp0[r][c-1] = max(dp0[r-1][c], dp1[r-1][c], dp2[r-1][c], dp3[r-1][c])
        dp3[r][c] = max(dp2[r][c-1] + v, dp3[r][c-1])
        dp2[r][c] = max(dp1[r][c-1] + v, dp2[r][c-1])
        dp1[r][c] = max(dp0[r][c-1] + v, dp1[r][c-1])

ans = max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])
print(ans)

Submission status: Wrong Answer

Solution code:
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

dp0 = [[0] * (C + 1) for _ in range(R + 1)]
dp1 = [[0] * (C + 1) for _ in range(R + 1)]
dp2 = [[0] * (C + 1) for _ in range(R + 1)]
dp3 = [[0] * (C + 1) for _ in range(R + 1)]

for r in range(R):
    for c in range(C):
        v = items[r][c]
        for fr, to in ((dp2, dp3), (dp1, dp2), (dp0, dp1)):
            fr[r][c + 1] = max(fr[r][c + 1], fr[r][c])
            to[r][c + 1] = max(to[r][c + 1], fr[r][c] + v)

        dp0[r + 1][c] = max(
            dp0[r + 1][c],
            dp0[r][c] + v,
            dp1[r][c] + v,
            dp2[r][c] + v,
            dp3[r][c]
        )

ans = max(
    dp0[R - 1][C], dp0[R][C - 1],
    dp1[R - 1][C], dp1[R][C - 1],
    dp2[R - 1][C], dp2[R][C - 1],
    dp3[R - 1][C], dp3[R][C - 1],
)

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

cost = [[0 for i in range(C)] for j in range(R)] 

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

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

for h in range(R):
    tmp_pick = []
    for w in range(C):
        if cost[h][w] != 0:
            heapq.heappush(tmp_pick,cost[h][w])
        if len(tmp_pick) <= 3:
            if w == 0:
                dp[h+1][w] = dp[h][w]+cost[h][w]
            else:
                dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w])
        else:
            min_value = heapq.heappop(tmp_pick)
            dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w]-min_value)
            
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
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

dp0 = [[0] * (C + 1) for _ in range(R + 1)]
dp1 = [[0] * (C + 1) for _ in range(R + 1)]
dp2 = [[0] * (C + 1) for _ in range(R + 1)]
dp3 = [[0] * (C + 1) for _ in range(R + 1)]

for r in range(R):
    for c in range(C):
        v = items[r][c]
        for fr, to in ((dp0, dp1), (dp1, dp2), (dp2, dp3)):
            dp0[r + 1][c] = max(dp0[r + 1][c], fr[r][c])
            fr[r][c + 1] = max(fr[r][c + 1], fr[r][c])
            dp0[r + 1][c] = max(dp0[r + 1][c], fr[r][c] + v)
            to[r][c + 1] = max(to[r][c + 1], fr[r][c] + v)

ans = max(
    dp0[R - 1][C], dp0[R][C - 1],
    dp1[R - 1][C], dp1[R][C - 1],
    dp2[R - 1][C], dp2[R][C - 1],
    dp3[R - 1][C], dp3[R][C - 1],
)

print(ans)

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)]
for r, c, v in X:
    items[r][c] = 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 i in range(1, R + 1):
    for j in range(1, C + 1):
        dp0[i][j] = max(dp1[i - 1][j], dp2[i - 1][j], dp3[i - 1][j])

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

        if items[i][j]:
            dp3[i][j] = max(dp3[i][j], dp2[i][j] + items[i][j])
            dp2[i][j] = max(dp2[i][j], dp1[i][j] + items[i][j])
            dp1[i][j] = max(dp1[i][j], dp0[i][j] + items[i][j])

print(max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1]))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.buffer.readline

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

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


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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 20:45:06 2020

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

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

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


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

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

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

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

R, C, K = getNM()
point = [[0] * C for i in range(R)]
for i in range(K):
    r, c, v = getNM()
    point[r - 1][c - 1] = v

# dp[i][j][k]: i行目j列まで行った時にk個まで取った時の最大値
dp = [[[0] * 4 for i in range(C)] for i in range(R)]
dp[0][0][1] = point[0][0]

for i in range(R):
    for j in range(C):
        # [i][j]にある宝を取得しない場合
        # 左のものから引き継ぐ
        if j - 1 >= 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0])
            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][2])
            dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3])
        # 上のものから引き継ぐ
        if i - 1 >= 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3])

        # 取得する場合
        p = point[i][j]
        if j - 1 >= 0:
            dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + p)
            dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + p)
            dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + p)
        if i - 1 >= 0:
            dp[i][j][1] = max(dp[i][j][0], dp[i - 1][j][0] + p, dp[i - 1][j][1] + p, dp[i - 1][j][2] + p, dp[i - 1][j][3] + p)

print(max(dp[-1][-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(K):
  r, c, v = map(int, input().split())
  goods[r][c] = v

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):

    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(2) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        used = [0, 0, 0]
        r_old = r_new
        r_new = r&1
        #横, 3まで
        c = 1
        while(c < C+1):
            if(items_matrix[r][c]==0):
                if(dp[r_old][c] <dp[r_new][c-1]):
                    dp[r_new][c] = dp[r_new][c-1]
                else:
                    dp[r_new][c] = dp[r_old][c]
                    used = [0, 0, 0]
                    #heapq.heapify(used)
                    while(c < C+1 and items_matrix[r][c]==0):
                        dp[r_new][c] = dp[r_old][c]
                        c += 1
                    c -= 1


            else:
                if(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                    minv = heapq.heappop(used)
                    dp[r_new][c] = dp[r_new][c-1]-minv + items_matrix[r][c]
                    heapq.heappush(used, items_matrix[r][c])
                elif(dp[r_new][c-1] > dp[r_old][c] + items_matrix[r][c]):
                    dp[r_new][c] = dp[r_new][c-1]
                else:
                    dp[r_new][c] = dp[r_old][c] + items_matrix[r][c]
                    used = [0, 0, items_matrix[r][c]]
                    #heapq.heapify(used)
            c += 1



    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):

    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(2) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        used = [0, 0, 0]
        r_old = r_new
        r_new = r&1
        #横, 3まで
        c = 1
        while(c < C+1):
            if(items_matrix[r][c]==0):
                if(dp[r_old][c] <dp[r_new][c-1]):
                    dp[r_new][c] = dp[r_new][c-1]
                else:
                    dp[r_new][c] = dp[r_old][c]
                    used = [0, 0, 0]
                    #heapq.heapify(used)
                    while(c < C+1 and items_matrix[r][c]==0):
                        dp[r_new][c] = dp[r_old][c]
                        c += 1
                    c -= 1


            else:
                if(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                    minv = heapq.heappop(used)
                    dp[r_new][c] = dp[r_new][c-1]-minv + items_matrix[r][c]
                    heapq.heappush(used, items_matrix[r][c])
                else:
                    dp[r_new][c] = dp[r_old][c] + items_matrix[r][c]
                    used = [0, 0, items_matrix[r][c]]
                    #heapq.heapify(used)
            c += 1



    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):

    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(R+1) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    for r in range(1, R+1): #縦
        used = [0, 0, 0]

         #横, 3まで
        c = 1
        while(c < C+1):
            if(items_matrix[r][c]==0):
                if(dp[r-1][c] <=dp[r][c-1]):
                    dp[r][c] = dp[r][c-1]
                else:
                    dp[r][c] = dp[r-1][c]
                    used = [0, 0, 0]
                    #heapq.heapify(used)
                    while(c < C+1 and dp[r-1][c] >dp[r][c-1] and items_matrix[r][c]==0):
                        dp[r][c] = dp[r-1][c]
                        c += 1
                    c -= 1


            else:
                if(dp[r][c-1]-used[0] > dp[r-1][c]):
                    minv = heapq.heappop(used)
                    dp[r][c] = dp[r][c-1]-minv + items_matrix[r][c]
                    heapq.heappush(used, items_matrix[r][c])
                else:
                    dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                    used = [0, 0, items_matrix[r][c]]
                    #heapq.heapify(used)
            c += 1



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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )

    edges = [ [] for _ in range(N+2) ]
    for i in range(N):
        edges[0].append([0, i]) #[cost, edge]
    for i in range(1, N+1):
        edges[i].append([0, N+1])
    for i in range(N):
        edges[i].append([ CList[i]*(-1), PList[i] ])
    """
    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(R+1) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    for r in range(1, R+1): #縦
        used = []
        for c in range(1, C+1): #横, 3まで
            if(items_matrix[r][c]==0):
                if(dp[r][c-1] <= dp[r-1][c] ):
                    dp[r][c] = dp[r-1][c]
                    used = []
                else:
                    dp[r][c] = dp[r][c-1]

            else:
                if( len(used)<3 ):
                    if(dp[r][c-1] <= dp[r-1][c] ):
                        dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                        used = [ ]
                        heapq.heappush(used, items_matrix[r][c])
                    else:
                        dp[r][c] = dp[r][c-1] + items_matrix[r][c]
                        heapq.heappush(used, items_matrix[r][c])
                else:
                    min_value= used[0]
                    if(min_value <items_matrix[r][c] ):
                        min2 = dp[r][c-1]-min_value
                        if(min2 > dp[r-1][c]):
                            dp[r][c] = min2 + items_matrix[r][c]
                            heapq.heappop(used)
                            heapq.heappush(used, items_matrix[r][c])

                        else:
                            dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                            used = [ ]
                            heapq.heappush(used, items_matrix[r][c])
                    else:
                        if(dp[r][c-1] > dp[r-1][c]+items_matrix[r][c]):
                            dp[r][c] = dp[r][c-1]
                        else:
                            dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                            used = [ ]
                            heapq.heappush(used, items_matrix[r][c])

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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )

    edges = [ [] for _ in range(N+2) ]
    for i in range(N):
        edges[0].append([0, i]) #[cost, edge]
    for i in range(1, N+1):
        edges[i].append([0, N+1])
    for i in range(N):
        edges[i].append([ CList[i]*(-1), PList[i] ])
    """
    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(R+1) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    for r in range(1, R+1): #縦
        used = []
        for c in range(1, C+1): #横, 3まで
            if(items_matrix[r][c]==0):
                if(dp[r][c-1] > dp[r-1][c] ):
                    dp[r][c] = dp[r][c-1]
                else:
                    dp[r][c] = dp[r-1][c]
                    used = []
            else:
                if( len(used)<3 ):
                    if(dp[r][c-1] <= dp[r-1][c] ):
                        dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                        used = [ ]
                        heapq.heappush(used, items_matrix[r][c])
                    else:
                        dp[r][c] = dp[r][c-1] + items_matrix[r][c]
                        heapq.heappush(used, items_matrix[r][c])
                else:
                    min_value= used[0]
                    if(min_value <items_matrix[r][c] ):
                        if(dp[r][c-1]-min_value > dp[r-1][c]):
                            dp[r][c] = dp[r][c-1] + items_matrix[r][c]-min_value
                        else:
                            dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                            used = [ ]
                            heapq.heappush(used, items_matrix[r][c])
                    else:
                        if(dp[r][c-1] > dp[r-1][c]+items_matrix[r][c]):
                            dp[r][c] = dp[r][c-1]
                        else:
                            dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                            used = [ ]
                            heapq.heappush(used, items_matrix[r][c])
                    
    print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys, math, heapq





if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )

    edges = [ [] for _ in range(N+2) ]
    for i in range(N):
        edges[0].append([0, i]) #[cost, edge]
    for i in range(1, N+1):
        edges[i].append([0, N+1])
    for i in range(N):
        edges[i].append([ CList[i]*(-1), PList[i] ])
    """
    R, C, K= map(int, input().strip().split() )
    dp = [  [0] * (C+1)  for _ in range(R+1) ] #[value]
    items_matrix = [ [0] * (C+1)  for _ in range(R+1) ]

    for i in range(K):
        item = list( map(int, input().strip().split() ) ) #[r, c, value] #y, x
        items_matrix[ item[0] ][ item[1] ] = item[2]
    # print(items_matrix)
    for r in range(1, R+1): #縦
        used = []
        for c in range(1, C+1): #横, 3まで
            if(items_matrix[r][c]==0):
                if(dp[r][c-1] > dp[r-1][c] ):
                    dp[r][c] = dp[r][c-1]
                else:
                    dp[r][c] = dp[r-1][c]
                    used = []
            else:
                if(dp[r][c-1] <= dp[r-1][c] ):
                    dp[r][c] = dp[r-1][c] + items_matrix[r][c]
                    used = [ ]
                    heapq.heappush(used, items_matrix[r][c])
                elif( len(used)==3 ):
                    min_value= used[0]
                    if(min_value <items_matrix[r][c] ):
                        dp[r][c] = dp[r][c-1] + items_matrix[r][c]-min_value
                        heapq.heappop(used)
                        heapq.heappush(used, items_matrix[r][c])

                else:
                    dp[r][c] = dp[r][c-1] + items_matrix[r][c]
                    heapq.heappush(used, items_matrix[r][c])
    print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
r,c,k=[int(i) for i in raw_input().split()]
a=[[-1 for i in range(c)]for j in range(r)]
for i in range(k):
    x,y,z=[int(j) for j in raw_input().split()]
    a[x-1][y-1]=z
dp=[[[0,0,0] for i in range(c+1)]for j in range(r+1)]
for i in range(r+1):
    for j in range(c+1):
        if(i>0):
            dp[i][j][0]=dp[i-1][j][2]
            dp[i][j][1]=dp[i-1][j][2]
            dp[i][j][2]=dp[i-1][j][2]
        if j>0:
            dp[i][j][2]=max(dp[i][j][2],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][1],dp[i][j-1][1])
            dp[i][j][0]=max(dp[i][j][0],dp[i][j-1][0])
        if(i>0 and j>0 and a[i-1][j-1]!=-1):
            dp[i][j][2]=max(dp[i][j][2]+a[i-1][j-1],dp[i][j-1][2])
            dp[i][j][1]=max(dp[i][j][1]+a[i-1][j-1],dp[i][j-1][1])
            dp[i][j][0]=max(dp[i][j][0]+a[i-1][j-1],dp[i][j-1][0])
        
print max(dp[r][c])

Submission status: Wrong Answer

Solution code:
# 入力
R, C, K = map(int, input().split())
point_map = [[0]*(C+1) for _ in range(R+1)]
max_point = [[[0]*4 for _ in range(C + 1)]
             for _ in range(R + 1)]
maxmax_point = [[0]*(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
        mxi = max_point[i]
        mxij = mxi[j]
        mxijm = mxi[jm]
        point = point_map[i][j]
        mxij[0] = max(
            mxijm[0], maxmax_point[im][j])
        if point == 0:
            mxij[1] = mxijm[1]
            mxij[2] = mxijm[2]
            mxij[3] = mxijm[3]
        else:
            itemss = [[]] + [items + [point]
                             for items in itemss]  # items[1] アイテム一つ
            itemss_len = len(itemss)
            #print(i, j, itemss)

            mxij[1] = max(
                mxijm[0] + point, maxmax_point[i - 1][j]+point)
            mxij[2] = mxijm[1] + point

            if itemss_len > 4:
                items = itemss[4]  # 4 こアイテムがあるやつ
                del_item = min(items)
                items.remove(del_item)
                sa = mxijm[3] - del_item
                #print("aaaaa", sa, mxijm[2])
                if sa > mxijm[2]:
                    mxij[3] = sa + point
                    itemss = itemss[:3] + [items]
                else:
                    mxij[3] = mxijm[2] + point
                    itemss = itemss[:4]
            else:
                mxij[3] = mxijm[2] + point

            #print(i, j, itemss)

        # [print(i, j, n, mxij[n])
        # for n in range(len(mxij))]
        maxmax_point[i][j] = max(mxij)
        #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())
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
        mxi = max_point[i]
        mxij = mxi[j]
        mxijm = mxi[jm]
        point = point_map[i][j]
        mxij[0] = max(
            mxijm[0], maxmax_point[im][j])
        if point == 0:
            mxij[1] = mxijm[1]
            mxij[2] = mxijm[2]
            mxij[3] = mxijm[3]
        else:
            itemss = [[]] + [items + [point]
                             for items in itemss]  # items[1] アイテム一つ
            itemss_len = len(itemss)
            #print(i, j, itemss)

            mxij[1] = max(
                mxijm[0] + point, maxmax_point[i - 1][j]+point)
            mxij[2] = mxijm[1] + point

            if itemss_len > 4:
                items = itemss[4]  # 4 こアイテムがあるやつ
                del_item = min(items)
                items.remove(del_item)
                sa = mxijm[3] - del_item
                #print("aaaaa", sa, mxijm[2])
                if sa > mxijm[2]:
                    mxij[3] = sa + point
                    itemss = itemss[:3] + [items]
                else:
                    mxij[3] = mxijm[2] + point
                    itemss = itemss[:4]
            else:
                mxij[3] = mxijm[2] + point

            #print(i, j, itemss)

        #[print(i, j, n, mxij[n])
        # for n in range(len(mxij))]
        maxmax_point[i][j] = max(mxij)
        #print(i, j, maxmax_point[i][j])

print(maxmax_point[R][C])

Submission status: Wrong Answer

Solution code:
f,h=range,input
R,C,K=map(int,h().split())
l=lambda:[-~C*[0]for i in f(R+1)]
G=l()
for i in'_'*K:r,c,v=map(int,h().split());G[r][c]=v
F=[l()for i in f(4)]
for r in f(1,R+1):
	for x in f(1,4):F[x][r][1:]=[max(F[x-1][r][c],max(F[x-1][r][c-1],(x<2)*F[3][r-1][c])+G[r][c],F[x][r][c-1])for c in f(1,C+1)]
print(F[3][R][C])