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())
V = [[]for i in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    V[r-1].append([c,v])
cost = [0]*(C+1)
for r in range(R):
    V[r].sort()
    hoge = [0,0,0,0]
    for c,v in V[r]:
        for i in range(3,0,-1):
            hoge[i] = max(cost[c],hoge[i-1])
            hoge[i] += v
        hoge[0] = cost[c]
        cost[c] = max(hoge)
    for i in range(1,C):
        cost[i] = max(cost[i],cost[i-1])

print(cost[-1])

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:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

inf=10**16
h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v
dp=[[[[-inf]*4 for _ in range(2)] for _ in range(w+1)] for _ in range(h+1)]
dp[0][0][0][0]=dp[0][0][1][0]=0
for i in range(h):
    for j in range(w):
        for d in range(2):
            for c in range(4):
                pre=dp[i][j][d][c]
                if pre==-inf:continue
                if d==0:
                    if i+1<=h:dp[i+1][j][d][c]=max(dp[i+1][j][d][c],pre)
                    if j+1<=w:dp[i][j+1][1][0]=max(dp[i][j+1][1][0],pre)
                else:
                    if i+1<=h:dp[i+1][j][0][0]=max(dp[i+1][j][0][0],pre)
                    if j+1<=w:dp[i][j+1][d][c]=max(dp[i][j+1][d][c],pre)
                if aa[i][j]:
                    if d==0:
                        if c<3 and i+1<=h:dp[i + 1][j][d][c] = max(dp[i + 1][j][d][c+1], pre+aa[i][j])
                        if c<3 and j+1<=w:dp[i][j+1][1][1] = max(dp[i][j+1][1][1], pre + aa[i][j])
                    else:
                        if c<3 and j+1<=w:dp[i][j+1][d][c] = max(dp[i][j+1][d][c+1], pre+aa[i][j])
                        if c<3 and i+1<=h:dp[i+1][j][0][1] = max(dp[i+1][j][0][1], pre + aa[i][j])
ans=-inf
for d in range(2):
    for c in range(4):
        ans=max(ans,dp[h-1][w][d][c])
        ans=max(ans,dp[h][w-1][d][c])
print(ans)

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

dp = [[[0] * 4 for _ in range(C)] for _ in range(R)]
for i in range(R):
    for j in range(C):
        if goods[i][j] == 0:
            if i < R - 1:
                dp[i+1][j][0] = max(dp[i+1][j][0], max(dp[i][j]))
            
            if j < C - 1:
                for k in range(4):
                    dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])

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

for i in range(3):
    dp[R-1][C-1][i] += goods[R-1][C-1]
print(max(dp[R-1][C-1]))

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])
            dp[c][r][0] = max(dp[c][r][0], dp[c][r - 1][0]) + items[c][r]
            #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
input = sys.stdin.readline

r, c, k = map(int, input().split())
a = [[0] * c for _ in range(r)]
dp = [[0] * (c + 1) for _ in range(r + 1)]
b = [[0] * (c+1) for _ in range(r+1)]
for i in range(k):
    x, y, v = map(int, input().split())
    a[x-1][y-1] = v

for j in range(c):
    f, s, t = 0, 0, 0
    for i in range(r):
        if a[i][j] >= f:
            t = s
            s = f
            f = a[i][j]
        elif a[i][j] >= s:
            t = s
            s = a[i][j]
        elif a[i][j] >= t:
            f = a[i][j]
        b[i + 1][j + 1] = t
        
        dp[i + 1][j + 1] = max(dp[i+1][j] + a[i][j], dp[i][j+1]  -b[i][j] + max(b[i][j+1], f))
        
print(dp[r][c])

Submission status: Wrong Answer

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

#print(M)

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

for i in range(R):
    for j in range(C):
        if i == 0 and j == 0:
            if M[i][j] != -1:
                dp[i+1][j][1] = M[i][j]
        if i != 0:
            dp[i+1][j][0] = max([dp[i][j][0], dp[i][j][1], dp[i][j][2]])
            for k in range(4):
                dp[i+1][j][k] = max(dp[i+1][j-1][k], dp[i+1][j][k])
                if k != 3:
                    if M[i][j] != -1:
                        dp[i+1][j][k+1] = max(dp[i+1][j-1][k]+M[i][j], dp[i+1][j][k+1])
        if j != 0:
            for k in range(4):
                dp[i+1][j][k] = max(dp[i+1][j-1][k], dp[i+1][j][k])
                if k != 3:
                    if M[i][j] != -1:
                        dp[i+1][j][k+1] = max(dp[i+1][j-1][k]+M[i][j], dp[i+1][j][k+1])

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)


