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:
def main():
    R, C, K = map(int, input().split())
    l = {}
    for _ in range(K):
        r, c, v = map(int, input().split())
        l[(r, c)] = 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 i in range(1, R+1):
        for j in range(1, C+1):
            dp0[i][j] = max(dp0[i-1][j], dp0[i][j-1], dp1[i][j-1], dp2[i][j-1], dp3[i][j-1])
            if (i, j) in l:
                dp1[i][j] = l[(i, j)] + max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j], dp0[i][j-1])
                dp2[i][j] = l[(i, j)] + dp1[i][j-1]
                dp3[i][j] = l[(i, j)] + dp2[i][j-1]
    return max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])

print(main())

Submission status: Wrong Answer

Solution code:
r,c,k = map(int,input().split())
v = [0 for _ in range(r*c)]
for _ in range(k):
  ri,ci,vi = map(int,input().split())
  v[c*(ri-1)+ci-1] = vi
a = [[0,0,0,0] for _ in range(r*c)]
if v[0]:
  a[0][1] = v[0]
for j in range(1,c):
  if v[j]:
    a[j][1] = max(a[j-1][1],v[j])
    if a[j-1][1]:
      a[j][2] = max(a[j-1][1]+v[j],a[j-1][2])
    if a[j-1][2]:
      a[j][3] = max(a[j-1][2]+v[j],a[j-1][3])
  else:
    a[j] = a[j-1]
for i in range(1,r):
  mx = max(a[(i-1)*c])
  a[i*c][0] = mx
  if v[i*c]:
    a[i*c][1] = mx+v[i*c]  
  for j in range(1,c):
    mx = max(a[(i-1)*c+j])
    if v[i*c+j]:
      a[i*c+j][1]= max(mx+v[i*c+j],a[i*c+j-1][1])
      if a[i*c+j-1][1]:
        a[i*c+j][2]= max(a[i*c+j-1][1]+v[i*c+j],a[i*c+j-1][2])
      if a[i*c+j-1][2]:
        a[i*c+j][3]= max(a[i*c+j-1][2]+v[i*c+j],a[i*c+j-1][3])
    else:
      a[i*c+j] = a[i*c+j-1]
    a[i*c+j][0] = mx
print(max(a[r*c-1]))

Submission status: Wrong Answer

Solution code:
import sys

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

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

print(grid)

ans=0
stk=[(0,0,0,3)]
while stk:
    i,j,tt,k=stk.pop()
    ntt,nk=tt,k
    if k>0:
        ntt+=grid[i][j]
        nk=k-1
    if i+1==R and j+1==C:
        ans=max(ans,ntt)
        continue
    for ni,nj in (i,j+1),(i+1,j):
        if not (ni<R and nj<C):
            continue
        if ni!=i:
            k=nk=3
        stk.append((ni,nj,tt,k))
        stk.append((ni,nj,ntt,nk))


print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    R, C, K = map(int, input().split())
    l = {}
    for _ in range(K):
        r, c, v = map(int, input().split())
        l[(r, c)] = 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 i in range(1, R+1):
        for j in range(1, C+1):
            dp0[i][j] = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j], dp0[i][j-1], dp1[i][j-1], dp2[i][j-1], dp3[i][j-1])
            if (i, j) in l:
                dp1[i][j] = l[(i, j)] + max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j], dp0[i][j-1])
                dp2[i][j] = l[(i, j)] + dp1[i][j-1]
                dp3[i][j] = l[(i, j)] + dp2[i][j-1]
    return max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])

print(main())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W, K = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(K)]
dp = [0] * (H * W * 4)
table = [0] * (H * W)
for i, j, v in a: table[(i - 1) * W + j - 1] = v
for i in range(H):
  for j in range(W):
    x = i * W + j
    for k in range(4):
      if i + 1 < H:
        y = (i + 1) * W + j
        dp[y * 4 + 0] = max(dp[y * 4 + 0], dp[x * 4 + k])
      if j + 1 < W:
        y = i * W + j + 1
        dp[y * 4 + k] = max(dp[y * 4 + k], dp[x * 4 + k])
      if k + 1 <= 3:
        if i + 1 < H:
          y = (i + 1) * W + j
          dp[y * 4 + 0] = max(dp[y * 4 + 0], dp[x * 4 + k] + table[x])
        if j + 1 < W:
          y = i * W + j + 1
          dp[y * 4 + k + 1] = max(dp[y * 4 + k + 1], dp[x * 4 + k] + table[x])
