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:
R, C, K = map(int, input().split())
T = [[0]*(C+1) for _ in range(R+1)]
dp = [[[-1]*(C+1) for _ in range(R+1)] for _ in range(4)]
for i in range(K):
    r, c, v = map(int, input().split())
    T[r][c] = v
dp[1][1][1] = T[1][1]
dp[0][1][1] = 0

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0 or i + j == R + C:
            for k in range(4):
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[0][i][j] = max(dp[0][i][j], dp[l][i-1][j])
                        dp[1][i][j] = max(dp[1][i][j], dp[l][i-1][j] + T[i][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for k in range(4):
    ans = max(ans, dp[k][R][C])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque


'''
dp[i][j]...(0,0)からスタートして(i,j)に到達する経路のなかでの最大の価値
dp[i][j]=max(dp[i][j-1],dp[i-1][j]) + v[i][j]


dp[i][j][k] ... (0,0)からスタートして(i,j)に到達する経路のなかでi行目ですでにk(以下)個選んでいるときの最大の価値

遷移は、i,jのvを拾うときと拾わないときのmax
dp[i][j][k] = max(dp[i][j-1][k-1]+V[i,j], dp[i][j-1][k]
                  ) #k個のままのほうが価値が高いかk-1から拾ったほうが価値が高いか

上からの遷移 # 拾うか拾わないかでこうかな
dp[i][j][0]=max(dp[i][j-1][0],max_k(dp[i-1][j]))
dp[i][j][1]=max(dp[i][j-1][0],max_k(dp[i-1][j][k])+V[i][j])
'''

R, C, K = ints()
RCV = read_tuple(K)
V = [[0] * C for _ in ra(R)]
for r, c, v in RCV:
    V[r - 1][c - 1] = v

dp = [[[0] * 4 for _ in ra(C)] for _ in ra(R)]
# dp[0][0][0] = 0
for k in ra(1, 4):
    dp[0][0][k] = V[0][0]
for j in ra(1, C):
    for k in ra(1, 4):
        dp[0][j][k] = max(dp[0][j - 1][k], dp[0][j - 1][k - 1] + V[0][j])

for i in ra(1, R):
    dp[i][0][0] = max(dp[i - 1][0])
    dp[i][0][1] = dp[i][0][0] + V[i][0]
    dp[i][0][2] = dp[i][0][0] + V[i][0]
    dp[i][0][3] = dp[i][0][0] + V[i][0]


exit(0)
for i in ra(1, R):
    for j in ra(1, C):
        ma = max(dp[i - 1][j])
        dp[i][j][0] = max(dp[i][j - 1][0],  # 左
                          ma)  # 上

        dp[i][j][1] = max(dp[i][j - 1][0] + V[i][j],  # 左から来て拾う
                          dp[i][j - 1][1],  # 拾わないほうが大きい場合もある
                          ma + V[i][j])  # 上から来て拾う

        dp[i][j][2] = max(dp[i][j - 1][1] + V[i][j], dp[i][j - 1][2])
        dp[i][j][3] = max(dp[i][j - 1][2] + V[i][j], dp[i][j - 1][3])


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

# print(*values, sep='\n')
# print(*dp, sep='\n')

Submission status: Wrong Answer

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0:
            for k in range(4):
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[0][i][j] = max(dp[0][i][j], dp[l][i-1][j])
                        dp[1][i][j] = max(dp[1][i][j], dp[l][i-1][j] + T[i][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for i in range(R+1):
    for j in range(C+1):
        for k in range(4):
            ans = max(ans, dp[k][i][j])
print(ans)

Submission status: Wrong Answer

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0:
            for k in range(4):
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j])
                        if k >= 1:
                            dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j] + T[i][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for i in range(R+1):
    for j in range(C+1):
        for k in range(4):
            ans = max(ans, dp[k][i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline


def main():
    h, w, k = map(int, input().split())
    values = np.zeros((h, w), dtype=np.int64)
    items = np.array([list(map(int, input().split())) for _ in range(k)])
    ys, xs, vs = items[:, 0] - 1, items[:, 1] - 1, items[:, 2]
    values[ys, xs] = vs

    DP = np.zeros((w+1, 4), dtype=np.int64)
    for y in range(h):
        line = values[y]
        for x in range(w):
            DP[x+1, 1:] = np.maximum(DP[x, :-1] + line[x], DP[x, 1:])

        DP[:-1, 0] = np.max(DP[1:], axis=1)

    print(DP.max())


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0:
            for k in range(4):
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j])
                    if k >= 1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j] + T[i][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for i in range(R+1):
    for j in range(C+1):
        for k in range(4):
            ans = max(ans, dp[k][i][j])
print(ans)

Submission status: Wrong Answer

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0:
            for k in range(4):
                for l in range(1, 4):
                    if dp[l][i-1][j] != -1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j] + T[i][j])
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for i in range(R+1):
    for j in range(C+1):
        for k in range(4):
            ans = max(ans, dp[k][i][j])
