Problem:


Score : 500 points

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

Find the maximum possible sum of the values of items he picks up.

Constraints

1 \leq R, C \leq 3000

1 \leq K \leq \min(2 \times 10^5, R \times C)

1 \leq r_i \leq R

1 \leq c_i \leq C

(r_i, c_i) \neq (r_j, c_j) (i \neq j)

1 \leq v_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

Output

Print the maximum possible sum of the values of items Takahashi picks up.

Sample Input 1

2 2 3
1 1 3
2 1 4
1 2 5

Sample Output 1

8

He has two ways to get to the goal:

Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.

Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.

Thus, the maximum possible sum of the values of items he picks up is 8.

Sample Input 2

2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2

Sample Output 2

29

We have four items in the 1-st row. The optimal choices are as follows:

Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.

Sample Input 3

4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

Sample Output 3

142



Sample input:
2 2 3
1 1 3
2 1 4
1 2 5


Sample output:
8

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, k = map(int, input().split())
    grid = [[0] * W for _ in range(H)]
    for _ in range(k):
        r, c, v = map(int, input().split())
        grid[r - 1][c - 1] = v

    dp = [[[0] * W for _ in range(H)] for _ in range(4)]
    if grid[0][0] != 0:
        dp[1][0][0] = grid[0][0]
    for h in range(H):
        for w in range(W):
            if grid[h][w] != 0:
                for i in range(4):
                    if h - 1 >= 0:
                        # 取らない
                        dp[0][h][w] = max(dp[0][h][w], dp[i][h - 1][w])
                        # 取る
                        dp[1][h][w] = max(dp[1][h][w], dp[i][h - 1][w] + grid[h][w])
                for j in range(4):
                    if w - 1 >= 0:
                        # 取らない
                        dp[j][h][w] = max(dp[j][h][w], dp[j][h][w - 1])
                        # 取る
                        if j - 1 >= 0:
                            dp[j][h][w] = max(dp[j][h][w], dp[j - 1][h][w - 1] + grid[h][w])
    res = 0
    for h in range(H):
        for w in range(W):
            for i in range(4):
                res = max(res, dp[i][h][w])
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def main():
    (h,w,k),*t=[[*map(int,t.split())]for t in open(0)]
    dp=[[0]*(w+1)for _ in range(h+1)]
    a=0
    for y,x,v in t:dp[y][x]=v
    for i in range(1,h+1):
        x=y=z=p=0
        for j in range(1,w+1):
            v=dp[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<=dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
            if a<dp[i][j]:a=dp[i][j]
    print(a)
main()

Submission status: Wrong Answer

Solution code:
h, w, k2 = map(int, input().split())
values = [[0 for j in range(w)] for i in range(h)]
for i in range(k2):
    y, x, v = map(int, input().split())
    y -= 1
    x -= 1
    values[y][x] = v

dp = [[[0 for k in range(4)] for j in range(w)] for i in range(h)]

for j in range(w):
    for k in range(4):
        dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k])
        if values[0][j] > 0:
            dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k - 1] + values[0][j])

for i in range(1, h):
    for j in range(w):

        # 0個選ぶとき
        dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j]))

        if j > 0:
            dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0])
        
        # 1個以上選ぶとき
        for k in range(1, 4):
            if j == 0:
                if k == 1 and values[i][j] > 0:
                    dp[i][j][k] = max(dp[i][j][k], max(dp[i - 1][j]) + values[i][j])
                continue

            if values[i][j] > 0:
                dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + values[i][j])
                if k == 1:
                    dp[i][j][k] = max(dp[i][j][k], max(dp[i - 1][j]) + values[i][j])

            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k])

for i in range(h):
    print(*dp[i])

ans = max(dp[-1][-1])
print(ans)

Submission status: Wrong Answer

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

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

for r, c, v in RCV:
    F[r - 1][c - 1] = v