for k in range(3): dp[(H * W - 1) * 4 + k] += table[-1]
print(max(dp[-4: -1]))
#print(dp, table)

Submission status: Wrong Answer

Solution code:
r,c,k = map(int,input().split())
lst = [[0]*c for _ in range(r)]
for i in range(k):
    a,b,v = map(int,input().split())
    lst[a-1][b-1] = v
dp = [[[0]*4 for i in range(c)] for j in range(r)]
dp[0][0][1] = lst[0][0]
for i in range(r):
    for j in range(c):
        x,y = i+1,j
        if x < r:
            dp[x][y][0] = max(dp[x][y][0], max(dp[i][j])+lst[x][y])
        x,y = i,j+1
        if y < c:
            dp[x][y][3] = max(dp[x][y][3], dp[i][j][3], dp[i][j][2]+lst[x][y])
            dp[x][y][2] = max(dp[x][y][2], dp[i][j][2], dp[i][j][1]+lst[x][y])
            dp[x][y][1] = max(dp[x][y][1], dp[i][j][1], dp[i][j][0]+lst[x][y])
            dp[x][y][0] = max(dp[x][y][0], dp[i][j][0])
print(max(dp[-1][-1]))

Submission status: Wrong Answer

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

cost = [[[0]*4 for _ in range(C)] for _ in range(R)]
for x in range(R):
    for y in range(C):
        for z in range(4):
            if z == 0:
                cost[x][y][z] = max(cost[x][y-1][z],max(cost[x-1][y])) 
            else:
                cost[x][y][z] = max(cost[x][y-1][z],max(cost[x-1][y]),cost[x][y-1][z-1]+M[x][y],max(cost[x-1][y])+M[x][y])