print(ans)

Submission status: Wrong Answer

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

for i in range(1, R+1):
    for j in range(1, C+1):
        if T[i][j] != 0:
            for k in range(4):
                for l in range(4):
                    if dp[l][i-1][j] != -1:
                        dp[k][i][j] = max(dp[k][i][j], dp[l][i-1][j] + T[i][j])
                if dp[k][i][j-1] != -1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
                if dp[k-1][i][j-1] != -1 and k >= 1:
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + T[i][j])
ans = 0
for i in range(R+1):
    for j in range(C+1):
        for k in range(4):
            ans = max(ans, dp[k][i][j])

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
R, C, K = map(int, input().split())
items = dict()
for i in range(K):
    r, c, v = map(int, input().split())
    if not r in items:
        items[r] = dict()
    items[r][c] = v
DP = [[0]*(C+1) for i in range(R+1)]
DP_items = [[list()]*(C+1) for i in range(R+1)]
for i in range(R):
    i = i+1
    for j in range(C):
        j = j+1
        if i in items:
            if j in items[i]:
                if DP[i-1][j] >= DP[i][j-1]:
                    DP[i][j] = DP[i-1][j] + items[i][j]
                    DP_items[i][j] = [items[i][j]]
                elif len(DP_items[i][j-1])==3:
                    DP[i][j] = max(DP[i][j-1],
                                   DP[i][j-1] - min(DP_items[i][j-1])
                                   + items[i][j])
                    if min(DP_items[i][j-1])<items[i][j]:
                        DP_items[i][j] = DP_items[i][j-1] + [items[i][j]]
                        DP_items[i][j].remove(min(DP_items[i][j-1]))
                else:
                    DP[i][j] = DP[i][j-1] + items[i][j]
                    DP_items[i][j] = DP_items[i][j-1] + [items[i][j]]
                continue
        if DP[i-1][j] >= DP[i][j-1]:
            DP[i][j] = DP[i-1][j]
            DP_items[i][j] = list()
        else:
            DP[i][j] = DP[i][j-1]
            DP_items[i][j] = DP_items[i][j-1][:]

#for l in DP_items:
#    print(l)
#for l in DP:
#    print(l)
print(DP[R][C])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
H,W,K=map(int,input().split())
G=[[0]*W for i in range(H)]

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

dp=[[[-1]*4 for i in range(W)] for i in range(H)]

dp[0][0][0]=0
if G[0][0]!=0:
    dp[0][0][1]=G[0][0]

for i in range(H):
    for j in range(W):
        for k in range(3):
            if i+1<H:
                dp[i+1][j][1]=max(dp[i+1][j][1],max(dp[i][j])+G[i+1][j])
                dp[i+1][j][0]=max(dp[i+1][j][0],max(dp[i][j]))
            if j+1<W:
                dp[i][j+1][k+1]=max(dp[i][j+1][k+1],dp[i][j][k]+G[i][j+1])
                dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k])
print(max(dp[H-1][W-1]))

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    R, C, K = NMI()
    grid = defaultdict(int)
    for i in range(K):
        r, c, v = NMI()
        grid[(r-1)*10**4 + c-1] = v

    # dp = [[[0]*4 for _ in range(C+2)] for _ in range(R+2)]
    # (r, c, i) -> r * 10**8 + c * 10**4 + i
    def get_point(r, c, i):
        return r * 10**8 + c * 10**4 + i

    dp = defaultdict(int)
    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now = dp[get_point(r, c, i)]
                val = grid[(r-1)*10**4 + c-1]

                if i == 3:
                    # 取らずに下
                    dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                    continue

                # 取って下
                dp[get_point(r+1, c, 0)] = max(now + val, dp[get_point(r+1, c, 0)])
                # 取らずに下
                dp[get_point(r+1, c, 0)] = max(now, dp[get_point(r+1, c, 0)])
                # 取って横
                dp[get_point(r, c+1, i+1)] = max(now + val, dp[get_point(r, c+1, i+1)])
                # 取らずに横
                dp[get_point(r, c+1, i)] = max(now, dp[get_point(r, c+1, i)])

    dp_RC = [dp[get_point(R, C, i)] for i in range(4)]
    print(max(dp_RC))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal


