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 copy
from collections import defaultdict as dd
r, c, k = map(int, input().split())

#cnt[col][row] => val
cnt = dd(lambda: dd(int))
for i in range(k):
    row, col, v = map(int, input().split())
    cnt[row][col] = v

def replace(cand, val):
    cand_val, cand_add_vals = copy.deepcopy(cand)
    for i in range(len(cand_add_vals)):
        if cand_add_vals[i] < val:
            cand_add_vals[i] = val
            cand_add_vals.sort()
            break
    return [cand_val, cand_add_vals]

def eval(arr):
    #arr = [past_val, add_vals:[a1, a2, a3]]
    past_val, add_vals = arr
    return past_val + sum(add_vals)

dp = [[[0, [0, 0, 0]] for i in range(c+1)] for j in range(r+1)]
#dp[col][lowest_row] => [val1, val2, val3(biggest)]

for row in range(1, r + 1):
    for col in range(1, c + 1):
        #add_vals = [0, 0, 0]
        val = cnt[row][col]
        #print(row, col, val)
        #left
        cand1 = dp[row][col-1]
        cand1 = replace(cand1, val)
        #up
        cand2 = dp[row-1][col]
        cand2 = [eval(cand2), [0, 0, val]]
        #print(cand1, cand2)

        if eval(cand1) > eval(cand2):
            dp[row][col] = cand1
        elif eval(cand1) <= eval(cand2):
            dp[row][col] = cand2

print(eval(dp[r][c]))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

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

item_gotten = [[0, 0, 0] for _ in range(R+1)]

dp = [[0]*(C+1) for _ in range(R+1)]
for x in range(1, C+1):
	for y in range(1, R+1):
		tmp1 = dp[y][x-1]
		if item_gotten[y][0] < item[y][x]:
			insort(item_gotten[y], item[y][x])
			tmp1 -= item_gotten[y].pop(0)
			tmp1 += item[y][x]

		if dp[y-1][x]+item[y][x] >= tmp1:
			dp[y][x] = dp[y-1][x]+item[y][x]
			item_gotten[y] = [0, 0, item[y][x]]
		else:
			dp[y][x] = tmp1


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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

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

item_gotten = [[0, 0, 0] for _ in range(R+1)]

dp = [[0]*(C+1) for _ in range(R+1)]
for y in range(1, R+1):
	for x in range(1, C+1):
		tmp1 = dp[y][x-1]
		if item_gotten[y][0] < item[y][x]:
			insort(item_gotten[y], item[y][x])
			tmp1 -= item_gotten[y].pop(0)
			tmp1 += item[y][x]

		if dp[y-1][x]+item[y][x] >= tmp1:
			dp[y][x] = dp[y-1][x]+item[y][x]
			item_gotten[y] = [0, 0, item[y][x]]
		else:
			dp[y][x] = tmp1


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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

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

item_gotten = [[0, 0, 0] for _ in range(R+1)]

dp = [[0]*(C+1) for _ in range(R+1)]
for y in range(1, R+1):
	for x in range(1, C+1):
		tmp1 = dp[y][x-1]
		idx = bisect_left(item_gotten[y], item[y][x])
		if idx:
			tmp1 -= item_gotten[y].pop(0)
			insort(item_gotten[y], item[y][x])
			tmp1 += item[y][x]

		if dp[y-1][x]+item[y][x] >= tmp1:
			dp[y][x] = dp[y-1][x]+item[y][x]
			item_gotten[y] = [0, 0, item[y][x]]
		else:
			dp[y][x] = tmp1


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

Submission status: Wrong Answer

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

    dp = [[[0, [0, 0, 0]] for _ in range(C)] for __ in range(R)]
    if D.get((0, 0)):
        dp[0][0] = [D[(0, 0)], [0, 0, D[(0, 0)]]]
    for r in range(R):
        for c in range(C):
            if D.get((r, c)):
                v = D[(r, c)]
                if c != 0:
                    dp[r][c][0] = dp[r][c - 1][0] - sum(dp[r][c - 1][1])
                    dp[r][c][1] = dp[r][c - 1][1]
                    dp[r][c][1].append(v)
                    dp[r][c][1] = sorted(dp[r][c][1])[1:]
                    dp[r][c][0] += sum(dp[r][c][1])
                if r != 0:
                    if dp[r][c][0] <= dp[r - 1][c][0] + v:
                        dp[r][c][0] = dp[r - 1][c][0] + v
                        dp[r][c][1] = [0, 0, v]

            else:
                if c != 0:
                    dp[r][c] = dp[r][c - 1]
                if r != 0:
                    if dp[r][c][0] <= dp[r - 1][c][0]:
                        dp[r][c][0] = dp[r - 1][c][0]
                        dp[r][c][1] = [0, 0, 0]
    print(dp[R - 1][C - 1][0])
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H,W,K = map(int,input().split())
A = [[0 for _ in range(W+1)] for _ in range(H+1)]
for _ in range(K):
    r,c,v = map(int,input().split())
    A[r][c]=v