print(cost[-1][-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
R,C,K = map(int, input().split())
data = [[0 for i in range(C)] for j in range(R)]
for i in range(K):
    r,c,v = map(int, input().split())
    data[r-1][c-1] = v

ans = [[[-1,-1,-1,-1] for i in range(C)] for j in range(R)]

que = deque()
if data[0][0]:
    ans[0][0][1] = data[0][0]
    que.append([0,0,1])

ans[0][0][0] = 0
que.append([0,0,0])
while que:
    i,j,now = que.popleft()
    if i < R-1:
        if data[i+1][j]:
            if ans[i+1][j][1] == -1:
                ans[i + 1][j][1] = ans[i][j][now]+data[i+1][j]
                que.append([i+1,j,1])
            else:
                ans[i+1][j][1] = max(ans[i+1][j][1],ans[i][j][now]+data[i+1][j])
        if ans[i+1][j][0] == -1:
            ans[i + 1][j][0] = ans[i][j][now]
            que.append([i+1,j,0])
        ans[i+1][j][0] = max(ans[i+1][j][0], ans[i][j][0])
    if j < C-1:
        if data[i][j+1] and now <= 2:
            if ans[i][j+1][now+1] == -1:
                ans[i][j+1][now+1] = ans[i][j][now] + data[i][j+1]
                que.append([i,j+1,now+1])
            else:
                ans[i][j + 1][now + 1] = max(ans[i][j + 1][now + 1],ans[i][j][now] + data[i][j + 1])

        if ans[i][j+1][now] == -1:
            ans[i][j+1][now] = ans[i][j][now]
            que.append([i,j+1,now])
        else:
            ans[i][j+1][now] = max(ans[i][j+1][now],ans[i][j][now])

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

Submission status: Wrong Answer

Solution code:
from collections import deque
R,C,K = map(int, input().split())
data = [[0 for i in range(C)] for j in range(R)]
for i in range(K):
    r,c,v = map(int, input().split())
    data[r-1][c-1] = v

ans = [[[-1,-1,-1,-1] for i in range(C)] for j in range(R)]

que = deque()
if data[0][0]:
    ans[0][0][1] = data[0][0]
    que.append([0,0,1])
else:
    ans[0][0][0] = 0
    que.append([0,0,0])
while que:
    i,j,now = que.popleft()
    if i < R-1:
        if data[i+1][j]:
            if ans[i+1][j][1] == -1:
                ans[i + 1][j][1] = ans[i][j][now]+data[i+1][j]
                que.append([i+1,j,1])
            else:
                ans[i+1][j][1] = max(ans[i+1][j][1],ans[i][j][now]+data[i+1][j])
        if ans[i+1][j][0] == -1:
            ans[i + 1][j][0] = ans[i][j][now]
            que.append([i+1,j,0])
        ans[i+1][j][0] = max(ans[i+1][j][0], ans[i][j][0])
    if j < C-1:
        if data[i][j+1] and now <= 2:
            if ans[i][j+1][now+1] == -1:
                ans[i][j+1][now+1] = ans[i][j][now] + data[i][j+1]
                que.append([i,j+1,now+1])
            else:
                ans[i][j + 1][now + 1] = max(ans[i][j + 1][now + 1],ans[i][j][now] + data[i][j + 1])

        if ans[i][j+1][now] == -1:
            ans[i][j+1][now] = ans[i][j][now]
            que.append([i,j+1,now])
        else:
            ans[i][j+1][now] = max(ans[i][j+1][now],ans[i][j][now])

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

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def getval():
    r,c,k = map(int,input().split())
    item = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        ri,ci,vi = map(int,input().split())
        item[ri-1][ci-1] = vi
    return r,c,k,item

def main(r,c,k,item):
    #DP containing the information of max value given k items picked in i,j
    prev = [[0 for i in range(4)]]
    inititem = item[0][0]
    prev[0] = [0,inititem,inititem,inititem]
    for i in range(1,r):
        initval = prev[i-1][3]
        temp = []
        cur = item[i][0]
        for j in range(4):
            temp.append(initval)
        for j in range(1,4):
            temp[j] += cur
        prev.append(temp)

    for j in range(1,c):
        init = deepcopy(prev[0])
        for i in range(1,4):
            init[i] = prev[0][i]
            if prev[0][i-1]+item[0][j]>init[i]:
                init[i] = prev[0][i-1]+item[0][j]
        curcol = [init]
        for i in range(1,r):
            cur = item[i][j]
            left = curcol[-1][3]
            down = prev[i]
            temp = [left]
            if down[0]>temp[0]:
                temp[0] = down[0]
            for k in range(1,4):
                c = down[k]
                if left+cur>c:
                    c = left+cur 
                if down[k-1]+cur>c:
                    c = down[k-1]+cur
                temp.append(c)
            curcol.append(temp)
        prev = deepcopy(curcol)
            
    print(max(prev[-1]))
    #print(prev)
           
if __name__=="__main__":
    r,c,k,item = getval()
    main(r,c,k,item)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def getval():
    r,c,k = map(int,input().split())
    item = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        ri,ci,vi = map(int,input().split())
        item[ri-1][ci-1] = vi
    return r,c,k,item

def main(r,c,k,item):
    #DP containing the information of max value given k items picked in i,j
    prev = [[0 for i in range(4)]]
    inititem = item[0][0]
    prev[0] = [0,inititem,inititem,inititem]
    for i in range(1,r):
        initval = prev[i-1][3]
        temp = []
        cur = item[i][0]
        for j in range(4):
            temp.append(initval)
        for j in range(1,4):
            temp[j] += cur
        prev.append(temp)

    for j in range(1,c):
        init = deepcopy(prev[0])
        for i in range(1,4):
            init[i] = max(prev[0][i],prev[0][i-1]+item[0][j])
        curcol = [init]
        for i in range(1,r):
            cur = item[i][j]
            left = curcol[-1]
            down = prev[i]
            temp = [max(left[3],down[0])]
            for k in range(1,4):
                temp.append(max(max(left[3],down[k-1])+cur,down[k]))
            curcol.append(temp)
        prev = deepcopy(curcol)
            
    print(max(prev[-1]))
           
if __name__=="__main__":
    r,c,k,item = getval()
    main(r,c,k,item)

Submission status: Time Limit Exceeded

Solution code:
def getval():
    r,c,k = map(int,input().split())
    item = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        ri,ci,vi = map(int,input().split())
        item[ri-1][ci-1] = vi
    return r,c,k,item

def main(r,c,k,item):
    #DP containing the information of max value given k items picked in i,j
    dp = [[[0,0,0,0] for i in range(c)] for i in range(r)]
    inititem = item[0][0]
    dp[0][0] = [0,inititem,inititem,inititem]
    for i in range(1,r):
        cur = item[i][0]
        init = dp[i-1][0][3]
        for j in range(4):
            if j:
                dp[i][0][j] += init + cur
            else:
                dp[i][0][j] += init

    for i in range(1,c):
        cur = item[0][i]
        prev = dp[0][i-1]
        for j in range(1,4):
            dp[0][i][j] = max(prev[j-1]+cur,prev[j])

    for i in range(1,r):
        for j in range(1,c):
            left = dp[i-1][j]
            down = dp[i][j-1]
            cur = item[i][j]
            dp[i][j][0] = max(left[3],down[0])
            for k in range(1,4):
                dp[i][j][k] = max(max(left[3],down[k-1])+cur,down[k])

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

if __name__=="__main__":
    r,c,k,item = getval()
    main(r,c,k,item)

Submission status: Time Limit Exceeded

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

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

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

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

dp[0][0][0] = 0
dp[0][0][1] = values[0][0]

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

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

Submission status: Time Limit Exceeded

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

r, c, k = map(int, input().split())
v = [[0]*(c+1) for i in range(r+1)]
for i in range(k):
    cr, cc, cv = map(int, input().split())
    v[cr][cc] = cv

dp = [[0]*4 for _ in range(c+1)]
last = [[0]*4 for _ in range(c+1)]
for cr in range(1, r+1):
    for cc in range(1, c+1):
        upper = max(last[cc][0], last[cc][1], last[cc][2], last[cc][3])
        dp[cc][0] = max(upper, dp[cc-1][0], dp[cc][0])
        dp[cc][1] = max(dp[cc-1][1], dp[cc-1][0]+v[cr][cc], upper+v[cr][cc])
        dp[cc][2] = max(dp[cc-1][2], dp[cc-1][1]+v[cr][cc])
        dp[cc][3] = max(dp[cc-1][3], dp[cc-1][2]+v[cr][cc])
        last = dp
print(max(dp[c]))

Submission status: Time Limit Exceeded

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

r, c, k = map(int, input().split())
v = [[0]*(c+1) for i in range(r+1)]
for i in range(k):
    cr, cc, cv = map(int, input().split())
    v[cr][cc] = cv

dp = [[[0]*4 for _ in range(c+1)] for i in range(r+1)]
for cr in range(1, r+1):
    for cc in range(1, c+1):
        upper = max(dp[cr-1][cc][0], dp[cr-1][cc][1], dp[cr-1][cc][2], dp[cr-1][cc][3])
        
        dp[cr][cc][0] = max(upper, dp[cr][cc-1][0], dp[cr][cc][0])
        dp[cr][cc][1] = max(dp[cr][cc-1][1], dp[cr][cc-1][0]+v[cr][cc], upper+v[cr][cc])
        dp[cr][cc][2] = max(dp[cr][cc-1][2], dp[cr][cc-1][1]+v[cr][cc])
        dp[cr][cc][3] = max(dp[cr][cc-1][3], dp[cr][cc-1][2]+v[cr][cc])
print(max(dp[r][c]))

Submission status: Time Limit Exceeded

Solution code:
r, c, k = map(int, input().split())
v = [[0]*(c+1) for i in range(r+1)]
for i in range(k):
    cr, cc, cv = map(int, input().split())
    v[cr][cc] = cv

dp = [[[0]*4 for _ in range(c+1)] for i in range(r+1)]
for cr in range(1, r+1):
    for cc in range(1, c+1):
        for cn in range(4):
            upper = max(dp[cr-1][cc][0], dp[cr-1][cc][1], dp[cr-1][cc][2], dp[cr-1][cc][3])
            if cn == 0:
                # print(cr, cc, upper)
                dp[cr][cc][cn] = max(upper, dp[cr][cc-1][cn], dp[cr][cc][cn])
            elif cn == 1:
                dp[cr][cc][cn] = max(dp[cr][cc-1][cn], dp[cr][cc-1][cn-1]+v[cr][cc], upper++v[cr][cc])
            else:
                dp[cr][cc][cn] = max(dp[cr][cc-1][cn], dp[cr][cc-1][cn-1]+v[cr][cc])

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

Submission status: Time Limit Exceeded

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

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

max_p = [0]*(C+1)
for i in range(R+1):
    now0 = [0]*(C+1)
    now1 = [0]*(C+1)
    now2 = [0]*(C+1)
    now3 = [0]*(C+1)
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            now0[j] = max(now0[j], max_p[j], la)
            now1[j] = max(now1[j], max_p[j] + v, la + v, lb)
        max_p[j] = max(now0[j], now1[j], now2[j], now3[j])

print(max(now0[C], now1[C], now2[C], now3[C]))

Submission status: Time Limit Exceeded

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

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

pre0 = [0]*(C+1)
pre1 = [0]*(C+1)
pre2 = [0]*(C+1)
pre3 = [0]*(C+1)
for i in range(R+1):
    now0 = [0]*(C+1)
    now1 = [0]*(C+1)
    now2 = [0]*(C+1)
    now3 = [0]*(C+1)
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            max_p = max(pre0[j], pre1[j], pre2[j], pre3[j])
            now0[j] = max(now0[j], max_p, la)
            now1[j] = max(now1[j], max_p + v, la + v, lb)
    pre0 = now0
    pre1 = now1
    pre2 = now2
    pre3 = now3

print(max(now0[C], now1[C], now2[C], now3[C]))

Submission status: Time Limit Exceeded

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

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

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]
            dp2[i][j] = max(dp2[i][j], lc, lb + v)
            dp3[i][j] = max(dp3[i][j], ld, lc + v)

        if i > 0:
            max_p = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])
            dp0[i][j] = max(dp0[i][j], max_p, la)
            dp1[i][j] = max(dp0[i][j], max_p + v, lb, la + v)