def solve(R: int, C: int, N: int, r: "List[int]", c: "List[int]", v: "List[int]"):
    rr = sorted(list(set(r)))
    cc = sorted(list(set(c)))
    #print(rr, cc)
    rmap, cmap = {}, {}
    for i, vv in enumerate(rr):
        rmap[vv] = i
    for i, vv in enumerate(cc):
        cmap[vv] = i

    h, w = len(rr), len(cc)
    x = {}
    for i in range(N):
        rx, cx = rmap[r[i]], cmap[c[i]]
        if not rx in x:
            x[rx] = []
        x[rx].append((cx, v[i]))

    dp = [[0] * (w + 1) for _ in range(h + 1)]
    #print(x)
    for i in range(h):
        #for j in range(w):
        #    dp[i][j + 1] = max(dp[i][j + 1], dp[i][j])
        if not i in x:
            dp[i + 1] = dp[i]
            continue
        ww = x[i]
        ww.sort()
        for j in range(len(ww)):
            jj = ww[j][0]
            tmp = [0, 0, 0]
            s = 0
            for k in range(j, len(ww)):
                kk = ww[k][0]
                if tmp[-1] < ww[k][1]:
                    s += ww[k][1] - tmp[-1] 
                    tmp[-1] = ww[k][1]
                    tmp.sort(reverse=True)
                dp[i + 1][kk] = max(dp[i + 1][kk], dp[i][jj] + s)

        for j in range(w):
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j], dp[i][j + 1])


        #for jj in range(w):
        #    tmp = [0, 0, 0]
        #    s = 0
        #    for j in range(jj, w):
        #        if i in x and j in x[i]:
        #            if tmp[-1] < x[i][j]:
        #                s += x[i][j] - tmp[-1] 
        #                tmp[-1] = x[i][j]
        #                tmp.sort(reverse=True)
        #        dp[i + 1][j] = max(dp[i + 1][j], dp[i][jj] + s)

    #print(dp)
    ret = 0
    for v in dp[h]:
        ret = max(ret, v)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    R = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    r = [int()] * (K)  # type: "List[int]"
    c = [int()] * (K)  # type: "List[int]"
    v = [int()] * (K)  # type: "List[int]"
    for i in range(K):
        r[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens)) - 1
        v[i] = int(next(tokens))
    solve(R, C, K, r, c, v)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import *
import numpy as np
R,C,K = map(int,input().split())
I = sorted([list(map(int,input().split())) for k in range(K)])
S = np.zeros((R,C),np.int32)
A = []

for r,c,v in I:
  S[r-1][c-1] = v

for C in permutations((R-1)*"0"+(C-1)*"1",R+C-2):
  x = 0
  y = 0
  z = S[0][0]
  
  for c in C:
    if c=="0":
      x+=1
      z+=S[x][y]
    else:
      y+=1
      z+=S[x][y]
  
  A+=[z]

print(max(A))

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
RCV = [[0]*C for i in range(R)]
for i in range(K):
    rcv = list(map(int, input().split()))
    r = rcv[0]-1
    c = rcv[1]-1
    v = rcv[2]
    RCV[r][c] = v
# for i in RCV:
#     print(i)

dp = [[0]*C for i in range(R)]
for i in range(R):
    base_num = 0
    sum_lst = [0, 0, 0]
    for j in range(C):
        if [i, j] == [0, 0]:
            sum_lst.append(RCV[i][j])
            sum_lst.sort()
            sum_lst = sum_lst[::-1]
            del sum_lst[-1]
            right = base_num + sum(sum_lst)
            dp[i][j] = right
        else:
            sum_lst.append(RCV[i][j])
            sum_lst.sort()
            sum_lst = sum_lst[::-1]
            del sum_lst[-1]
            down = dp[i-1][j] + RCV[i][j]
            right = base_num + sum(sum_lst)
            if down < right:
                dp[i][j] = right
            else:
                dp[i][j] = down
                base_num = dp[i-1][j]
# for i in dp:
#     print(i)
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys

R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)]
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    item[r][c] = v

dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            here = dp[k][i][j]
            if i + 1 <= R:
                dp[0][i + 1][j] = max(dp[0][i + 1][j], here)
                if k < 3:
                    dp[1][i + 1][j] = max(dp[1][i + 1][j], here + item[i + 1][j])
            if j + 1 <= C:
                dp[k][i][j + 1] = max(dp[k][i][j + 1], here)
                if k < 3:
                    dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], here + item[i][j + 1])