dp = [[[0 for _ in range(4)] for _ in range(W+1)] for _ in range(H+1)]
if A[H][W]>0:
    dp[H][W][0] = A[H][W]
    dp[H][W][1] = A[H][W]
    dp[H][W][2] = A[H][W]
for j in range(W-1,0,-1):
    for k in range(4):
        dp[H][j][k] = dp[H][j+1][k]
        if A[H][j]>0 and k<3:
            dp[H][j][k] = max(dp[H][j][k],dp[H][j+1][k+1]+A[H][j])
for i in range(H-1,0,-1):
    for k in range(4):
        if k==3:
            dp[i][W][k] = dp[i+1][W][0]
        elif A[i][W]>0:
            dp[i][W][k] = dp[i+1][W][0]+A[i][W]
for i in range(H-1,0,-1):
    for j in range(W-1,0,-1):
        for k in range(4):
            dp[i][j][k] = dp[i][j+1][k]
            if A[i][j]>0 and k<3:
                dp[i][j][k] = max(dp[i][j][k],dp[i][j+1][k+1]+A[i][j],dp[i+1][j][0]+A[i][j])
            else:
                dp[i][j][k] = max(dp[i][j][k],dp[i+1][j][0])
print(dp[1][1][0])

Submission status: Wrong Answer

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

dp = [[[0,0,0,0] for _j in range(c)] for _i in range(r)]
bord = [[0]*c for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v
            
for i in range(r):
    for j in range(c):
        dp[i][j][0] = max(dp[i-1][j])
        dp[i][j][1] = dp[i][j][0] + bord[i][j]
        for x in range(1, 4):
            dp[i][j][x] = max(
                dp[i][j][x],
                dp[i][j-1][x],
                dp[i][j-1][x-1]+bord[i][j],             
            )

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

Submission status: Wrong Answer

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

dp = [[[0, 0, 0, 0] for _j in range(c+1)] for _i in range(r)]
bord = [[0]*(c+1) for _i in range(r)]

for _i in range(k):
    _r, _c, _v = map(int, input().split())
    bord[_r-1][_c-1] = _v

for i in range(r):
    for j in range(c):
        if sum(dp[i][j-1])+max(0, bord[i][j]-min(dp[i][j-1])) <= sum(dp[i-1][j])+bord[i][j]:
            dp[i][j][2] = bord[i][j]
            dp[i][j][3] = sum(dp[i-1][j])
        else:
            dp[i][j] = dp[i][j-1].copy()
            if min(dp[i][j-1][:3]) < bord[i][j]:
                dp[i][j][dp[i][j].index(min(dp[i][j][:3]))] = bord[i][j]

print(sum(dp[r-1][c-1]))

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 21
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve(n=None, s1=None, s2=None):
    r, c, k = getList()
    value = [[0] * c for i in range(r)]

    for i in range(k):
        cr, cc, cv = getZList()
        cv += 1
        value[cr][cc] = cv

    dp = [[[0] * c for i in range(r)] for j in range(4)]
    # print(value)
    # print("")
    for y in range(r):
        for x in range(c):
            curv = value[y][x]
            if y > 0:
                dp[0][y][x] = max(
                    dp[0][y-1][x],
                    dp[1][y-1][x],
                    dp[2][y-1][x],
                    dp[3][y-1][x]
                )
                dp[1][y][x] = max(
                    dp[0][y - 1][x] + curv,
                    dp[1][y - 1][x] + curv,
                    dp[2][y - 1][x] + curv,
                    dp[3][y - 1][x] + curv
                )
            if curv == 0:
                for z in range(4):
                    dp[z][y][x] = max(dp[z][y][x], dp[z][y][x-1])

            else:
                dp[0][y][x] = max(dp[0][y][x], dp[0][y][x-1])
                dp[1][y][x] = max(dp[1][y][x], dp[1][y][x-1], dp[0][y][x-1] + curv)
                dp[2][y][x] = max(dp[2][y][x], dp[2][y][x-1], dp[1][y][x - 1] + curv)
                dp[3][y][x] = max(dp[3][y][x], dp[3][y][x-1], dp[2][y][x - 1] + curv)

    # for dd in dp:
    #     print(dd)

    ans = 0
    for z in range(4):
        for y in range(r):
            for x in range(c):
                ans = max(ans, dp[z][y][x])

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  for j in range(C):
    if i==0 and j==0:
      x=[score[i][j],0,0]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      x=[score[i][j],0,0]
      score[i][j]+=score[i-1][j]
    else:
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
        score[i][j]=score[i][j]+score[i-1][j]
    x.sort()
    
print(score[R-1][C-1])

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  for j in range(C):
    if i==0 and j==0:
      x=[score[i][j],0,0]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      x=[score[i][j],0,0]
      score[i][j]+=score[i-1][j]
    else:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
      score[i][j]=c
    x.sort()
    
print(score[R-1][C-1])

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      x=[score[i][j],0,0]
      score[i][j]+=score[i-1][j]
    else:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      x=[score[i][j],0,0]
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
      x=[score[i][j],0,0]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[score[i][j],0,0]
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(score[i][j], x[0])
      else:
        x=[0]*3
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(c, x[0])
      else:
        x=[0]*3
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(c, x[0])
      score[i][j]=c

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      score[i][j]=c
      if score[i][j-1]+max(0, c-x[0]) > c+score[i-1][j]:
        x[0] =max(c, x[0])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      c=max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0]))
      score[i][j]=c
      if score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j]:
        x[0] =max(c, x[0])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      score[i][j], x[0] =max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0])), max(score[i][j], x[0])*(score[i][j-1]+max(0, score[i][j]-x[0]) > score[i][j]+score[i-1][j])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