print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))

Submission status: Time Limit Exceeded

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

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

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d

print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))

Submission status: Time Limit Exceeded

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

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

pre = [[0]*(3+1) for _ in range(C+1)]
for i in range(R+1):
    now = [[0]*(3+1) for _ in range(C+1)]
    for j in range(C+1):
        for k in range(3+1):
            if i-1 >= 0:
                now[j][0] = max(now[j][0], pre[j][k])
                now[j][1] = max(now[j][1], pre[j][k] + V[i][j])

            if j-1 >= 0:
                now[j][k] = max(now[j][k], now[j-1][k])
                if k-1 >= 0:
                    now[j][k] = max(now[j][k], now[j-1][k-1] + V[i][j])
    pre = now

print(max(now[C]))

Submission status: Time Limit Exceeded

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

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

# print(*V,sep='\n',end='\n\n')

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

# for r in dp:
#     for c in r:
#         print('[',end='')
#         for x in c:
#             print(x,end=' ')
#         print(']',end='')
#     print()
# print()

for i in range(R+1):
    for j in range(C+1):
        for k in range(3+1):
            if i-1 >= 0:
                dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][k])
                dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][k] + V[i][j])

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

# for r in dp:
#     for c in r:
#         print('[',end='')
#         for x in c:
#             print(x,end=' ')
#         print(']',end='')
#     print()
# print()

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