r = 0
S = []
for c in range(C):
    if len(S) < 3:
        S.append(F[r][c])
        S = sorted(S)
        q = sum(S)
    else:
        if S[0] >= F[r][c]:
            q = sum(S)
        else:
            S[0] = F[r][c]
            q = sum(S)
            S = sorted(S)

    Q[r][c] = q


c = 0
S = []
for c in range(C):
    if len(S) < 3:
        S.append(F[r][c])
        S = sorted(S)
        q = sum(S)
    else:
        if S[0] >= F[r][c]:
            q = sum(S)
        else:
            S[0] = F[r][c]
            q = sum(S)
            S = sorted(S)

    Q[r][c] = q

for r in range(1, R):
    for c in range(1, C):
        Q[r][c] = max(Q[r - 1][c], Q[r][c - 1]) + F[r][c]

print(Q[-1][-1])

Submission status: Wrong Answer

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

for r in range(1, R+1):
    for c in range(1, C+1):
        A[r][c][1] = max(A[r-1][c][1], A[r][c-1][1])
        A[r][c][2] =max(A[r-1][c][2], A[r][c-1][2]) 
        A[r][c][3] =max(A[r-1][c][3], A[r][c-1][3])
        if B[r][c] != -1:
            A[r][c][1] = max(A[r][c][1], A[r][c-1][0]+B[r][c], A[r-1][c][1]+B[r][c])
            A[r][c][2] = max(A[r][c][2], A[r][c-1][1]+B[r][c], A[r-1][c][2]+B[r][c])
            A[r][c][3] = max(A[r][c][3], A[r][c-1][2]+B[r][c], A[r-1][c][3]+B[r][c])

print(max(A[R][C][0], A[R][C][1], A[R][C][2], A[R][C][3]))

Submission status: Wrong Answer

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

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

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

    dp[0][0] = [0] + [items[0][0]] * 3
    for r in range(1, R):
        for i in range(1, 4):
            dp[0][r][i] = max(dp[0][r - 1][i], dp[0][r - 1][i - 1] + items[0][r])

    for c in range(1, C):
        dp[c][0] = [dp[c - 1][0][1] + items[c][0]] * 4
        for r in range(1, R):
            dp[c][r][0] = max(dp[c - 1][r]) + items[c][r]
            dp[c][r][0] = max(dp[c][r][0], dp[c][r - 1][0])
            #print("dp[{}][{}]={}".format(c, r, dp[c][r]))
            for i in range(1, 4):
                dp[c][r][i] = max(dp[c][r - 1][i], dp[c][r - 1][i - 1] + items[c][r], dp[c][r][i - 1])

    #for c in range(C):
    #    print("dp[{}]={}".format(c, dp[c]))

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



if __name__ == "__main__":
    main()

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


class Top3:
    def __init__(self, ls):
        self.a, self.b, self.c = ls  # aが一番大きい

    def append(self, x):
        # xを突っ込む 3つのままに保つ
        # ついでに取れなくなったもののスコアを吐き出す
        if x < self.c:
            return x
        if x < self.b:
            ret = self.c
            self.c = x
            return ret
        if x < self.a:
            ret = self.c
            self.c = self.b
            self.b = x
            return ret
        if x >= self.a:
            ret = self.c
            self.c = self.b
            self.b = self.a
            self.a = x
            return ret



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

同じ行のアイテムは3つしか拾えないのはどうしようか...
各行でアイテムの価値をsortedlistに突っ込んでおくのは？ top3つ持ってればいいからそんなデータ構造いらない

