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
input=sys.stdin.readline

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W*self.__D+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

def main():
    r,c,k=map(int,input().split())
    v = list2D(r, c, 0)

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

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            """
            if i>0:
                x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3]) + val
                dp[i, j, 1]=max(dp[i, j, 1], x)
                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])

            if j>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, 0]+val,dp[i, j-1, 1])
                dp[i, j, 2]=max(dp[i, j, 2],dp[i, j-1, 1]+val,dp[i, j-1, 2])
                dp[i, j, 3]=max(dp[i, j, 3],dp[i, j-1, 2]+val,dp[i, j-1, 3])
            """
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

def main():
    r,c,k=map(int,input().split())
    v=[0] * (c * r)
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[(ri-1)*c + (ci-1)]=a

    dp= [0] * (c * r * 4)

    #print(dp)
    x = c * r
    if v[0]>0: dp[x] = v[0]

    for i in range(r-1):
        for j in range(c-1):
            idx = (i+1)*c+(j+1)
            idx2 = i*c + (j+1)
            idx3 = (i+1)*c + j

            a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2])+v[idx]
            dp[x+idx]=max(dp[x+idx], a)
            dp[idx]=max(dp[idx],dp[idx2],dp[x+idx2],dp[2*x+idx2],dp[3*x+idx2])

            dp[idx]=max(dp[idx],dp[idx3])
            dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx],dp[x + idx3])
            dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx],dp[2*x + idx3])
            dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx],dp[3*x + idx3])

    #print(dp)
    ans=0
    for i in range(4): ans = max(dp[i*x + (r-1)*c + (c-1)],ans)
    return print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(3):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = dp[0]
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]

for r in range(R):
   dp[1][r][0] = location[r][0]

if C > 1:
   for c in range(C-1):
         dp[1][0][c+1] = max(location[0][c+1] + dp[0][0][c], dp[1][0][c])
         dp[2][0][c+1] = max(location[0][c+1] + dp[1][0][c], dp[2][0][c])
   if R > 1:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1], dp[2][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c+1] + location[r+1][c+1], dp[1][r+1][c])
            dp[2][r+1][c+1] = max(dp[1][r+1][c] + location[r+1][c+1], dp[2][r+1][c])
ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1])
print(ans)

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
L = 4
items = [[0 for c in range(C+1)] for r in range(R+1)]
for k in range(K):
    r, c, v = map(int, input().split())
    items[r][c] = v
dp = [[[0 for limit in range(L)] for c in range(C+1)] for r in range(R+1)]
for r in range(1, R+1):
    for c in range(1, C+1):
        upper = max(dp[r-1][c])
        left = dp[r][c-1]
        item = items[r][c]
        if not item:
            dp[r][c] = left
            dp[r][c][0] = max(upper, left[0])
        else:
            dp[r][c][0] = max(upper, left[0])
            dp[r][c][1] = max(upper+item, left[1])
            dp[r][c][2] = max(left[1] + item, left[2])
            dp[r][c][3] = max(left[2] + item, left[3])