Submission status: Time Limit Exceeded

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

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

# print(*mesh,sep='\n',end='\n\n')

def big3(d): return sorted(d, reverse=True)[:3]

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

for i in range(1, R+1):
    for j in range(1, C+1):
        b3 = []
        for k in range(j, C+1):
            b3 = big3(b3 + [mesh[i][k]])
            # print('{} [{}:{}] b3 {} s3 {}'.format(i,j,k,b3,sum(b3)))
            dp[i][k] = max(\
                dp[i][k],\
                dp[i-1][j] + sum(b3),\
                dp[i-1][k] + mesh[i][k]\
                )
        # print('-----')
    # print('{} ['.format(i), end='')
    # for x in dp[i]: print('{:>2} '.format(x),end='')
    # print(']')
# print('===')
print(dp[R][C])

Submission status: Time Limit Exceeded

Solution code:
import sys
# print(sys.getrecursionlimit())
sys.setrecursionlimit(3000000)
# print(sys.getrecursionlimit())

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

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

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

# print(*mesh,sep='\n')

def big3(d):
    return sorted(d, reverse=True)[:3]

def dfs(i, j, k, s):
    # print('i {} j {} k {} s {}'.format(i,j,k,s))

    if i == R-1 and j == C-1:
        r = big3(k + [mesh[i][j]])
        # print('r {}'.format(r))
        return s + sum(r)

    # if i == R or j == C: return val

    # nx_k = k + [mesh[i][j]] if mesh[i][j] > 0 else k
    nx_k = big3(k + [mesh[i][j]])

    val = 0
    if j+1 <= C-1:
        val = dfs(i, j+1, nx_k, s)

    if i+1 <= R-1:
        pre = sum(nx_k)
        v = dfs(i+1, j, [], s + pre)
        val = max(val, v)
        # print('pre {} v {}'.format(pre,v))

    return val

