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

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
    items_matrix[1][1] = 0
    # 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まで

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            for c in range(1, C+1):
                if(items_matrix[r][c] < used[0]):
                    #左から交換しない
                    if(dp[r_old][c] + items_matrix[r][c] < dp[r_new][c-1]):
                        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] ]
                elif(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                    #dp[r_new][c-1]-used[0] + items_matrix[r][c] < dp[r_old][c] + items_matrix[r][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] ]
                """
                if(items_matrix[r][c]>0):
                    if(items_matrix[r][c] < used[0]):
                        #左から交換しない
                        if(dp[r_old][c] + items_matrix[r][c] < dp[r_new][c-1]):
                            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] ]
                    elif(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                        #dp[r_new][c-1]-used[0] + items_matrix[r][c] < dp[r_old][c] + items_matrix[r][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] ]

                else:

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



    print(dp[r_new][-1])

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): #縦
        used = [0, 0, 0]
        r_old = r_new
        r_new = r&1
        #横, 3まで

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            for c in range(C+1):
                if(items_matrix[r][c]>0):
                    if(items_matrix[r][c] < used[0]):
                        #左から交換しない
                        if(dp[r_old][c] + items_matrix[r][c] < dp[r_new][c-1]):
                            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] ]
                    elif(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                        #dp[r_new][c-1]-used[0] + items_matrix[r][c] < dp[r_old][c] + items_matrix[r][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] ]

                else:

                    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]
                    


    print(dp[r_new][-1])

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): #縦
        used = [0, 0, 0]
        r_old = r_new
        r_new = r&1
        #横, 3まで
        dp[r_new] = copy.copy(dp[r_old])
        if(Flags_item[r]==False):
            pass
        else:
            c = 1
            while(c < C+1):
                if(items_matrix[r][c]>0):
                    if(items_matrix[r][c] < used[0]):
                        #左から交換しない
                        if(dp[r_old][c] + items_matrix[r][c] < dp[r_new][c-1]):
                            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] ]
                    elif(dp[r_new][c-1]-used[0] > dp[r_old][c]):
                        #dp[r_new][c-1]-used[0] + items_matrix[r][c] < dp[r_old][c] + items_matrix[r][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]]
                    c += 1
                else:
                    while(c < C+1 and 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]
                        c += 1






    print(dp[r_new][-1])

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): #縦
        used = [0, 0, 0]
        r_old = r_new
        r_new = r&1
        #横, 3まで

        if(Flags_item[r]==False):
            dp[r_new] = copy.copy(dp[r_old])
        else:
            c = 1
            while(c < C+1):
                if(items_matrix[r][c]>0):
                    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]]
                    c += 1
                else:
                    while(c < C+1 and 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]
                        c += 1






    print(dp[r_new][-1])

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect

r,c,k=map(int,input().split())
v=[[0]*c for _ in range(r)]
for i in range(k):
    ri,ci,val=map(int,input().split())
    v[ri-1][ci-1]=val
d=[[0]*(c*r) for _ in range(4)]
d[1][0]=v[0][0]
for i in range(1,c):
    d[1][i]=max(d[1][i-1],d[0][i-1]+v[0][i])
    d[2][i]=max(d[2][i-1],d[1][i-1]+v[0][i])
    d[3][i]=max(d[3][i-1],d[2][i-1]+v[0][i])
for i in range(c,r*c):
    m=0
    for j in range(4):
        m=max(m,d[j][i-c])
    cx=i%c
    rx=i//c
    d[0][i]=max(0,m)
    d[1][i]=max(0,m+v[rx][cx],v[rx][cx]+d[0][i-1]) if cx!=0 else max(0,v[rx][cx]+m)
    d[2][i]=max(0,v[rx][cx]+d[1][i-1]) if cx!=0 else 0
    d[3][i]=max(0,v[rx][cx]+d[2][i-1]) if cx!=0 else 0
ans=0
for j in range(4):
  ans=max(ans,d[j][r*c-1])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# 文字列をinput()した場合、末尾に改行が入るので注意
def main(R,C,K,rcv):
  inf=float('inf')
  dp0=[[-inf,-inf,-inf,0] for _ in range(C)]
  dp1=[[-inf]*4 for _ in range(C)]
  dp0[0][3]=0
  rcv.sort(key=lambda x:R*x[0]+x[1])
  i=0
  nowc=0
  while i<K and rcv[i][0]==1:
    nextc=rcv[i][1]-1
    for j in range(nowc+1,nextc+1):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][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 j in range(nowc+1,C):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][2]=dp0[nowc][2]
  for j in range(1,R):
    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 j in range(nowc+1,C):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][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)]
  #import datetime
  #print(datetime.datetime.now())
  main(R,C,K,rcv)
  #print(datetime.datetime.now())

Submission status: Wrong Answer

Solution code:
def solve():
    from collections import defaultdict
    r, c, k = map(int, input().split())
    v = defaultdict(int)
    for i in range(k):
        rr, cc, vv = map(int, input().split())
        rr, cc = rr - 1, cc - 1
        v[(rr, cc)] = vv
    dp = [[0 for _ in range(c)] for _ in range(4)]
    for i in range(r):
        tmp = [[0 for _ in range(c)] for _ in range(4)]
        tmp[1][0] = v[(i, 0)]
        for j in range(1, c):
            dp_tmp = max(dp[x][j] for x in range(4))
            tmp[0][j] = max(dp_tmp, tmp[0][j - 1])
            tmp[1][j] = max(dp_tmp + v[(i, j)], tmp[0][j - 1] + v[(i, j)], tmp[1][j - 1])
            tmp[2][j] = max(tmp[1][j - 1] + v[(i, j)], tmp[2][j - 1])
            tmp[3][j] = max(tmp[2][j - 1] + v[(i, j)], tmp[3][j - 1])
        dp = tmp
    print(max(dp[x][c - 1] for x in range(4)))
solve()

Submission status: Wrong Answer

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

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

MAP=[[0 for _ in range(C)] for _ in range(R)]
DP=[[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

DP[0][0][1]=MAP[0][0]
for i in range(R):
    for j in range(C):
        for num,point in enumerate([[1,0],[0,1]]):
            x,y=point
            X,Y=i+x,j+y
            if 0<=X<R and 0<=Y<C:
                for k in range(3):
                    if num==0:
                        DP[X][Y][1]=max(DP[X][Y][1],DP[i][j][3]+MAP[X][Y])
                    else:
                        DP[X][Y][k+1]=max(DP[X][Y][k+1],DP[X][Y][k],DP[i][j][k+1],DP[i][j][k]+MAP[X][Y])
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)]

MAP=[[0 for _ in range(C)] for _ in range(R)]
DP=[[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

DP[0][0][1]=MAP[0][0]
for i in range(R):
    for j in range(C):
        for num,point in enumerate([[1,0],[0,1]]):
            x,y=point
            X,Y=i+x,j+y
            if 0<=X<R and 0<=Y<C:
                for k in range(3):
                    if num==0:
                        DP[X][Y][1]=max(DP[X][Y][1],DP[i][j][3]+MAP[X][Y])
                    else:
                        DP[X][Y][k+1]=max(DP[X][Y][k+1],DP[X][Y][k],DP[i][j][k+1],DP[i][j][k]+MAP[X][Y])
print(max(DP[-1][-1]))

Submission status: Wrong Answer

Solution code:
'''
dpt[r][c][taken]
への遷移は、
max(dpt[r-1][c]) -> dpt[r][c][0]
dpt[r][c-1][taken-1] -> dpt[r][c][taken] + field[r][c]
dpt[r][c-1][taken] -> dpt[r][c][taken]
'''

import numpy as np
from numba import njit
def main():
    R, C, K = map(int, input().split())
    field = np.full((R+1,C+1), 0)
    dpt = np.full((R+1,C+1,4), 0)
    for i in range(K):
        r, c, v = map(int, input().split())
        field[r][c] = v
    dp(R, C, field, dpt)
    print(max(dpt[-1][-1]))
    print(field)
    print(dpt)

def dp(R, C, field, dpt):
    for r in range(1, R+1):
        for c in range(1, C+1):
            for taken in range(4):
                max_v = 0
                if taken == 0:
                    max_v = max(dpt[r-1][c])
                    max_v = max(dpt[r][c-1][0], max_v)
                elif taken == 1:
                    max_v = max(dpt[r][c-1][taken-1] + field[r][c],
                                dpt[r][c-1][taken],
                                max(dpt[r-1][c]) + field[r][c])
                else:
                    max_v = max(dpt[r][c-1][taken-1] + field[r][c],
                                dpt[r][c-1][taken])
                dpt[r][c][taken] = max_v
    return True

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import stdin
    def input():
        return stdin.readline().strip()

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

    def compare(a, b):
        for k in range(4):
            if a[k] < b[k]:
                a[k] = b[k]
        return a

    # dp[j][n] = j列にいて、n個itemを拾った時のアイテムの価値の合計の最大値
    # 1行ずつdpを更新する
    # 0行目
    # 0列目
    value = v[0][0]
    if value == 0:
        dp = [[0] * 4]
    else:
        dp = [[0, value, value, value]]
    # j列目
    for j in range(1, c):
        value = v[0][j]
        if value == 0:
            dp.append(dp[j-1])
            continue
        a = dp[j-1].copy()
        b = [0, a[0]+value, a[1]+value, a[2]+value]
        dp.append(compare(a, b))

    # i行目
    for i in range(1, r):
        # 0列目
        pre = dp[0][3]
        value = v[i][0]
        if value == 0:
            new_dp = [[pre] * 4]
        else:
            new_dp = [[pre, pre+value, pre+value, pre+value]]
        # j列目
        for j in range(1, c):
            value = v[i][j]
            if value == 0:
                a = new_dp[j-1].copy()
                b = [dp[j][3]] * 4
                new_dp.append(compare(a, b))
            else:
                a = new_dp[j-1].copy()
                pre = dp[j][3]
                b = [pre, pre+value, pre+value, pre+value]
                a = compare(a, b)
                b = [a[0], a[0]+value, a[1]+value, a[2]+value]
                new_dp.append(compare(a, b))

        dp = new_dp

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

main()

Submission status: Wrong Answer

Solution code:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  kdp[0]=dp[0]
  kdp[1]=l[i][0]+dp[0]
  ndp[0]=dp[0]+kdp[1]
  for j in range(1,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])

Submission status: Wrong Answer

Solution code:
import math
from numba import jit
import numpy as np

@jit
def solve():
    
    r,c,k = map(int,input().split())

    value = np.zeros((r,c), dtype=np.int64)
    for i in range(k):
        r_t,c_t,v_t = map(int,input().split())
        value[r_t-1][c_t-1] = v_t

    dp = np.zeros((r,c,4), dtype=np.int64)
    dp[0][0][0] = 0

    for i in range(r):
        for j in range(c):
            for m in range(4):
                if i + 1 < r:
                    dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m])
                if j + 1 < c:
                    dp[i][j+1][m] = max(dp[i][j+1][m],dp[i][j][m])
                if value[i][j] != 0 and m < 3:
                    if i + 1 < r:
                        dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m]+value[i][j])
                    if j + 1 < c:
                        dp[i][j+1][m+1] = max(dp[i][j+1][m+1],dp[i][j][m]+value[i][j])

    for m in range(3):
        dp[r-1][c-1][m+1] = max(dp[r-1][c-1][m+1],dp[r-1][c-1][m]+value[r-1][c-1])
    return max(dp[r-1][c-1])

print(solve())

Submission status: Wrong Answer

Solution code:
import math
from numba import jit
import numpy as np

@jit
def solve():
    
    r,c,k = map(int,input().split())

    value = np.zeros((r,c), dtype=np.int64)
    for i in range(k):
        r_t,c_t,v_t = map(int,input().split())
        value[r_t-1][c_t-1] = v_t

    dp = np.zeros((r,c,4), dtype=np.int64)
    dp[0][0][0] = 0

    for i in range(r):
        for j in range(c):
            for m in range(4):
                if i + 1 < r:
                    dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m])
                if j + 1 < c:
                    dp[i][j+1][m] = max(dp[i][j+1][m],dp[i][j][m])
                if value[i][j] != 0 and m < 3:
                    if i + 1 < r:
                        dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m]+value[i][j])
                    if j + 1 < c:
                        dp[i][j+1][m+1] = max(dp[i][j+1][m+1],dp[i][j][m]+value[i][j])

    return max(dp[r-1][c-1])+value[r-1][c-1]

print(solve())

Submission status: Wrong Answer

Solution code:
import math
import numpy as np

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

    value = np.zeros((r,c), dtype=np.int64)
    for i in range(k):
        r_t,c_t,v_t = map(int,input().split())
        value[r_t-1][c_t-1] = v_t

    dp = np.zeros((r,c,4), dtype=np.int64)
    dp[0][0][0] = 0

    for i in range(r):
        for j in range(c):
            for m in range(4):
                if i + 1 < r:
                    dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m])
                if j + 1 < c:
                    dp[i][j+1][m] = max(dp[i][j+1][m],dp[i][j][m])
                if value[i][j] != 0 and m < 3:
                    if i + 1 < r:
                        dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m]+value[i][j])
                    if j + 1 < c:
                        dp[i][j+1][m+1] = max(dp[i][j+1][m+1],dp[i][j][m]+value[i][j])

    return max(dp[r-1][c-1])+value[r-1][c-1]

print(solve())

Submission status: Wrong Answer

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

value = [[0]*c for _ in range(r)]
for i in range(k):
    r_t,c_t,v_t = map(int,input().split())
    value[r_t-1][c_t-1] = v_t

dp = [[[-math.inf]*4 for _ in range(c)] for _ in range(r)]
dp[0][0][0] = 0

for i in range(r):
    for j in range(c):
        for m in range(4):
            if i + 1 < r:
                dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m])
            if j + 1 < c:
                dp[i][j+1][m] = max(dp[i][j+1][m],dp[i][j][m])
            if value[i][j] != 0:
                if i + 1 < r:
                    dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m]+value[i][j])
                if j + 1 < c and m < 3:
                    dp[i][j+1][m+1] = max(dp[i][j+1][m+1],dp[i][j][m]+value[i][j])

print(max(dp[r-1][c-1])+value[r-1][c-1])

Submission status: Wrong Answer

Solution code:
import math
r,c,k = map(int,input().split())
rcv_list = []
for i in range(k):
    r_t,c_t,v_t = map(int,input().split())
    rcv_list.append([r_t-1,c_t-1,v_t])

dp = [[[-math.inf]*4 for _ in range(c)] for _ in range(r)]
dp[0][0][0] = 0

value = [[0]*c for _ in range(r)]

for i in range(k):
    r_t,c_t,v_t = rcv_list[i]
    value[r_t][c_t] = v_t

for i in range(r):
    for j in range(c):
        for m in range(4):
            if i + 1 < r:
                dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m])
            if j + 1 < c:
                dp[i][j+1][m] = max(dp[i][j+1][m],dp[i][j][m])
            if value[i][j] != 0 and m < 3:
                if i + 1 < r:
                    dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][m]+value[i][j])
                if j + 1 < c:
                    dp[i][j+1][m+1] = max(dp[i][j+1][m+1],dp[i][j][m]+value[i][j])

print(max(dp[r-1][c-1])+value[r-1][c-1])

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split(' '))
dp = [[[0, -1,-1,-1] for i in range(C)] for j in range(R)]
original=[[0 for i in range(C)] for j in range(R)]

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

for i in range(R):
    for j in range(C):
        if i == 0 and j == 0:
            dp[i][j][1]=dp[i][j][0]
        elif j == 0:
            dp[i][j][1]=dp[i][j][0]
            dp[i][j][0] += dp[i - 1][j][0]
        else:
            top, left = -1, dp[i][j - 1]
            if i > 0:
                top = dp[i - 1][j][0]
            if top >= left[0]:
                dp[i][j][0] += top
            else:
                dp[i][j][1] = dp[i][j - 1][1]
                dp[i][j][2] = dp[i][j - 1][2]
                dp[i][j][3] = dp[i][j - 1][3]
                if dp[i][j-1][3] ==-1:
                    curr=original[i][j]
                    dp[i][j][0] += left[0]
                    if dp[i][j-1][1]==-1:
                        dp[i][j][1]=curr
                    elif dp[i][j-1][2]==-1:
                        dp[i][j][2]=curr
                    else:
                        dp[i][j][3]=curr
                else:
                    smallest= min(dp[i][j-1][1:])
                    curr=original[i][j]
                    if curr>smallest:
                        dp[i][j][0]+=dp[i][j-1][0]-smallest
                        if dp[i][j-1][1] == smallest:
                            dp[i][j][1] = curr
                        elif dp[i][j-1][2] == smallest:
                            dp[i][j][2] = curr
                        else:
                            dp[i][j][3] = curr
                    else:
                        dp[i][j][0]+=dp[i][j-1][0]

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