print(max(dp[R][C]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
L = 4
items = [[0 for c in range(C+1)] for r in range(R+1)]
for k in range(K):
    r, c, v = map(int, input().split())
    items[r][c] = v
dp = [[[0 for limit in range(L)] for c in range(C+1)] for r in range(R+1)]
for r in range(1, R+1):
    for c in range(1, C+1):
        upper = max(dp[r-1][c])
        left = dp[r][c-1]
        item = items[r][c]
        if not item:
            dp[r][c] = left
            dp[r][c][0] = max(upper, left[0])
        else:
            dp[r][c][1] = max(upper+item, left[1])
            dp[r][c][2] = max(left[1] + item, left[2])
            dp[r][c][3] = max(left[2] + item, left[3])
print(max(dp[R][C]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
L = 4
items = [[0 for c in range(C+1)] for r in range(R+1)]
for k in range(K):
    r, c, v = map(int, input().split())
    items[r][c] = v
dp = [[[0 for limit in range(L)] for c in range(C+1)] for r in range(R+1)]
for r in range(1, R+1):
    for c in range(1, C+1):
        upper = max(dp[r-1][c])
        left = dp[r][c-1]
        item = items[r][c]
        if not item:
            dp[r][c] = left
        else:
            dp[r][c][0] = max(upper, left[0])
            dp[r][c][1] = max(upper+item, left[1])
            dp[r][c][2] = max(left[1] + item, left[2])
            dp[r][c][3] = max(left[2] + item, left[3])
print(max(dp[R][C]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(3):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = dp[0]
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]
for c in range(C):
   dp[1][0][c] = location[0][c]
if R == 1 :
   ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1])
else: 
   for r in range(R-1):
         dp[1][r+1][0] = max(location[r+1][0], dp[1][r][0])
         dp[2][r+1][0] = max(location[r+1][0] + dp[1][r][0], dp[2][r][0])
   if C == 1:
      ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1])
   else:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[2][r][c+1], dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c]+location[r+1][c+1], dp[1][r+1][c], dp[1][r][c+1]+location[r+1][c+1], dp[0][r][c+1]+location[r+1][c+1])
            dp[2][r+1][c+1] = max(dp[1][r+1][c]+location[r+1][c+1], dp[2][r+1][c])
      ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1])
print(ans)

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] * c
for i in range(r):
    rs = []
    for j in range(c):
        if j == 0:
            dp[j] += v[i][j]
            if v[i][j] > 0:
                rs.append(v[i][j])
        else:
            if v[i][j] > 0:
                rs.append(v[i][j])
            tmp = dp[j - 1] + v[i][j]
            if len(rs) == 4:
                m = min(rs)
                tmp -= m
                rs.remove(m)
            if tmp > dp[j] + v[i][j]:
                dp[j] = tmp
            else:
                dp[j] += v[i][j]
                if v[i][j] > 0:
                    rs = [v[i][j]]
                else:
                    rs = []