ans = 0
for k in range(4):
    ans = max(ans, dp[k][-1][-1])
print(ans)

Submission status: Wrong Answer

Solution code:
# by the authority of GOD     author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase
from heapq import *

def main():
    R,C,K = map(int,input().split())
    arr = [[0]*C for _ in range(R)]
    for _ in range(K):
        r,c,v = map(int,input().split())
        arr[r-1][c-1] = v
    dp = [[0]*C for _ in range(R)]
    for i in range(R):
        x = []
        heapify(x)
        carry = 0
        for j in range(C):
            if i and j and dp[i-1][j] > dp[i][j-1]:
                x = []
                heapify(x)
                carry = dp[i-1][j]
            if len(x) == 3:
                y = heappop(x)
                heappush(x,max(y,arr[i][j]))
            else:
                heappush(x,arr[i][j])
            dp[i][j] = sum(x)+carry
    print(dp[-1][-1])

#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys; input = sys.stdin.buffer.readline
INF = float("inf")

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	R, C, K = getlist()
	items = [[0] * C for i in range(R)]
	for i in range(K):
		r, c, v = getlist()
		r -= 1; c -= 1
		items[r][c] = v

	DP = [[[-INF] * 4 for i in range(C)] for j in range(R)]
	# 初期
	if items[0][0] != 0:
		DP[0][0][0] = 0
		DP[0][0][1] = items[0][0]

	for r in range(1, R):
		DP[r][0][0] = max(DP[r - 1][0][0], DP[r - 1][0][1])
		DP[r][0][1] = max(DP[r - 1][0][1], DP[r - 1][0][0]) + items[r][0]

	for c in range(1, C):
		DP[0][c][0] = DP[0][c - 1][0]
		for itr in range(1, 4):
			DP[0][c][itr] = max(DP[0][c - 1][itr - 1] + items[0][c], DP[0][c - 1][itr])

	for r in range(1, R):
		for c in range(1, C):
			prev = max(DP[r - 1][c])
			val = items[r][c]
			DP[r][c][0] = max(DP[r][c - 1][0], prev)
			DP[r][c][1] = max(max(DP[r][c - 1][0], prev) + val, DP[r][c - 1][1])
			for itr in range(2, 4):
				DP[r][c][itr] =  max(DP[r][c - 1][itr - 1] + val, DP[r][c - 1][itr])

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


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
R,C,K = map(int,input().split())
val = [[0]*C for _ in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    r,c = r-1,c-1
    val[r][c] = v
dp0 = [[0]*C for _ in range(R)]
dp1 = [[0]*C for _ in range(R)]
dp2 = [[0]*C for _ in range(R)]
dp3 = [[0]*C for _ in range(R)]
if val[0][0] != 0:
    dp1[0][0] = val[0][0]
for i in range(1,C):
    if val[0][i] == 0:
        dp1[0][i] = dp1[0][i-1]
        dp2[0][i] = dp2[0][i-1]
        dp3[0][i] = dp3[0][i-1]
    else:
        dp1[0][i] = max(dp0[0][i-1]+val[0][i],dp1[0][i-1])
        dp2[0][i] = max(dp1[0][i-1]+val[0][i],dp2[0][i-1])
        dp3[0][i] = max(dp2[0][i-1]+val[0][i],dp3[0][i-1])

for i in range(1,R):
    for j in range(C):
        dp0[i][j] = max([dp1[i-1][j],dp2[i-1][j],dp3[i-1][j]])
    for j in range(C):
        if j == 0:
            if val[i][j] != 0:
                dp1[i][j] = val[i][j]
        else:
            if val[i][j] != 0:
                dp1[i][j] = max(dp1[i][j-1],dp0[i][j]+val[i][j])
            else:
                dp1[i][j] = max(dp1[i][j-1],dp0[i][j])
    for j in range(C):
        if j == 0:
            continue
        if j == 1:
            if val[i][j] != 0 and val[i][j-1] != 0:
                dp2[i][j] = val[i][j] + val[i][j-1]
        else:
            if val[i][j] != 0:
                dp2[i][j] = max(dp2[i][j-1],dp1[i][j-1]+val[i][j])
            else:
                dp2[i][j] = dp2[i][j-1]
    for j in range(C):
        if j == 0 or j == 1:
            continue
        if j == 2:
            if val[i][j] != 0 and val[i][j-1] != 0 and val[i][j-2] != 0:
                dp3[i][j] = val[i][j] + val[i][j-1] + val[i][j-2]
        else:
            if val[i][j] != 0:
                dp3[i][j] = max(dp3[i][j-1],dp2[i][j-1]+val[i][j])
            else:
                dp3[i][j] = dp3[i][j-1]
ans = max(dp1[R-1][C-1],dp2[R-1][C-1],dp3[R-1][C-1])
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

r, c, k = map(int, input().split())
ans = np.array([[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:
R,C,K=map(int,input().split())
rcv = [[0]*C for _ in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    rcv[r-1][c-1]=v

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

if rcv[0][0]>0:
    dp[0][0][0]=0
    dp[1][0][0]=rcv[0][0]
    dp[2][0][0]=rcv[0][0]
    dp[3][0][0]=rcv[0][0]

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

i=0
for j in range(1,C):
    dp[0][i][j]=dp[0][i][j-1]
    for r in range(1,4):
        dp[r][i][j]=max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j])

for i in range(1,R):
    for j in range(1,C):
        dp[0][i][j]=max(dp[0][i-1][j],dp[0][i-1][j])
        for r in range(1,4):
            dp[r][i][j]=max(dp[r][i-1][j], dp[r-1][i-1][j]+rcv[i][j],
                           dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j])
print(max( dp[i][-1][-1] for i in range(0,4)) )

Submission status: Wrong Answer

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

Item=[["*"]*(C+2)]+[["*"]+[0]*C+["*"] for _ in range(R)]+[["*"]*(C+2)]

for i in range(K):
    x,y,v=map(int,input().split())
    Item[x][y]=v

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

for x in range(1,C+1):
    for y in range(1,R+1):
        if x==y==1:
            DP[y][x][1]=Item[1][1]
        elif y==1:
            for k in range(3):
                DP[y][x][k+1]=max(DP[y][x-1][k]+Item[y][x],DP[y][x-1][k])

            for k in range(4):
                DP[y][x][k]=max(DP[y][x][k],DP[y][x-1][k])
        elif x==1:
            for k in range(4):
                DP[y][x][0]=max(DP[y][x][0],DP[y-1][x][k])
                DP[y][x][1]=max(DP[y][x][0],DP[y-1][x][k]+Item[y][x])
        else:
            for k in range(3):
                DP[y][x][k+1]=max(DP[y][x-1][k]+Item[y][x],DP[y][x-1][k])

            for k in range(4):
                DP[y][x][0]=max(DP[y][x][0],DP[y-1][x][k]+Item[y][x])
                DP[y][x][k]=max(DP[y][x][k],DP[y][x-1][k])
assert 1
print(max(DP[-1][-1]))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
import copy
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)

r, c, t = map(int, input().split())
A = [list(map(int, input().split())) for i in range(t)]
p = dict()
for i in range(t):
    p[(A[i][0], A[i][1])] = A[i][2]

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

for i in range(1, r+1):
    for j in range(1, c+1):
        if (i, j) == (0, 0):
            continue
        dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2],
                          dp[i][j-1][0], dp[i][j-1][4], dp[i][j-1][5])
        dp[i][j][1] = dp[i][j-1][1]
        if (i, j-1) in p:
            tmp = max(dp[i][j-1][0], dp[i][j-1][4], dp[i][j-1][5]) + p[(i, j-1)]
            dp[i][j][1] = max(dp[i][j][1], tmp)
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + p[(i, j - 1)] if ((i, j - 1) in p and dp[i][j - 1][1] > 0) else -1)
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + p[(i, j - 1)] if ((i, j - 1) in p and dp[i][j - 1][2] > 0) else -1)

        dp[i][j][4] = dp[i-1][j][4]
        if (i-1, j) in p:
            tmp = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2]) + p[(i-1, j)]
            dp[i][j][4] = max(dp[i][j][4], tmp)
        # dp[i][j][4] = max(dp[i-1][j][4], dp[i-1][j][0] + p[(i-1, j)] if (i-1, j) in p else -1)
        dp[i][j][5] = max(dp[i-1][j][5], dp[i-1][j][4] + p[(i-1, j)] if ((i-1, j) in p and dp[i-1][j][4] > 0) else -1)
        dp[i][j][6] = max(dp[i-1][j][6], dp[i-1][j][5] + p[(i-1, j)] if ((i-1, j) in p and dp[i-1][j][5] > 0) else -1)

        # for k in range(7):
            # dp[i][j][k] = max(dp[i][j-1][k], dp[i-1][j][k])
            # if k in [1,2,3,5,6,7,9,10,11,13,14,15] and (i, j-1) in p and (k == 1 or dp[i][j-1][k-1] != 0):
            #     dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + p[(i, j-1)])
            # if k >= 4 and (i-1, j) in p and (k == 4 or dp[i-1][j][k-4] != 0):
            #     dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-4] + p[(i-1, j)])