# ans = dfs(0, 0, 1 if mesh[0][0] > 0 else 0)
ans = dfs(0, 0, [], 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
value=[[0]*(C+1) for i in range(R+1)]
for i in range(K):
  r,c,v=map(int,input().split())
  value[r][c]=v
DP0=[[0]*(C+1) for i in range(R+1)]
DP1=[[0]*(C+1) for i in range(R+1)]
DP2=[[0]*(C+1) for i in range(R+1)]
DP3=[[0]*(C+1) for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    v=value[i][j]
    DP0[i][j]=max(DP0[i][j-1],DP1[i-1][j],DP2[i-1][j],DP3[i-1][j])
    DP1[i][j]=max(DP0[i-1][j]+v,DP1[i-1][j]+v,DP2[i-1][j]+v,
                  DP3[i-1][j]+v,DP1[i][j-1],DP0[i][j-1]+v)
    DP2[i][j]=max(DP1[i][j-1]+v,DP2[i][j-1])
    DP3[i][j]=max(DP2[i][j-1]+v,DP3[i][j-1])

    
print(max(DP0[R][C],DP1[R][C],DP2[R][C],DP3[R][C]))

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
value=[[0]*(C+1) for i in range(R+1)]
for i in range(K):
  r,c,v=map(int,input().split())
  value[r][c]=v
DP0=[[0]*(C+1) for i in range(R+1)]
DP1=[[0]*(C+1) for i in range(R+1)]
DP2=[[0]*(C+1) for i in range(R+1)]
DP3=[[0]*(C+1) for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    DP0[i][j]=max(DP0[i][j-1],DP1[i-1][j],DP2[i-1][j],DP3[i-1][j])
    DP1[i][j]=max(DP0[i-1][j]+value[i][j],DP1[i-1][j]+value[i][j],DP2[i-1][j]+value[i][j],
                  DP3[i-1][j]+value[i][j],DP1[i][j-1],DP0[i][j-1]+value[i][j])
    DP2[i][j]=max(DP1[i][j-1]+value[i][j],DP2[i][j-1])
    DP3[i][j]=max(DP2[i][j-1]+value[i][j],DP3[i][j-1])

    
print(max(DP0[R][C],DP1[R][C],DP2[R][C],DP3[R][C]))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys
input=sys.stdin.readline
r,c,k=map(int,input().split())
d=defaultdict(int)
for i in range(k):
  a1,a2,a3=map(int,input().split())
  d[a1*10**4+a2]=a3
dp0=[[0 for i in range(c+1)] for j in range(r+1)]
dp1=[[0 for i in range(c+1)] for j in range(r+1)]
dp2=[[0 for i in range(c+1)] for j in range(r+1)]
dp3=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
  for j in range(c):
    x=d[i*10**4+j+1]
    y=d[(i+1)*10**4+j]
    dp3[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp2[i+1][j]+y,dp3[i+1][j])
    dp2[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp1[i+1][j]+y,dp2[i+1][j])
    dp1[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j]+y,dp1[i+1][j])
    dp0[i+1][j+1]=max(dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j])
z=d[r*10**4+c]
print(max(dp2[i+1][j+1]+z,dp3[i+1][j+1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
r,c,k=map(int,input().split())
d=defaultdict(int)
for i in range(k):
  a1,a2,a3=map(int,input().split())
  d[a1*10**4+a2]=a3
dp0=[[0 for i in range(c+1)] for j in range(r+1)]
dp1=[[0 for i in range(c+1)] for j in range(r+1)]
dp2=[[0 for i in range(c+1)] for j in range(r+1)]
dp3=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
  for j in range(c):
    x=d[i*10**4+j+1]
    y=d[(i+1)*10**4+j]
    dp3[i+1][j+1]=max(dp0[i][j+1]+x,dp1[i][j+1]+x,dp2[i][j+1]+x,dp3[i][j+1],dp2[i+1][j]+y,dp3[i+1][j])
    dp2[i+1][j+1]=max(dp0[i][j+1]+x,dp1[i][j+1]+x,dp2[i][j+1]+x,dp3[i][j+1],dp1[i+1][j]+y,dp2[i+1][j])
    dp1[i+1][j+1]=max(dp0[i][j+1]+x,dp1[i][j+1]+x,dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j]+y,dp1[i+1][j])
    dp0[i+1][j+1]=max(dp0[i][j+1]+x,dp1[i][j+1]+x,dp2[i][j+1]+x,dp3[i][j+1],dp0[i+1][j])
z=d[r*10**4+c]
print(max(dp0[i+1][j+1]+z,dp1[i+1][j+1]+z,dp2[i+1][j+1]+z,dp3[i+1][j+1]))

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目j列到達時点での最大価値。i行目において拾えるアイテムの数はK個。
"""
def main():
    import sys
    input = sys.stdin.readline
    R,C,K = map(int,input().split())
    items = [[0]*C for _ in range(R)]
    for _ in range(K):
        r,c,v = map(int,input().split())
        items[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):
            v = items[i][j]

            #上から入るパターン
            dp[i][j][0] = max(dp[i-1][j])
            if v:
                dp[i][j][1] = dp[i][j][0]+v
            
            #左から入ってくるパターン
            if j > 0:
                for k in range(4):
                    #このマスのアイテムをとらないパターン
                    dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
                    if v and k>0:
                        dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+v)

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

Submission status: Time Limit Exceeded

Solution code:
"""
dp[i][j][k] => i行目j列到達時点での最大価値。i行目において拾えるアイテムの数はK個。
"""
R,C,K = map(int,input().split())
items = [[0]*C for _ in range(R)]
for _ in range(K):
    r,c,v = map(int,input().split())
    items[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):
        v = items[i][j]

        #上から入るパターン
        dp[i][j][0] = max(dp[i-1][j])
        if v:
            dp[i][j][1] = dp[i][j][0]+v
        
        #左から入ってくるパターン
        if j > 0:
            for k in range(4):
                #このマスのアイテムをとらないパターン
                dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k])
                if v and k>0:
                    dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k-1]+v)

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