print(dp[c - 1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


r,c,k = readints()
a = [[0]*(c+1) for i in range(r+1)]

for i in range(k):
    R,C,V = readints()
    a[R][C] = V

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

for x in range((r+1)*(c+1)*4):
    i = x//((c+1)*4)
    l = x%((c+1)*4)
    j = l//4
    l %= 4
    if i==0:
        continue
    if j==0:
        continue
    if l==0:
        dp[x] = max(dp[i*(c+1)*4 + (j-1)*4], dp[(i-1)*(c+1)*4 + j*4 + 3])
    else:
        dp[x] = max(dp[i*(c+1)*4 + (j-1)*4 + 3], dp[(i-1)*(c+1)*4 + j*4 + 3]+a[i][j], dp[i*(c+1)*4 + (j-1)*4 + l-1]+a[i][j])

print(dp[-1])

Submission status: Wrong Answer

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

dp = [[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r in range(R):
    for c in range(C):
        for i in range(1, 4):
            if c + r == 0:
                dp[r][c][i] = dp[r][c][i - 1] + v_list[r][c]
                continue
            if c > 0:
                dp[r][c][i] = max(dp[r][c][i], dp[r][c - 1][i - 1] + v_list[r][c])
            if r > 0:
                dp[r][c][i] = max(dp[r][c][i], dp[r - 1][c][i - 1] + v_list[r][c])
print(max(dp[-1][-1]))

Submission status: Wrong Answer

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

dp = [[0 for _ in range(C)] for _ in range(R)]
for r in range(R):
    for c in range(C):
        dp[r][c] = max(max(dp[r][c], dp[r][c - 1] + v_list[r][c]), dp[r - 1][c] + v_list[r][c])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main(R,C,K,rcv):
  inf=float('inf')
  dp0=[[-inf,-inf,-inf,0] for _ in range(C)]
  # dp0[i][j]:i列目の時点で残りちょうどj個取れる時の最大値
  rcv.sort(key=lambda x:R*x[0]+x[1])
  i=0
  nowc=0
  for j in range(R):
    # j+1行目に対する操作
    # 初期化
    for c in range(C):
      dp0[c][3]=max(dp0[c])
      dp0[c][2]=-inf
      dp0[c][1]=-inf
      dp0[c][0]=-inf
    nowc=0
    while i<K and rcv[i][0]==j+1:
      nextc=rcv[i][1]-1
      for k in range(nowc+1,nextc+1):
        dp0[k][0]=max(dp0[k][0],dp0[nowc][0])
        dp0[k][1]=dp0[nowc][1]
        dp0[k][2]=dp0[nowc][2]
      v=rcv[i][2]
      dp0[nextc][0]=max(dp0[nextc][0],dp0[nextc][1]+v)
      dp0[nextc][1]=max(dp0[nextc][1],dp0[nextc][2]+v)
      dp0[nextc][2]=max(dp0[nextc][2],dp0[nextc][3]+v)
      i+=1
      nowc=nextc
    for k in range(nowc+1,C):
      dp0[k][0]=dp0[nowc][0]
      dp0[k][1]=dp0[nowc][1]
      dp0[k][2]=dp0[nowc][2]
    #print(dp0)
  print(max(dp0[-1]))
if __name__=='__main__':
  R,C,K=map(int,input().split())
  rcv=[list(map(int,input().split())) for _ in range(K)]
  main(R,C,K,rcv)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main(R,C,K,rcv):
  inf=float('inf')
  dp0=[[-inf,-inf,-inf,0] for _ in range(C)]
  # dp0[i][j]:i列目の時点で残りちょうどj個取れる時の最大値
  rcv.sort(key=lambda x:R*x[0]+x[1])
  i=0
  nowc=0
  for j in range(R):
    # j+1行目に対する操作
    # 初期化
    for c in range(C):
      dp0[c][3]=max(dp0[c])
      dp0[c][2]=-inf
      dp0[c][1]=-inf
      dp0[c][0]=-inf
    nowc=0
    while i<K and rcv[i][0]==j+1:
      nextc=rcv[i][1]-1
      for k in range(nowc+1,nextc+1):
        dp0[k][0]=dp0[nowc][0]
        dp0[k][1]=dp0[nowc][1]
        dp0[k][2]=dp0[nowc][2]
      v=rcv[i][2]
      dp0[nextc][0]=max(dp0[nextc][0],dp0[nextc][1]+v)
      dp0[nextc][1]=max(dp0[nextc][1],dp0[nextc][2]+v)
      dp0[nextc][2]=max(dp0[nextc][2],dp0[nextc][3]+v)
      i+=1
      nowc=nextc
    for k in range(nowc+1,C):
      dp0[k][0]=dp0[nowc][0]
      dp0[k][1]=dp0[nowc][1]
      dp0[k][2]=dp0[nowc][2]
    #print(dp0)
  print(max(dp0[-1]))
if __name__=='__main__':
  R,C,K=map(int,input().split())
  rcv=[list(map(int,input().split())) for _ in range(K)]
  main(R,C,K,rcv)

Submission status: Wrong Answer

Solution code:
def solve():
    R,C,K = map(int, input().split())
    value = [[0 for _ in range(C)] for _ in range(R)]
    dp = [[[0,0,0,0] for _ in range(C+1)] for _ in range (R+1)]
    for _ in range(K):
        r,c,v = map(int, input().split())
        value[r-1][c-1] = v

    #print(value)
    #print(dp)

    for r in range(1, R+1):
        for c in range(1, C+1):
            v = value[r-1][c-1]
            if v == 0: # no item
                dp[r][c][0] = max(max(dp[r-1][c]), dp[r-1][c][0])
                dp[r][c][1] = dp[r][c-1][1]
                dp[r][c][2] = dp[r][c-1][2]
                dp[r][c][2] = dp[r][c-1][3]
            else:
                dp[r][c][0] = max(max(dp[r - 1][c]), dp[r - 1][c][0])
                dp[r][c][1] = max(dp[r][c-1][1], dp[r][c-1][0] + v, max(dp[r - 1][c]) + v)
                dp[r][c][2] = max(dp[r][c-1][2], dp[r][c-1][1] + v)
                dp[r][c][3] = max(dp[r][c-1][3], dp[r][c-1][2] + v)

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

solve()

Submission status: Wrong Answer

Solution code:
R,C,K = map(int,input().split())
a = [[0]*3005]*3005
dp = [[[0]*4]*3005]*3005

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

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

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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, copy
import numpy as np





if(__name__ == "__main__"):

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

    Flags_item = [False]*(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): #縦
        r_new = r
        r_old = r-1

        used_1 = [0]
        used_2 = [0, 0]
        used_3 = [0, 0, 0]

        values = [0, 0, 0]
        for c in range(1, C+1):

            if(items_matrix[r][c]==0):
                #used_1, 2, 3は変化なし
                dp[r_new][c] = max(dp[r_new][c-1], dp[r_old][c])
            else:
                #used_3, 左から移動
                if(values[1] + items_matrix[r][c] > values[2] + max(0, items_matrix[r][c] - used_3[0] ) ):
                    #used_2+items_matrix[r][c]がベスト
                    values[2] = values[1] + items_matrix[r][c]

                    used_3 = copy.copy(used_2)
                    heapq.heappush(used_3, items_matrix[r][c])

                elif(items_matrix[r][c] > used_3[0]):
                    #used_3で良い。交換するかを考える
                    #交換
                    values[2] += items_matrix[r][c] - used_3[0]
                    heapq.heappop(used_3)
                    heapq.heappush(used_3, items_matrix[r][c])


                #used_2, 左から
                values[1] = values[0] + items_matrix[r][c]
                used_2 = [used_1[0]]
                heapq.heappush(used_2, items_matrix[r][c])

                #used_1, 上から移動
                used_1[0] = items_matrix[r][c]
                values[0] = dp[r_old][c] + items_matrix[r][c]

                #dp
                dp[r_new][c] = max(values)    
            #print(r, dp[r_new], values)
            #print("  ", used_1, used_2, used_3)


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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
R,C,K = map(int,input().split())
lis = [[0] * C for i in range(R)]
for _ in range(K):
    r,c,v = map(int,input().split())
    lis[r-1][c-1] = v
dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for j in range(C):
        dp[i][j][1] = max(dp[i][j][1],max(dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])+lis[i][j])
        dp[i][j][0] = max(dp[i][j][0],max(dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3]))
        for k in range(1,4):
            dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
            dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+lis[i][j])


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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, copy
import numpy as np





if(__name__ == "__main__"):

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

    Flags_item = [False]*(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]
        Flags_item[ item[0] ] = True

    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        r_new = r
        r_old = r-1
        if(Flags_item[r_new]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            used_1 = [0]
            used_2 = [0, 0]
            used_3 = [0, 0, 0]

            values = [0, 0, 0]
            for c in range(1, C+1):

                if(items_matrix[r][c]==0):
                    #used_1, 2, 3は変化なし
                    dp[r_new][c] = max(dp[r_new][c-1], dp[r_old][c])
                else:
                    #used_3, 左から移動
                    if(values[1] + items_matrix[r][c] > values[2] + max(0, items_matrix[r][c] - used_3[0] ) ):
                        #used_2+items_matrix[r][c]がベスト
                        values[2] = values[1] + items_matrix[r][c]

                        used_3 = copy.copy(used_2)
                        heapq.heappush(used_3, items_matrix[r][c])

                    else:
                        #used_3で良い。交換するかを考える
                        if(items_matrix[r][c] > used_3[0]):
                            #交換

                            values[2] += items_matrix[r][c] - used_3[0]
                            heapq.heappop(used_3)
                            heapq.heappush(used_3, items_matrix[r][c])

                    #used_2, 左から
                    values[1] = values[0] + items_matrix[r][c]
                    used_2[0] = used_1[0]
                    used_2[1] = items_matrix[r][c]

                    #used_1, 上から移動
                    used_1[0] = items_matrix[r][c]
                    values[0] = dp[r_old][c] + items_matrix[r][c]

                    #dp
                    dp[r_new][c] = max(values)
            #print(r, dp[r_new], values)
            #print("  ", used_1, used_2, used_3)


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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, copy
import numpy as np





if(__name__ == "__main__"):

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

    Flags_item = [False]*(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]
        Flags_item[ item[0] ] = True

    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        r_old = r_new
        r_new = r&1

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            used_1 = [0]
            used_2 = [0, 0]
            used_3 = [0, 0, 0]

            values = [0, 0, 0]
            for c in range(1, C+1):

                if(items_matrix[r][c]==0):
                    #used_1, 2, 3は変化なし
                    dp[r_new][c] = max(dp[r_new][c-1], dp[r_old][c])
                else:
                    #used_3, 左から移動
                    if(values[1] + items_matrix[r][c] > values[2] + max(0, items_matrix[r][c] - used_3[0] ) ):
                        #used_2+items_matrix[r][c]がベスト
                        values[2] = values[1] + items_matrix[r][c]

                        used_3 = copy.copy(used_2)
                        heapq.heappush(used_3, items_matrix[r][c])

                    else:
                        #used_3で良い。交換するかを考える
                        if(items_matrix[r][c] > used_3[0]):
                            #交換

                            values[2] += items_matrix[r][c] - used_3[0]
                            heapq.heappop(used_3)
                            heapq.heappush(used_3, items_matrix[r][c])

                    #used_2, 左から
                    values[1] = values[0] + items_matrix[r][c]
                    used_2[0] = used_1[0]
                    used_2[1] = items_matrix[r][c]

                    #used_1, 上から移動
                    used_1[0] = items_matrix[r][c]
                    values[0] = dp[r_old][c] + items_matrix[r][c]

                    #dp
                    dp[r_new][c] = max(values)
            #print(r, dp[r_new], values)
            #print("  ", used_1, used_2, used_3)


    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
R,C,K = map(int,input().split())
lis = [[0] * C for i in range(R)]
for _ in range(K):
    r,c,v = map(int,input().split())
    lis[r-1][c-1] = v
dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for j in range(C):
        dp[i][j][1] = max(dp[i][j][1],max(dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])+lis[i][j])
        dp[i][j][0] = max(dp[i][j][0],max(dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3]))
        for k in range(1,4):
            dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
            dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+lis[i][j])


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

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, copy
import numpy as np