# for i in range(1, r+1):
#     for j in range(1, c+1):
#         print(dp[i][j])

if (r, c) not in p:
    print(max(dp[-1][-1]))
    exit()

vmax = 0
for k in range(7):
    if k in [0,1,2,4,5]:
        vmax = max(vmax, dp[-1][-1][k]+p[(r, c)])
    else:
        vmax = max(vmax, dp[-1][-1][k])

print(vmax)

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
r = [0] + [0] * K
c = [0] + [0] * K
v = [0] + [0] * K
goods = [[0] * (C + 1) for i in range(R + 1)]
for i in range(1, K + 1):
    _r, _c, _v = map(int, input().split())
    r[i], c[i], v[i] = _r, _c, _v
    goods[_r][_c] = _v
dp = [[[0] * 4 for i in range(C + 1)] for j in range(R + 1)]
for i in range(1, R + 1):
    for j in range(1, C + 1):
        for k in range(1, 4):
            if goods[i][j] != 0:
                dp[i][j][k] = max(dp[i-1][j][0], dp[i-1][j][1], dp[i-1][j][2], dp[i-1][j][3], dp[i][j-1][k-1])
                dp[i][j][k] += goods[i][j]
            else:
                dp[i][j][k] = max(dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3],
                                  dp[i][j - 1][k])

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