score = [[0]*C for i in range(R)]

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

for i in range(R):
  x=[0]*3
  for j in range(C):
    x.sort()
    if i==0 and j==0:
      x[0]=score[i][j]
    elif i==0 and j>0:
      score[i][j], x[0] = score[i][j-1]+max(0, score[i][j]-x[0]), max(score[i][j], x[0])
    elif i>0 and j==0:
      score[i][j]+=score[i-1][j]
    elif i>0 and j>0:
      score[i][j], x[0] =max(score[i][j]+score[i-1][j], score[i][j-1]+max(0, score[i][j]-x[0])), max(score[i][j], x[0])

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

Submission status: Wrong Answer

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

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

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

for i in range(R):
    for j in range(C):
        if i > 0:
            dp[i][j] = dp[i - 1][j]
            carry[i][j] = []
        if j > 0:
            tmp = dp[i][j-1]
            if len(carry[i][j-1]) == 3:
                tmp -= min(carry[i][j-1][0], items[i][j])

            if tmp > dp[i][j]:
                dp[i][j] = dp[i][j - 1]
                carry[i][j] = carry[i][j-1]

        dp[i][j] += items[i][j]
        carry[i][j].append(items[i][j])
        carry[i][j].sort()
        if len(carry[i][j]) >= 4:
            dp[i][j] -= carry[i][j][0]
            del carry[i][j][0]

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 i in range(K):
    r, c, v = map(int, input().split())
    items[r-1][c-1] = v

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

for i in range(R):
    for j in range(C):
        if i > 0:
            dp[i][j] = dp[i - 1][j]
            carry[i][j] = []
        if j > 0:
            tmp = dp[i][j-1]
            if len(carry[i][j-1]) == 3:
                tmp -= min(carry[i][j-1][0], items[i][j])

            if tmp >= dp[i][j]:
                dp[i][j] = dp[i][j - 1]
                carry[i][j] = carry[i][j-1]

        dp[i][j] += items[i][j]
        carry[i][j].append(items[i][j])
        carry[i][j].sort()
        if len(carry[i][j]) >= 4:
            dp[i][j] -= carry[i][j][0]
            del carry[i][j][0]

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        
        dp[i+1][j+1][0] = max(dp[i][j+1])
        for k in range(4): # move downside without selecting
            candidate = dp[i][j+1][k] + reward
            if candidate > dp[i+1][j+1][1]:
                dp[i+1][j+1][1] = candidate
                
        for k in range(3): # move downside without selecting
            if dp[i+1][j][k] + reward > dp[i+1][j+1][k+1]:
                dp[i+1][j+1][k+1] = dp[i+1][j][k] + reward