if(__name__ == "__main__"):

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

    Flags_item = [False]*(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]
        Flags_item[ item[0] ] = True

    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        r_old = r_new
        r_new = r&1

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            used_1 = np.zeros( 1, dtype=np.int64)
            used_2 = np.zeros( 2, dtype=np.int64)
            used_3 = np.zeros( 3, dtype=np.int64)
            #used_2 = [0, 0]
            #used_3 = [0, 0, 0]
            values = [0, 0, 0]
            for c in range(1, C+1):

                if(items_matrix[r][c]==0):
                    #used_1, 2, 3は変化なし
                    dp[r_new][c] = max(dp[r_new][c-1], dp[r_old][c])
                else:
                    #used_3, 左から移動
                    if(values[1] + items_matrix[r][c] > values[2] + max(0, items_matrix[r][c] - min(used_3) ) ):
                        #used_2+items_matrix[r][c]がベスト
                        values[2] = values[1] + items_matrix[r][c]
                        for j in range(2):
                            used_3[j] = used_2[j]
                        used_3[2] = items_matrix[r][c]

                    else:
                        #used_3で良い。交換するかを考える
                        min_v = min(used_3)
                        if(items_matrix[r][c] > min_v):
                            #交換
                            min_i = -1
                            for j in range(3):
                                if(used_3[j] == min_v):
                                    min_i = j
                                    break
                            values[2] += items_matrix[r][c] - min_v
                            used_3[min_i]= items_matrix[r][c]

                    #used_2, 左から
                    values[1] = values[0] + items_matrix[r][c]
                    used_2[0] = used_1[0]
                    used_2[1] = items_matrix[r][c]

                    #used_1, 上から移動
                    used_1[0] = items_matrix[r][c]
                    values[0] = dp[r_old][c] + items_matrix[r][c]

                    #dp
                    dp[r_new][c] = max(values)
            #print(r, dp[r_new], values)
            #print("  ", used_1, used_2, used_3)


    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