Submission status: Wrong Answer

Solution code:
R, C, K = map(int, input().split())
V = [[[] for _ in range(C)] for _ in range(R)]
for _ in range(K):
  r, c, v = map(int, input().split())
  V[r-1][c-1].append(v)
M = [[[0, 0, 0, 0] for _ in range(C)] for _ in range(R)]
# M[r][c][count in column (c)]
for r in range(R):
  for c in range(C):
    V[r][c].sort(reverse=True)
    #candidates = [0 for _ in range(4)]
    #print(candidates)
    for n_pick in range(1 + min(3, len(V[r][c]))):
      s = sum(V[r][c][:n_pick])
      M[r][c][n_pick]=max(M[r][c][n_pick],s)
      if r > 0:
        for n_prev in range(4):
          #print('R++', r, c, n_pick, n_prev)
          if M[r-1][c][n_prev] > 0 or n_prev == 0:
            M[r][c][n_pick]=max(M[r][c][n_pick],
              M[r-1][c][n_prev] + s)
      if c > 0:
        for n_prev in range(4 - n_pick):
          #print('C++', r, c, n_pick, n_prev)
          if  M[r][c-1][n_prev]  > 0 or n_prev == 0:
            M[r][c][n_prev+n_pick]=max(M[r][c][n_pick],
              M[r][c-1][n_prev] + s)
    #print(candidates)
    #print(r, c, candidates, M)
    #for i in range(4):
    #  M[r][c][i] = max(candidates[i])
    #print(r, c, M)
#print(V, M)
print(max(M[R-1][C-1]))

Submission status: Wrong Answer

Solution code:
r,c,k = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(k)]
s = list( [0 for _ in range(c+1)] for _ in range(r+1))
for r1 , c1, v1 in l:
    s[r1][c1]  = v1

dp = [list( [0 for _ in range(5)] for _ in range(c+1)) for _ in range(r+1)]
"""
for i in range(r):
    for q in range(1):
        a = dp[i][1][q+1]
        b = dp[i][1][q] + s[i][1]
        dp[i][1][q+1] = max(a,b)

for j in range(c):
    for q in range(1):
        a = dp[1][j][q+1]
        b = dp[1][j][q] + s[1][j]
        dp[1][j][q+1] = max(a,b)
"""

for i in range(r):
    for j in range(c):
        for q in range(4):
            a = dp[i+1][j][q+1]
            b = dp[i+1][j][q] + s[i+1][j]
            e = dp[i][j+1][q+1]
            d = dp[i][j+1][q] +s[i][j+1]
            dp[i+1][j+1][q+1] = max(a,b,e,d)
print(max(dp[-1][-1]))