print(max(dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
rcvs = [list(map(int, input().split())) for _ in range(K)]

for i in range(len(rcvs)):
    rcvs[i][0] -= 1
    rcvs[i][1] -= 1

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

for rcv in rcvs:
    rewards[rcv[0]][rcv[1]] = rcv[2]
    
#%%

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

for i in range(R):
    for j in range(C):
        reward = rewards[i][j]
        for k in range(4): # move downside not select
            for l in range(4):
                if dp[i][j+1][k][l] > dp[i+1][j+1][k][0]:
                    dp[i+1][j+1][k][0] = dp[i][j+1][k][l]
        for k in range(4): # move rightside not select
            for l in range(4):
                if dp[i+1][j][k][l] > dp[i+1][j+1][0][l]:
                    dp[i+1][j+1][0][l] = dp[i+1][j][k][l]
        
        for k in range(3): # move downside select the item
            for l in range(4):
                if dp[i][j+1][k][l] + reward > dp[i+1][j+1][k+1][1]:
                    dp[i+1][j+1][k+1][1] =  dp[i][j+1][k][l] + reward
                    
        for k in range(4): # move rightside select the item
            for l in range(3):
                if dp[i+1][j][k][l] + reward > dp[i+1][j+1][1][l+1]:
                    dp[i+1][j+1][1][l+1] =  dp[i+1][j][k][l] + reward
#%%
                    
answer = 0
for i in range(R):
    for j in range(C):
        for k in range(4):
            for l in range(4):
                if answer < dp[i+1][j+1][k][l]:
                    answer = dp[i+1][j+1][k][l]
print(answer)

Submission status: Wrong Answer

Solution code:
R, C, K, *rcv = map(int, open(0).read().split())
items = {(r, c): v for r, c, v in zip(*[iter(rcv)] * 3)}

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
dp[1][1][1] = 0 if (1, 1) not in items else items[(1, 1)]
for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            if j < C:
                dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
            if (i, j + 1) in items and k < 3:
                dp[i][j + 1][k + 1] = max(
                    dp[i][j + 1][k + 1], dp[i][j][k] + items[(i, j + 1)]
                )
            if i < R:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k])
            if (i + 1, j) in items and k < 3:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + items[(i + 1, j)])
print(max(dp[R][C]))

Submission status: Wrong Answer

Solution code:
r,c,k=map(int,input().split())
goods=[list(map(int,input().split())) for _ in range(k)]
g=[[0]*c for _ in range(r)]
for cr,cc,ck in goods:
    g[cr-1][cc-1]=ck

dp=[[[-1]*4 for _ in range(c+1)] for _ in range(r+1)]

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

for i in range(r):
    for j in range(c):
        for k in range(3):
            if g[i][j]>0 and dp[i+1][j][k]>=0:
                dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
            else:
                dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
        
        dp[i+1][j+1][0]=max(max(dp[i][j+1]),dp[i+1][j][0])
        
        if g[i][j]>0 and dp[i+1][j+1][0]>=0:
            dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
            
print(max(dp[r][c]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w,K = map(int,input().split())
dp = [[[0]*4 for j in range(w+2)] for i in range(h+2)]
p =  [[0 for j in range(w+1)] for i in range(h+1)]
for _ in range(K):
    x,y,v = map(int,input().split())
    p[x][y] = v
    
for i in range(1, h+1):
    for j in range(1, w+1):
        for k in range(3):
            
            dp[i][j+1][3-k] = max(dp[i][j][3-k], dp[i][j][2-k]+p[i][j])
            
        dp[i+1][j][0] = max(dp[i][j+1])

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

Submission status: Wrong Answer

Solution code:
h,w,k = map(int, input().split())
item = [[0 for _ in range(w)] for _ in range(h)]
for i in range(k):
    y,x,v = list(map(int,input().split()))
    item[y-1][x-1]=v

dp = [[[0 for _ in range(w)] for _ in range(h)] for _ in range(3)]

for y in range(h):
    for x in range(w):
        for i in range(3):
            if y<h-1:
                dp[0][y+1][x]=max(dp[0][y+1][x],dp[i][y][x])
            if x<w-1:
                dp[i][y][x+1]=max(dp[i][y][x+1],dp[i][y][x])

        if item[y][x]>0:
            for i in range(3):
                if y<h-1:
                    dp[0][y+1][x]=max(dp[0][y+1][x],dp[i][y][x] + item[y][x])
                if x<w-1 and i!=2:
                    dp[i+1][y][x+1]=max(dp[i+1][y][x+1],dp[i][y][x] + item[y][x])



ans=[dp[0][-1][-1] + item[-1][-1],
     dp[1][-1][-1] + item[-1][-1],
     dp[2][-1][-1] + item[-1][-1]]
print(max(ans))