R,C,K = map(int,input().split())
lis = [[0] * C for i in range(R)]
for _ in range(K):
    r,c,v = map(int,input().split())
    lis[r-1][c-1] = v
dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for j in range(C):
        for k in range(1,4):
            dp[i][j][k] = max(dp[i-1][j][k],dp[i][j-1][k])
            dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+lis[i][j])
            dp[i][j][k] = max(dp[i][j][k],dp[i-1][j][3]+lis[i][j])
#print(dp)
print(max(dp[R-1][C-1][1],dp[R-1][C-1][2],dp[R-1][C-1][3]))

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, copy





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) ]

    Flags_item = [False]*(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]
        Flags_item[ item[0] ] = True

    # print(items_matrix)
    r_old = 0
    r_new = 0
    for r in range(1, R+1): #縦
        r_old = r_new
        r_new = r&1
        #横, 3まで

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            used_1 = [0]
            used_2 = [0, 0]
            used_3 = [0, 0, 0]
            values = [0, 0, 0]
            for c in range(1, C+1):

                if(items_matrix[r][c]==0):
                    #used_1, 2, 3は変化なし
                    dp[r_new][c] = max(dp[r_new][c-1], dp[r_old][c])
                else:
                    #used_3, 左から移動
                    if(values[1] + items_matrix[r][c] > values[2] + max(0, items_matrix[r][c] - used_3[0]) ):
                        #used_2+items_matrix[r][c]がベスト
                        values[2] = values[1] + items_matrix[r][c]
                        used_3 = copy.copy(used_2)
                        heapq.heappush(used_3, items_matrix[r][c])

                    else:
                        #used_3で良い。交換するかを考える
                        if(items_matrix[r][c] > used_3[0]):
                            #交換
                            values[2] += items_matrix[r][c] - used_3[0]
                            heapq.heappop(used_3)
                            heapq.heappush(used_3, items_matrix[r][c])

                    #used_2, 左から
                    values[1] = values[0] + items_matrix[r][c]
                    used_2 = copy.copy(used_1)
                    heapq.heappush(used_2, items_matrix[r][c])

                    #used_1, 上から移動
                    used_1 = [items_matrix[r][c]]
                    values[0] = dp[r_old][c] + items_matrix[r][c]

                    #dp
                    dp[r_new][c] = max(values)
            #print(r, dp[r_new], values)
            #print("  ", used_1, used_2, used_3)





    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
import heapq
r, c, k = map(int, input().split())
itm = [[0]*c for _ in range(r)]
for i in range(k):
  ri, ci, vi = map(int, input().split())
  itm[ri-1][ci-1] = vi
dp = [0]*c
for i in range(r):
  que = []
  heapq.heapify(que)
  nowv = itm[i][0]
  if nowv > 0:
    heapq.heappush(que, nowv)
    dp[0] += nowv
  for j in range(1, c):
    nowv = itm[i][j]
    prev = 0
    if nowv > 0:
      heapq.heappush(que, nowv)
      if len(que)>3:
        prev = heapq.heappop(que)
    sumv1 = dp[j] + nowv#上から
    sumv2 = dp[j-1] + nowv - prev#左から
    if sumv1 >= sumv2:
      dp[j] = sumv1
      que = []
      heapq.heapify(que)
      if nowv > 0:
        heapq.heappush(que, nowv)
    else:
      dp[j] = sumv2
print(dp[-1])