from numba import jit

@jit
def run_dp(MAP, dp, Rs, Cs):
    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][k])
                if MAP[i][j]:
                    dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][k]+MAP[i][j])

                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
                if MAP[i][j] and k < 3:
                    dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j-1][k] + MAP[i][j])
    return dp

def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    #Rs = sorted(list(Rs))
    Rs = np.array(sorted(list(Rs)))
    #Cs = sorted(list(Cs))
    Cs = np.array(sorted(list(Cs)))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    #MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    MAP = np.zeros((len(Rs)+1, len(Cs)+1), dtype= np.int32)
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    #dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]
    dp = np.zeros((len(Rs)+1, len(Cs)+1, 4), dtype= np.int32)

    run_dp(MAP, dp, Rs, Cs)

    #print(dp)
    #for m in MAP:
    #   print(m)
    dp = run_dp(MAP, dp, Rs, Cs)

    print(max(dp[len(Rs)][len(Cs)]))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
def main():
    r, c, k = map(int, input().split())
    d = [[0]*c for _ in [0]*r]
    for _ in range(k):
        t1, t2, t3 = map(int, input().split())
        d[t1-1][t2-1] = t3
    dp = [[0]*4 for _ in [0]*c]
    dp[0][1] = d[0][0]
    for x in range(r-1):
        d2 = d[x]
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c-1):
            d3 = d2[y+1]
            for z in range(3):
                dp[y+1][z+1] = max(dp[y+1][z+1], dp[y][z]+d3)
            for z in range(4):
                dp[y+1][z] = max(dp[y+1][z], dp[y][z])
        if x != r-1:
            d2 = d[x+1]
            for y in range(c):
                dp2[y][1] = max(dp2[y][1], max(dp[y])+d2[y])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        dp = dp2
    print(max(dp[c-1]))


main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal



def run():
    R,C,K = map(int, sysread().split())
    Rs = set()
    Cs = set()
    vals = []
    for i in range(K):
        r, c, v = map(int, sysread().split())
        vals.append((r, c, v))
        Rs.add(r)
        Cs.add(c)
    Rs = sorted(list(Rs))
    Cs = sorted(list(Cs))
    r_idx = {r:i+1 for i, r in enumerate(Rs)}
    c_idx = {c:i+1 for i, c in enumerate(Cs)}
    MAP = [[0] * (len(Cs)+1) for _ in range(len(Rs)+1)]
    for r,c,v in vals:
        rn = r_idx[r]
        cn = c_idx[c]
        MAP[rn][cn] = v

    dp = [[[0] * 4 for _ in range(len(Cs)+1)] for i in range(len(Rs)+1)]

    for i in range(1, len(Rs)+1):
        for j in range(1, len(Cs)+1):
            for k in range(4):
                dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][k])
                if MAP[i][j]:
                    dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][k]+MAP[i][j])

                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
                if MAP[i][j] and k < 3:
                    dp[i][j][k+1] = dp[i][j-1][k] + MAP[i][j]
    #print(dp)
    #for m in MAP:
    #    print(m)
    print(max(dp[len(Rs)][len(Cs)]))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

r,c,k=MI()
dp=[[[-inf]*4 for j in range(c)]  for i in range(r)]
val=[[0]*c for i in range(r)]
for i in range(k):
    x,y,v=MI()
    val[x-1][y-1]=v
if val[0][0]>0:
    dp[0][0][1]=val[0][0]
    dp[0][0][0]=0
else:
    dp[0][0][0]=0
#print(dp)
for i in range(r):
    for j in range(c):
            if i<r-1:
                if val[i+1][j]>0:
                    for k in range(4):
                            dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][k]+val[i+1][j])
                            dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][k])
                else:
                    for k in range(4):
                            dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][k])                 
            if j<c-1:
                if val[i][j+1]>0:
                    for k in range(1,4):
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k-1]+val[i][j+1])
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k])
                else:
                    for k in range(4):
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k])
#print(val)
print(max(dp[-1][-1]))
#print(dp)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