dpだけじゃなくて行方向にいままでどんな要素を選んだか記録する必要あり
ritmとする。
横方向の移動があったときだけ中身を継承する(これはスコア算出に使う)
'''

R, C, K = ints()
RCV = read_tuple(K)
values = defaultdict(lambda: 0)
for r, c, v in RCV:
    values[r - 1, c - 1] = v

dp = [[-1] * C for _ in ra(R)]
dp[0][0] = values[0, 0]
for i in ra(1, R):
    dp[i][0] = values[i, 0] + dp[i - 1][0]


# 素直なdpの実装
for i in ra(R):
    top3 = Top3(ls=[values[i, 0], 0, 0])  # 行のitemをもつ
    for j in ra(1, C):
        if i - 1 >= 0:
            top = dp[i - 1][j] + values[i, j]
        else:
            top = -1
        left = dp[i][j - 1] + values[i, j] - \
            top3.append(values[i, j])  # top3を考慮した横のスコア

        # もしtopが大きければtop3は採用しない
        if top >= left:
            dp[i][j] = top
            top3 = Top3(ls=[values[i, j], 0, 0])
        else:
            dp[i][j] = left
            # appendしたtop3を次も継続して使う
        # dp[i][j] = max(top, left)

print(dp[-1][-1])
# print(*dp, sep='\n')

Submission status: Wrong Answer

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

R, C, K = map(int, input().split())
INF = 10**18
dp = [[[[-INF] * 4 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]
items = [[0] * (C+1) for _ in range(R+1)]
dp[1][0][0][0] = dp[0][1][0][0] = 0
for _ in range(K):
    r, c, v = map(int, input().split())
    items[r][c] = v
for i in range(1, R+1):
    for j in range(1, C+1):
        for k in range(4):
            for l in range(4):
                dp[i][j][0][0] = max(dp[i][j][0][0], dp[i-1][j][k][l], dp[i][j-1][k][l])
        for k in range(1, 4):
            for l in range(4):
                dp[i][j][0][k] = max(dp[i][j][0][k], dp[i-1][j][l][k])
                dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j-1][k][l])
                if items[i][j]:
                    dp[i][j][1][k] = max(dp[i][j][1][k], dp[i-1][j][l][k-1] + items[i][j])
                    dp[i][j][k][1] = max(dp[i][j][k][1], dp[i][j-1][k-1][l] + items[i][j])
ans = 0
for k in range(4):
    for l in range(4):
        ans = max(ans, dp[R][C][k][l])
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, c, k = map(int, input().split())
ans = [[0]*(c+1) for _ in range(r+1)]
for x in stdin.readlines():
    R, C, v = list(map(int, x.split()))
    ans[R][C] = v

for i in range(1, r+1):
    tmp = []
    for j in range(1, c+1):
        if ans[i][j-1] <= ans[i-1][j]:
            tmp = []
            if ans[i][j]:
                tmp.append(ans[i][j])
            ans[i][j] += ans[i-1][j]
        else:
            if ans[i][j]:
                tmp.append(ans[i][j])
            if len(tmp) > 3:
                tmp.sort()
                ans[i][j] += ans[i][j-1] - min(tmp)
                tmp.pop(0)
            else:
                ans[i][j] += ans[i][j-1]
print(ans[r][c])

Submission status: Wrong Answer

Solution code:
import heapq
import sys
input = lambda: sys.stdin.readline().rstrip()
r, c, k = map(int, input().split())
# (価値v,同じ行でいくつ拾ったか)
dp = [[[0, 0] for _ in range(c + 5)] for _ in range(r + 5)]
# pt = [[0 for _ in range(c+5)] for _ in range(r+5)]

for i in range(k):
    r1, c1, v = map(int, input().split())
    dp[r1][c1] = [v, 0]

# for i in dp:
#     print(i)

for i in range(1, r + 1):
    pt = []
    heapq.heapify(pt)
    cnt = 0
    for j in range(1, c + 1):
        isget = False
        mini = 0
        # 何か落ちている
        if dp[i][j][0] != 0:
            heapq.heappush(pt, dp[i][j][0])
            cnt += 1
            isget = True
        ans1 = dp[i - 1][j][0] + dp[i][j][0]
        # 同じ行で4つ以上なら最小値捨てる
        if cnt == 4:
            mini = heapq.heappop(pt)
            cnt -= 1
        ans2 = dp[i][j - 1][0] + dp[i][j][0] - mini

        if ans1 >= ans2:
            if isget:
                dp[i][j] = [ans1, 1]
            else:
                dp[i][j] = [ans1, 0]
        else:
            dp[i][j] = [ans2, cnt]

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

# for i in dp:
#     print(i)

Submission status: Wrong Answer

Solution code:
from sys import stdin

r, c, k = map(int, input().split())
ans = [[0]*(c+1) for _ in range(r+1)]
for x in stdin.readlines():
    R, C, v = list(map(int, x.split()))
    ans[R][C] = v

for i in range(1, r+1):
    tmp = []
    for j in range(1, c+1):
        if ans[i][j-1] <= ans[i-1][j]:
            tmp = []
            if ans[i][j]:
                tmp.append(ans[i][j])
            ans[i][j] += ans[i-1][j]
        else:
            if ans[i][j]:
                tmp.append(ans[i][j])
            if len(tmp) > 3:
                tmp.sort()
                ans[i][j] += ans[i][j-1] - min(tmp)
                tmp.pop(0)
            else:
                ans[i][j] += ans[i][j-1]
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    (h,w,k),*t=[[*map(int,t.split())]for t in open(0)]
    dp=[[0]*(w+1)for _ in range(h+1)]
    for y,x,v in t:dp[y][x]=v
    for i in range(1,h+1):
        x=y=z=p=0
        for j in range(1,w+1):
            v=dp[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<=dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
    a=dp[h][w]
    dp=[[0]*(w+1)for _ in range(h+1)]
    for y,x,v in t:dp[y][x]=v
    for i in range(1,h+1):
        x=y=z=p=0
        for j in range(1,w+1):
            v=dp[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
    print(max(a,dp[h][w]))
main()

Submission status: Wrong Answer

Solution code:
R, C, K = list(map(int,input().split()))
v = [[0 for _ in range(C)] for _ in range(R)]
for i in range(K):  
    in1, in2, in3 = list(map(int,input().split()))
    v[in1 - 1][in2 - 1] = in3
    
dp = [[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
dp[0][0][0] = 0
dp[0][0][1] = v[0][0]
dp[0][0][2] = 0
dp[0][0][3] = 0
m = [[0 for _ in range(C)] for _ in range(R)]
m[0][0] = max(dp[0][0])

for i in range(1, R):
    dp[i][0][3] = max(dp[i - 1][0][3], dp[i - 1][0][2] + v[i][0])
    dp[i][0][2] = max(dp[i - 1][0][2], dp[i - 1][0][1] + v[i][0])
    dp[i][0][1] = max(dp[i - 1][0][1], dp[i - 1][0][0] + v[i][0])
    dp[i][0][0] = dp[i - 1][0][0]
    m[i][0] = max(dp[i][0])
    
for i in range(1, C):
    dp[0][i][3] = max(dp[0][i - 1][3], dp[0][i - 1][2] + v[0][i])
    dp[0][i][2] = max(dp[0][i - 1][2], dp[0][i - 1][1] + v[0][i])
    dp[0][i][1] = max(dp[0][i - 1][1], dp[0][i - 1][0] + v[0][i])
    dp[0][i][0] = dp[0][i - 1][0]
    m[0][i] = max(dp[0][i])

for i in range(1, R):
    for j in range(1, C):
        if v[i][j]:
            dp[i][j][3] = max(dp[i][j - 1][3], v[i][j] + dp[i][j - 1][2], v[i][j] + m[i - 1][j])
            dp[i][j][2] = max(dp[i][j - 1][2], v[i][j] + dp[i][j - 1][1], v[i][j] + m[i - 1][j])
            dp[i][j][1] = max(dp[i][j - 1][1], v[i][j] + dp[i][j - 1][0], v[i][j] + m[i - 1][j])
            dp[i][j][0] = max(dp[i][j - 1][0], m[i - 1][j])
        else:
            dp[i][j][3] = max(dp[i][j - 1][3], m[i - 1][j])
            dp[i][j][2] = max(dp[i][j - 1][2], m[i - 1][j])
            dp[i][j][1] = max(dp[i][j - 1][1], m[i - 1][j])
            dp[i][j][0] = max(dp[i][j - 1][0], m[i - 1][j])
        m[i][j] = max(dp[i][j])
ans = m[R - 1][C - 1]
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)
                    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:
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)
                    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
        
print(dp[r][c])

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
import collections
D=collections.defaultdict(int)
r,c,k=map(int,input().split())
for _ in range(k):
	ri,ci,v=map(int,input().split())
	ri-=1
	ci-=1
	D[ri*10**5+ci]=v

DP=[[[0]*4 for _ in range(c)] for i in range(r)]
for i in range(r):
	for j in range(c):
		v=D[i*10**5+j]
		if v!=0:
			DP[i][j][1]=max(DP[i-1][j])+v
		for ii in range(4):
			DP[i][j][0]=max(DP[i][j][0],DP[i-1][j][ii])
			if v!=0:
				DP[i][j][1]=max(DP[i][j][1],DP[i-1][j][ii]+v)
			DP[i][j][ii]=max(DP[i][j][ii],DP[i][j-1][ii])
			if v!=0 and ii!=0:
				DP[i][j][ii]=max(DP[i][j][ii],DP[i][j-1][ii-1]+v)
		
ans=max(DP[r-1][c-1])
print(ans)

Submission status: Wrong Answer

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template
R,C,K = inputlist()
dp = [[[0,0,0,0] for _ in range(C)] for _ in range(R)]
li = [0]*K
val = {}
pl = [0]*K
for i in range(K):
    r,c,v = inputlist()
    pl[i] = (r-1,c-1)
    val[(r-1,c-1)] = v
for i in range(K):
    dp[pl[i][0]][pl[i][1]][0] = val[pl[i]]

k = dp[0][0][0]
for i in range(1,4):
    dp[0][0][i] = k

tmp = dp[0][0][0]
for x in range(C):
    dp[0][x][1] = max(tmp,dp[0][x][0])
    if dp[0][x][0] > tmp:
        tmp = dp[0][x][0]
tmp = dp[0][0][0]
for y in range(R):
    dp[y][0][1] = max(tmp,dp[y][0][0])
    if dp[y][0][0] > tmp:
        tmp = dp[y][0][0]
for y in range(1,R):
    for x in range(1,C):
        dp[y][x][1] = max(dp[y-1][x][1],dp[y][x-1][1],dp[y][x][0])

for i in range(2,4):
    for x in range(1,C):
        dp[0][x][i] = max(dp[0][x-1][i],dp[0][x-1][i-1]+dp[0][x][0])
    for y in range(1,R):
        dp[y][0][i] = max(dp[y-1][0][i],dp[y-1][0][i-1]+dp[y][0][0])
    for y in range(1,R):
        for x in range(1,C):
            dp[y][x][i] = max(dp[y-1][x][i],dp[y][x-1][i],dp[y-1][x][i-1]+dp[y][x][0],dp[y][x-1][i-1]+dp[y][x][0])

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

Submission status: Wrong Answer

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

def smax(i,j):
    if i>j:
        return i
    else:
        return j

def smax2(L):
    return smax(smax(L[0],L[1]),smax(L[2],L[3]))

def main():
    R,C,K = list(map(int,input().split()))

    D = [[0]*C for i in range(R)]

    for i in range(K):
        r,c,v = list(map(int,input().split()))
        r,c = r-1,c-1
        D[r][c] = v
        
    DP = [[[0]*4 for j in range(C)] for i in range(R)]
    """
    for i in range(R):
        for j in range(C):
            if i!=0 and j!=0:
                DP[i][j][0] = smax(smax2(DP[i-1][j]), DP[i][j-1][0])
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], smax(DP[i][j-1][1],smax2(DP[i-1][j])+D[i][j]))
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            elif i!=0 and j==0:
                DP[i][j][0] = smax2(DP[i-1][j])
                DP[i][j][1] = smax2(DP[i-1][j])+D[i][j]
            elif i==0 and j!=0:
                DP[i][j][0] = DP[i][j-1][0]
                DP[i][j][1] = smax(DP[i][j-1][0] + D[i][j], DP[i][j-1][1])
                DP[i][j][2] = smax(DP[i][j-1][1] + D[i][j], DP[i][j-1][2])
                DP[i][j][3] = smax(DP[i][j-1][2] + D[i][j], DP[i][j-1][3])
            else:
                DP[i][j][0] = 0
                DP[i][j][1] = D[i][j]

    print(smax2(DP[R-1][C-1]))
    """
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

for i in range(R):
    for j in range(C):
        if i == 0 and j == 0:
            pass
        else:
            if i != 0 and j != 0:
                dp[i][j] += max(dp[i-1][j], dp[i][j-1])
            elif i != 0:
                dp[i][j] += dp[i-1][j]
            else:
                dp[i][j] += dp[i][j-1]
print(dp[R-1][C-1])

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
grid = [[0]*C for _ in range(R)]
for i in range(K):
    r, c, v= map(int, input().split())
    grid[r-1][c-1] = v
dp = [[[0]*4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for k in range(C):
        tmp = max(dp[i-1][k])
        dp[i][k][0] = max(dp[i][k-1][0],tmp)
        dp[i][k][1] = dp[i][k-1][1]
        dp[i][k][2] = dp[i][k-1][2]
        dp[i][k][3] = dp[i][k-1][3]
        if grid[i][k] != 0:
            dp[i][k][1] = max(dp[i][k-1][0]+grid[i][k],tmp+grid[i][k],dp[i][k][1])
            dp[i][k][2] = max(dp[i][k-1][1]+grid[i][k],dp[i][k][2])
            dp[i][k][3] = max(dp[i][k-1][2]+grid[i][k],dp[i][k][3])
print(max(dp[-1][-1]))

Submission status: Wrong Answer

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


def main():
    r, c, k = map(int, input().split())
    values = [[0] * c for _ in range(r)]
    for _ in range(k):
        y, x, v = map(int, input().split())
        values[y-1][x-1] = v

    DP0 = [[0]*(c+1) for _ in range(r+1)]
    DP1 = [[0]*(c+1) for _ in range(r+1)]
    DP2 = [[0]*(c+1) for _ in range(r+1)]
    DP3 = [[0]*(c+1) for _ in range(r+1)]

    for y in range(r):
        for x in range(c):
            value = values[y][x]
            for table1, table2 in zip([DP0, DP1, DP2], [DP1, DP2, DP3]):
                # そのまま右に
                if table1[y][x] > table1[y][x+1]:
                    table1[y][x+1] = table1[y][x]

                # 1個とって右に行く
                if table1[y][x] + value > table2[y][x+1]:
                    table2[y][x+1] = table1[y][x] + value

            # 下に行く
            DP0[y+1][x] = max(DP0[y][x+1], DP1[y][x+1], DP2[y][x+1], DP3[y][x+1])

    ans = max(max(DP0[r]), max(DP1[r]), max(DP2[r]), max(DP3[r]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(items,R,C):  # fix inputs here
    console("----- solving ------")
    console(R,C)

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

    for r,c,v in items:
        grid[r-1][c-1] = v

    # console(np.array(grid))  # PLEASE SILENCE
    console("\n\n")

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

    if grid[0][0] > 0:
        dp[0][0][1] = dp[0][0][0] + grid[0][0]

    for i in range(1,R):
        dp[i][0][0] = max(dp[i-1][0][0], dp[i-1][0][1])
        if grid[i][0] > 0:
            dp[i][0][1] = dp[i][0][0] + grid[i][0]

    for j in range(1,C):
        for k in range(1,4):
            dp[0][j][k] = max(dp[0][j-1][k], dp[0][j-1][k-1] + grid[0][j])

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE
    console("\n\n")

    for i in range(1,R):
        for j in range(1,C):
            dp[i][j][0] = max(max(dp[i-1][j][k] for k in range(4)), dp[i][j-1][0])
            if grid[i][j] > 0:
                dp[i][j][1] = dp[i][j][0] + grid[i][j]
                for k in range(2,4):
                    dp[i][j][k] = max(dp[i][j-1][k], dp[i][j-1][k-1] + grid[i][j])
            else:
                for k in range(1,4):
                    dp[i][j][k] = dp[i][j-1][k]

    # console(np.array(dp)[:,:,0])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,1])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,2])  # PLEASE SILENCE
    # console(np.array(dp)[:,:,3])  # PLEASE SILENCE
    # console(np.array(dp))  # PLEASE SILENCE

    # return a string (i.e. not a list or matrix)
    return max(dp[R-1][C-1][k] for k in range(4))


def console(*args):  # the judge will not read these print statement
    print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
# sys.stdin.readlines()

for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    r,c,nrows = list(map(int,input().split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    grid = []
    for _ in range(nrows):
        grid.append(list(map(int,input().split())))

    res = solve(grid,r,c)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)

Submission status: Wrong Answer

Solution code:
R, C, K = list(map(int,input().split()))
v = [[0 for _ in range(C)] for _ in range(R)]
for i in range(K):  
    in1, in2, in3 = list(map(int,input().split()))
    v[in1 - 1][in2 - 1] = in3
    
dp = [[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
dp[0][0][0] = 0
dp[0][0][1] = v[0][0]
dp[0][0][2] = v[0][0]
dp[0][0][3] = v[0][0]
m = [[0 for _ in range(C)] for _ in range(R)]

for i in range(1, R):
    dp[i][0][3] = max(dp[i - 1][0][3], dp[i - 1][0][2] + v[i][0])
    dp[i][0][2] = max(dp[i - 1][0][2], dp[i - 1][0][1] + v[i][0])
    dp[i][0][1] = max(dp[i - 1][0][1], dp[i - 1][0][0] + v[i][0])
    dp[i][0][0] = dp[i - 1][0][0]
    m[i][0] = max(dp[i][0])
    
for i in range(1, C):
    dp[0][i][3] = max(dp[0][i - 1][3], dp[0][i - 1][2] + v[0][i])
    dp[0][i][2] = max(dp[0][i - 1][2], dp[0][i - 1][1] + v[0][i])
    dp[0][i][1] = max(dp[0][i - 1][1], dp[0][i - 1][0] + v[0][i])
    dp[0][i][0] = dp[0][i - 1][0]
    m[0][i] = max(dp[0][i])

for i in range(1, R):
    for j in range(1, C):
        if v[i][j]:
            dp[i][j][3] = max(dp[i][j - 1][3], v[i][j] + dp[i][j - 1][2], v[i][j] + m[i - 1][j])
            dp[i][j][2] = max(dp[i][j - 1][2], v[i][j] + dp[i][j - 1][1], v[i][j] + m[i - 1][j])
            dp[i][j][1] = max(dp[i][j - 1][1], v[i][j] + dp[i][j - 1][0], v[i][j] + m[i - 1][j])
            dp[i][j][0] = max(dp[i][j - 1][0], m[i - 1][j])
        else:
            dp[i][j][3] = max(dp[i][j - 1][3], m[i - 1][j])
            dp[i][j][2] = max(dp[i][j - 1][2], m[i - 1][j])
            dp[i][j][1] = max(dp[i][j - 1][1], m[i - 1][j])
            dp[i][j][0] = max(dp[i][j - 1][0], m[i - 1][j])
        m[i][j] = max(dp[i][j])
ans = max(dp[R - 1][C - 1])
print(ans)