r,c,k=MI()
dp=[[[-1]*4 for j in range(c)]  for i in range(r)]
val=[[0]*c for i in range(r)]
for i in range(k):
    x,y,v=MI()
    val[x-1][y-1]=v
if val[0][0]>0:
    dp[0][0][1]=val[0][0]
    dp[0][0][0]=0
else:
    dp[0][0][0]=0
#print(dp)
for i in range(r):
    for j in range(c):
            if i<r-1:
                if val[i+1][j]>0:
                    for k in range(4):
                        if dp[i][j][k]>=0:
                            dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][k]+val[i+1][j])
                            dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][k])
                else:
                    for k in range(4):
                        if dp[i][j][k]>=0:
                            dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][k])                 
            if j<c-1:
                if val[i][j+1]>0:
                    for k in range(1,4):
                        if dp[i][j][k-1]>=0:
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k-1]+val[i][j+1])
                        if dp[i][j][k]>=0:
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k])
                else:
                    for k in range(4):
                        if dp[i][j][k]>=0:
                            dp[i][j+1][k]=max(dp[i][j+1][k],dp[i][j][k])
#print(val)
print(max(dp[-1][-1]))
#print(dp)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from collections import defaultdict

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

d = defaultdict(list)
for r, c, v in rcv:
    d[r].append([c, v])

for r in d.keys():
    d[r].sort()

dp = [0] * (C + 1)

for r, li in sorted(d.items()):
    prev = dp
    for i, (cs, vs) in enumerate(li):
        mx = 0
        for cg, vg in li[i+1:]:
            val = vs + mx + vg
            dp[cg] = max(dp[cg], prev[cs] + val)
            mx = max(mx, vg)

        dp[cs] = max(dp[cs], prev[cs] + vs)

    for c in range(C):
        dp[c+1] = max(dp[c+1], dp[c])

ans = dp[C]
print(ans)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
# 入力
R, C, K = map(int, input().split())
point_map = [[0]*(C+1) for _ in range(R+1)]
max_point = [[[0]*4 for _ in range(C + 1)]
             for _ in range(R + 1)]


for i in range(K):
    _r, _c, _k = map(int, input().split())
    point_map[_r][_c] = _k

for i in range(1, R+1):
    im = i-1
    for j in range(1, C + 1):
        jm = j - 1
        mxi = max_point[i]
        mxim = max_point[i-1]
        mxij = mxi[j]
        mxijm = mxi[jm]
        point = point_map[i][j]
        mxij[0] = max(
            mxijm[0],
            mxim[j][0],
            mxim[j][1],
            mxim[j][2],
            mxim[j][3]
        )

        mxij[1] = mxij[0] + point
        if mxij[1] < mxijm[1]:
            mxij[1] = mxijm[1]

        mxij[2] = mxijm[1] + point
        #if mxij[2] < mxijm[2]:
        #    mxij[2] = mxijm[2]
        mxij[3] = mxijm[2] + point
        if mxij[3] < mxijm[3]:
            mxij[3] = mxijm[3]
        # print(i, j, max_point[i][j])

print(max(max_point[R][C]))

Submission status: Wrong Answer

Solution code:
# 入力
R, C, K = map(int, input().split())
point_map = [[0]*(C+1) for _ in range(R+1)]
max_point = [[[0]*4 for _ in range(C + 1)]
             for _ in range(R + 1)]


for i in range(K):
    _r, _c, _k = map(int, input().split())
    point_map[_r][_c] = _k

for i in range(1, R+1):
    im = i-1
    for j in range(1, C + 1):
        jm = j - 1
        mxi = max_point[i]
        mxim = max_point[i-1]
        mxij = mxi[j]
        mxijm = mxi[jm]
        point = point_map[i][j]
        mxij[0] = max(
            mxijm[0],
            mxim[j][0],
            mxim[j][1],
            mxim[j][2],
            mxim[j][3]
        )

        mxij[1] = mxij[0] + point
        #if mxij[1] < mxijm[1]:
        #    mxij[1] = mxijm[1]

        mxij[2] = mxijm[1] + point
        #if mxij[2] < mxijm[2]:
        #    mxij[2] = mxijm[2]
        mxij[3] = mxijm[2] + point
        if mxij[3] < mxijm[3]:
            mxij[3] = mxijm[3]
        # print(i, j, max_point[i][j])

print(max(max_point[R][C]))

Submission status: Wrong Answer

Solution code:
import copy
import heapq

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

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

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

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

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

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