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: Time Limit Exceeded

Solution code:
R, C, K = map(int,input().split())
MAP = [[0 for i in range(C)] for j in range(R)]
DP = [[[0 for i in range(4)] for j in range(C+1)] for k in range(R+1)]

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

for i in range(R):
    for j in range(C):
        DP[i+1][j+1][0] = max(DP[i+1][j+1][0], max(DP[i][j+1]))
        DP[i+1][j+1][1] = max(DP[i+1][j+1][1], max(DP[i][j+1]) + MAP[i][j])
        for k in range(4):
            DP[i+1][j+1][k] = max(DP[i+1][j+1][k], DP[i+1][j][k])
        for k in range(3):
            DP[i+1][j+1][k+1] = max(DP[i+1][j+1][k+1], DP[i+1][j][k] + MAP[i][j])

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

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split())
item = [[0]*(C+1) for _ in range(R+1)]
for _ in range(K):
    r, c, v = map(int, input().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)
                # 移動先のアイテムをとる場合
                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)
                # 現在のkが3未満のときのみ，移動先のアイテムをとることが可能
                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: Time Limit Exceeded

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

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

MAP=[[0 for _ in range(C+2)] for _ in range(R+2)]
DP=[[[0 for _ in range(3)] for _ in range(C+2)] for _ in range(R+2)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

for i in range(R):
    for j in range(C):
        for k in range(3):
            if k==0:
                DP[i][j][k]=max(DP[i][j-1][k],DP[i-1][j][k]+MAP[i][j],
                                DP[i-1][j][k+1]+MAP[i][j],
                                DP[i-1][j][k+2]+MAP[i][j])
            else:
                DP[i][j][k]=max(DP[i][j-1][k],DP[i][j-1][k-1]+MAP[i][j])

ans=0
for i in range(R):
    for j in range(C):
        ans=max(ans,max(DP[i][j]))
print(ans)

Submission status: Time Limit Exceeded

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

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

MAP=[[0 for _ in range(C+2)] for _ in range(R+2)]
DP=[[[0 for _ in range(3)] for _ in range(C+2)] for _ in range(R+2)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

for i in range(R):
    for j in range(C):
        for k in range(3):
            if k==0:
                DP[i][j][k]=max(DP[i][j-1][k],DP[i-1][j][k]+MAP[i][j],
                                DP[i-1][j][k+1]+MAP[i][j],
                                DP[i-1][j][k+2]+MAP[i][j])
            else:
                DP[i][j][k]=max(DP[i][j-1][k],DP[i][j-1][k-1]+MAP[i][j])

ans=0
for i in range(R):
    for j in range(C):
        ans=max(ans,max(DP[i][j]))
print(ans)

Submission status: Time Limit Exceeded

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

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

MAP=[[0 for _ in range(C)] for _ in range(R)]
DP=[[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

DP[0][0][1]=MAP[0][0]
for i in range(R):
    for j in range(C):
        score=max(DP[i][j])
        for num,point in enumerate([[1,0],[0,1]]):
            x,y=point
            X,Y=i+x,j+y
            if 0<=X<R and 0<=Y<C:
                for k in range(3):
                    if num==0:
                        DP[X][Y][1]=max(DP[X][Y][1],score+MAP[X][Y])
                    else:
                        DP[X][Y][k+1]=max(DP[X][Y][k+1],DP[X][Y][k],DP[i][j][k+1],DP[i][j][k]+MAP[X][Y])
print(max(DP[-1][-1]))

Submission status: Time Limit Exceeded

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

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

MAP=[[0 for _ in range(C)] for _ in range(R)]
DP=[[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

DP[0][0][1]=MAP[0][0]
for i in range(R):
    for j in range(C):
        for num,point in enumerate([[1,0],[0,1]]):
            x,y=point
            X,Y=i+x,j+y
            if 0<=X<R and 0<=Y<C:
                for k in range(3):
                    if num==0:
                        DP[X][Y][1]=max(DP[X][Y][1],max(DP[i][j])+MAP[X][Y])
                    else:
                        DP[X][Y][k+1]=max(DP[X][Y][k+1],DP[X][Y][k],DP[i][j][k+1],DP[i][j][k]+MAP[X][Y])
print(max(DP[-1][-1]))

Submission status: Time Limit Exceeded

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

MAP=[[0 for _ in range(C)] for _ in range(R)]
DP=[[[0 for _ in range(4)] for _ in range(C)] for _ in range(R)]
for r,c,v in rcv:
    MAP[r-1][c-1]=v

DP[0][0][1]=MAP[0][0]
for i in range(R):
    for j in range(C):
        for num,point in enumerate([[1,0],[0,1]]):
            x,y=point
            X,Y=i+x,j+y
            if 0<=X<R and 0<=Y<C:
                for k in range(3):
                    if num==0:
                        DP[X][Y][1]=max(DP[X][Y][1],max(DP[i][j])+MAP[X][Y])
                    else:
                        DP[X][Y][k+1]=max(DP[X][Y][k+1],DP[X][Y][k],DP[i][j][k+1],DP[i][j][k]+MAP[X][Y])
print(max(DP[-1][-1]))

Submission status: Time Limit Exceeded

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

# dp[i][j][k] = i,jまでで、i列のものをk個取ったときのスコア
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):
    m = mat[i-1][j-1]
    # 左からの遷移
    dp1[i][j] = max(dp1[i][j-1], dp0[i][j-1]+m)
    dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1]+m)
    dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1]+m)

    # 上からの遷移
    x = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])
    dp0[i][j] = max(dp0[i][j-1], x)
    dp1[i][j] = max(dp1[i][j], x+m)

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

# dp[i][j][k] = i,jまでで、i列のものをk個取ったときのスコア
dp = [ [ [0]*4 for _ in range(C+1)] for _ in range(R+1) ]

for i in range(1,R+1):
  for j in range(1,C+1):
    m = mat[i-1][j-1]
    # 左からの遷移
    for k in range(1, 4):
      dp[i][j][k] = max(dp[i][j-1][k], dp[i][j-1][k-1]+m)

    # 上からの遷移
    x = max(dp[i-1][j])
    dp[i][j][0] = max(dp[i][j-1][0], x)
    dp[i][j][1] = max(dp[i][j][1], x+m)

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

Submission status: Time Limit Exceeded

Solution code:
'''
dpt[r][c][taken]
への遷移は、
max(dpt[r-1][c]) -> dpt[r][c][0]
dpt[r][c-1][taken-1] -> dpt[r][c][taken] + field[r][c]
dpt[r][c-1][taken] -> dpt[r][c][taken]
'''

import numpy as np
from numba import njit
def main():
    R, C, K = map(int, input().split())
    field = np.full((R+1,C+1), 0)
    dpt = np.full((R+1,C+1,4), 0)
    for i in range(K):
        r, c, v = map(int, input().split())
        field[r][c] = v
    dp(R, C, field, dpt)
    print(max(dpt[-1][-1]))

def dp(R, C, field, dpt):
    for r in range(1, R+1):
        for c in range(1, C+1):
            for taken in range(4):
                max_v = 0
                if taken == 0:
                    max_v = max(dpt[r-1][c])
                    max_v = max(dpt[r][c-1][0], max_v)
                elif taken == 1:
                    max_v = max(dpt[r][c-1][taken-1] + field[r][c],
                                dpt[r][c-1][taken],
                                max(dpt[r-1][c]) + field[r][c])
                else:
                    max_v = max(dpt[r][c-1][taken-1] + field[r][c],
                                dpt[r][c-1][taken])
                dpt[r][c][taken] = max_v
    return True

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

items = [[0] * c for i in range(r)]

for _ in range(k):
    i, j, v = map(int, input().split())
    i -= 1
    j -= 1
    items[i][j] += v

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

for i in range(r):
    for j in range(c):
        for k in range(2, -1, -1):
            dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + items[i][j])
        for k in range(4):
            if i + 1 < r:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k])
            if j + 1 < c:
                dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
print(max(dp[r-1][c-1]))

Submission status: Time Limit Exceeded

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]

def solve():
    def chmax(i, j, c, val):dp[i][j][c]=max(dp[i][j][c],val)

    dp = [[[-1] * 4 for _ in range(w+1)] for _ in range(h+1)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            if aa[i][j]:
                for c in range(2,-1,-1):
                    pre = dp[i][j][c]
                    if pre==-1:continue
                    chmax(i,j,c+1,dp[i][j][c]+aa[i][j])
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -1: continue
                chmax(i+1,j,0,pre)
                chmax(i,j+1,c,pre)

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

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
solve()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  H=range(h)
  W=range(w)
  dp=[[[0]*4 for j in W] for i in H]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[0][0][1]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[y][x][0]
      dp[y][x][0]=0
      for i in range(4):
        if 0<x:
          if dp[y][x][i]<dp[y][x-1][i]:
            dp[y][x][i]=dp[y][x-1][i]
          if 0<i and v!=0 and dp[y][x][i]<dp[y][x-1][i-1]+v:
            dp[y][x][i]=dp[y][x-1][i-1]+v
        if 0<y:
          if dp[y][x][0]<dp[y-1][x][i]:
            dp[y][x][0]=dp[y-1][x][i]
          if v!=0 and dp[y][x][1]<dp[y-1][x][i]+v:
            dp[y][x][1]=dp[y-1][x][i]+v
  print(max(dp[h-1][w-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  dp=[[[0]*4 for j in range(w)] for i in range(h)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    dp[r-1][c-1][0]=v
  if dp[0][0][0]!=0:
    dp[0][0][1]=dp[0][0][0]
    dp[0][0][0]=0
  for y in range(h):
    for x in range(w):
      v=dp[y][x][0]
      dp[y][x][0]=0
      for i in range(4):
        if 0<x:
          if dp[y][x][i]<dp[y][x-1][i]:
            dp[y][x][i]=dp[y][x-1][i]
          if 0<i and v!=0 and dp[y][x][i]<dp[y][x-1][i-1]+v:
            dp[y][x][i]=dp[y][x-1][i-1]+v
        if 0<y:
          if dp[y][x][0]<dp[y-1][x][i]:
            dp[y][x][0]=dp[y-1][x][i]
          if v!=0 and dp[y][x][1]<dp[y-1][x][i]+v:
            dp[y][x][1]=dp[y-1][x][i]+v
  print(max(dp[h-1][w-1]))
main()

Submission status: Time Limit Exceeded

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]

def solve():
    dp = [[[-1] * 4 for _ in range(w)] for _ in range(h)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -1: continue
                if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre)
                if j + 1 < w: dp[i][j + 1][c] = max(dp[i][j + 1][c], pre)
                if c<3 and aa[i][j]:
                    if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre + aa[i][j])
                    if j + 1 < w: dp[i][j + 1][c + 1] = max(dp[i][j + 1][c + 1], pre + aa[i][j])

    ans = 0
    for c in range(4):
        if c == 3: ans = max(ans, dp[h - 1][w - 1][c])
        else: ans = max(ans, dp[h - 1][w - 1][c] + aa[h - 1][w - 1])
    print(ans)

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
solve()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  h,w,k=map(int,input().split())
  p=[[0]*w for _ in range(h)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    p[r-1][c-1]=v
  dp=[[[0]*4 for j in range(w)] for i in range(h)]
  if p[0][0]!=0:
    dp[0][0][1]=p[0][0]
  for y in range(h):
    for x in range(w):
      for i in range(4):
        if x<w-1:
          if dp[y][x+1][i]<dp[y][x][i]:
            dp[y][x+1][i]=dp[y][x][i]
          if i<3 and p[y][x+1]!=0 and dp[y][x+1][i+1]<dp[y][x][i]+p[y][x+1]:
            dp[y][x+1][i+1]=dp[y][x][i]+p[y][x+1]
        if y<h-1:
          if dp[y+1][x][0]<dp[y][x][i]:
            dp[y+1][x][0]=dp[y][x][i]
          if p[y+1][x]!=0 and dp[y+1][x][1]<dp[y][x][i]+p[y+1][x]:
            dp[y+1][x][1]=dp[y][x][i]+p[y+1][x]
  print(max(dp[h-1][w-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  h,w,k=map(int,input().split())
  p=[[0]*w for _ in range(h)]
  for _ in range(k):
    r,c,v=map(int,input().split())
    p[r-1][c-1]=v
  dp=[[[0]*4 for j in range(w)] for i in range(h)]
  if p[0][0]!=0:
    dp[0][0][1]=p[0][0]
  for y in range(h):
    for x in range(w):
      for i in range(4):
        if x<w-1:
          if dp[y][x+1][i]<dp[y][x][i]:
            dp[y][x+1][i]=dp[y][x][i]
          if i<3 and p[y][x+1]!=0 and dp[y][x+1][i+1]<dp[y][x][i]+p[y][x+1]:
            dp[y][x+1][i+1]=dp[y][x][i]+p[y][x+1]
        if y<h-1:
          if dp[y+1][x][0]<dp[y][x][i]:
            dp[y+1][x][0]=dp[y][x][i]
          if p[y+1][x]!=0 and dp[y+1][x][1]<dp[y][x][i]+p[y+1][x]:
            dp[y+1][x][1]=dp[y][x][i]+p[y+1][x]
  print(max(dp[h-1][w-1]))
main()

Submission status: Time Limit Exceeded

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

items = [[0] * c for i in range(r)]

for _ in range(k):
    i, j, v = map(int, input().split())
    i -= 1
    j -= 1
    items[i][j] += v

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


for i in range(r):
    for j in range(c):
        for k in range(2,-1,-1):
            dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + items[i][j])
        for k in range(4):
            if i + 1 < r:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k])
            if j + 1 < c:
                dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
print(max(dp[r-1][c-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys

R, C, K = map(int, sys.stdin.readline().split())

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

# r行目、c列目にいるときに、その行でアイテムをi個取得している場合の最大価値
dp0 = [[0 for _ in range(C+1)] for _ in range(R+1)]
dp1 = [[0 for _ in range(C+1)] for _ in range(R+1)]
dp2 = [[0 for _ in range(C+1)] for _ in range(R+1)]
dp3 = [[0 for _ in range(C+1)] for _ in range(R+1)]
# print(dp)
for r in range(R):
    for c in range(C):
        # 取らない
        dp0[r+1][c] = max((dp0[r][c], dp1[r][c], dp2[r][c], dp3[r][c]))
        dp1[r][c+1] = max(dp1[r][c], dp1[r][c])
        dp2[r][c+1] = max(dp2[r][c], dp2[r][c])
        dp3[r][c+1] = max(dp3[r][c], dp3[r][c])

        if grid[r][c] == 0:
            continue
        # 取る
        dp0[r+1][c] = max(dp0[r][c] + grid[r][c], dp0[r+1][c])
        dp0[r+1][c] = max(dp1[r][c] + grid[r][c], dp0[r+1][c])
        dp0[r+1][c] = max(dp2[r][c] + grid[r][c], dp0[r+1][c])

        dp1[r][c+1] = max(dp0[r][c] + grid[r][c], dp1[r][c+1])
        dp2[r][c+1] = max(dp1[r][c] + grid[r][c], dp2[r][c+1])
        dp3[r][c+1] = max(dp2[r][c] + grid[r][c], dp3[r][c+1])

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline

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

  # dp[i][j][l]:(i,j)に居て、i行目のアイテムをl個拾ったときの価値最大値。
  dp = [[[0 for _ in range(4)] for _ in range(c+1)] for _ in range(r+1)]
  for i in range(r):
    for j in range(c):
      # アイテムを拾う遷移。個数が多い方から計算する。
      for l in range(2,-1,-1):
        dp[i][j][l+1] = max(dp[i][j][l]+b[i][j],dp[i][j][l+1])
      # 移動する遷移。行を移動する際は、拾ったアイテム数はリセットされる。
      for l in range(4):
        dp[i+1][j][0] = max(dp[i][j][l],dp[i+1][j][0])
        dp[i][j+1][l] = max(dp[i][j][l],dp[i][j+1][l])
  ans = 0
  for i in range(4):
    ans = max(dp[r-1][c-1][i],ans)
  print(ans)

main()

Submission status: Time Limit Exceeded

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

# dp[i][j][l]:(i,j)に居て、i行目のアイテムをl個拾ったときの価値最大値。
dp = [[[0 for _ in range(4)] for _ in range(c+1)] for _ in range(r+1)]
for i in range(r):
  for j in range(c):
    # アイテムを拾う遷移。個数が多い方から計算する。
    for l in range(2,-1,-1):
      dp[i][j][l+1] = max(dp[i][j][l]+b[i][j],dp[i][j][l+1])
    # 移動する遷移。行を移動する際は、拾ったアイテム数はリセットされる。
    for l in range(4):
      dp[i+1][j][0] = max(dp[i][j][l],dp[i+1][j][0])
      dp[i][j+1][l] = max(dp[i][j][l],dp[i][j+1][l])
ans = 0
for i in range(4):
  ans = max(dp[r-1][c-1][i],ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp=[0]*C
for i in range(R):
  ndp=[0]*C
  kdp=[0]*4
  for j in range(0,C):
    kdp[0]=max(kdp[0],dp[j])
    for k in range(2,-1,-1):
      kdp[k+1]=max(kdp[k+1],kdp[k]+l[i][j])
    ndp[j]=max(kdp)
  dp=ndp
print(dp[C-1])

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
l=[[0] * C for i in range(R)]
dp=[[[0] * 4 for i in range(C)] for j in range(R)]
for i in range(K):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  l[a][b]=c
dp[0][0][1]=l[0][0]
for x in range(1,R+C-1):
  for y in range(R):
    i=y
    j=x-y
    if j<0 or C<=j:
      continue
    for k in range(4):
      A=0
      if 0<j:
        A=max(A,dp[i][j-1][k])
        if 0<k:
          A=max(A,dp[i][j-1][k-1]+l[i][j])
      if 0<i and k<2:
        for q in range(4):
          A=max(A,dp[i-1][j][q]+k*l[i][j])
      dp[i][j][k]=A
X=dp[R-1][C-1][3]
for k in range(3):
  X=max(X,dp[R-1][C-1][k])
print(X)

Submission status: Time Limit Exceeded

Solution code:
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    r, c, k = map(int, read().split())
    items = [[0] * c for _ in range(r)]
    for _ in range(k):
        ri, ci, vi = map(int, read().split())
        items[ri - 1][ci - 1] = vi
    dp = [[[0] * 4 for _ in range(c)] for _ in range(r)]
    dp[0][0][1] = items[0][0]
    for i in range(r):
        for j in range(c):
            if i < r - 1:
                dp[i + 1][j][0] = max(max(dp[i][j]), dp[i + 1][j][0])
                dp[i + 1][j][1] = max(dp[i + 1][j][0] + items[i + 1][j], dp[i + 1][j][1])
            if j < c - 1:
                dp[i][j + 1][0] = max(dp[i][j][0], dp[i][j + 1][0])
                dp[i][j + 1][1] = max(dp[i][j][1], dp[i][j + 1][1])
                dp[i][j + 1][2] = max(dp[i][j][2], dp[i][j + 1][2])
                dp[i][j + 1][3] = max(dp[i][j][3], dp[i][j + 1][3])
                if items[i][j + 1] > 0:
                    dp[i][j + 1][1] = max(dp[i][j][0] + items[i][j + 1], dp[i][j + 1][1])
                    dp[i][j + 1][2] = max(dp[i][j][1] + items[i][j + 1], dp[i][j + 1][2])
                    dp[i][j + 1][3] = max(dp[i][j][2] + items[i][j + 1], dp[i][j + 1][3])
    print(max(dp[-1][-1]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
# input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

h,w,k=map(int,input().split())
# R=[]
A=[[0]*w for i in range(h)]
dp=[[[0]*4 for j in range(w)] for i in range(h)]

for s in sys.stdin.readlines():
    x,y,v=map(int,s.split())
    x-=1;y-=1
    A[x][y]=v
    # dp[x][y][1]=v
    # dp[x][y][2]=v
    # dp[x][y][3]=v;
dp[0][0][1]=A[0][0]
for i in range(h):
    for j in range(w):
        for k in range(4):
            here=dp[i][j][k]
            if i<h-1:
                dp[i+1][j][0] = max(here,dp[i+1][j][0])
                dp[i+1][j][1] = max(here+A[i+1][j], dp[i+1][j][1])
            if j<w-1:
                dp[i][j+1][k] = max(dp[i][j+1][k], here)
                if k<=2:
                    dp[i][j+1][k+1]= max(dp[i][j+1][k+1],here+ A[i][j+1])
           

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

Submission status: Time Limit Exceeded

Solution code:
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

h,w,k=map(int,input().split())
R=[]
A=[[0]*w for i in range(h)]
dp=[[[0]*4 for j in range(w)] for i in range(h)]

for i in range(k):
    x,y,v=map(int,input().split())
    x-=1;y-=1
    A[x][y]=v
    dp[x][y][1]=v
    # dp[x][y][2]=v
    # dp[x][y][3]=v;

for i in range(h):
    for j in range(w):
        for k in range(4):
            here=dp[i][j][k]
            if i<h-1:
                dp[i+1][j][0] = max(here,dp[i+1][j][0])
                dp[i+1][j][1] = max(here+A[i+1][j], dp[i+1][j][1])
            if j<w-1:
                dp[i][j+1][k] = max(dp[i][j+1][k], here)
                if k<=2:
                    dp[i][j+1][k+1]= max(dp[i][j+1][k+1],here+ A[i][j+1])
           

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

Submission status: Time Limit Exceeded

Solution code:
h,w,k=map(int,input().split())
R=[]
A=[[0]*w for i in range(h)]
dp=[[[0]*4 for j in range(w)] for i in range(h)]

for i in range(k):
    x,y,v=map(int,input().split())
    x-=1;y-=1
    A[x][y]=v
    dp[x][y][1]=v
    dp[x][y][2]=v
    dp[x][y][3]=v;

for i in range(h):
    for j in range(w):
        for k in [0,1,2,3]:
            here=dp[i][j][k]
            if i<h-1:
                dp[i+1][j][0] = max(here,dp[i+1][j][0])
                dp[i+1][j][1] = max(here+A[i+1][j], dp[i+1][j][1])
            if j<w-1:
                dp[i][j+1][k] = max(dp[i][j+1][k], here)
                if k<=2:
                    dp[i][j+1][k+1]= max(dp[i][j+1][k+1],here+ A[i][j+1])


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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

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

dp[1][1][1] = data[1][1]

for i in range(1,R+1):
    for j in range(1,C+1):
        for k in range(4):
            if i+1 < R+1:
                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]+data[i+1][j])     
            if j+1 < C+1:
                dp[i][j+1][k] = max(dp[i][j][k],dp[i][j+1][k])
                if k+1 <= 3:
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1],dp[i][j][k]+data[i][j+1])
            
print(max(dp[i][j]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
R,C,k=map(int,input().split())
A=[[0]*C for i in range(R)]
for i in range(k):
  r,c,v=map(int,input().split())
  A[r-1][c-1]=v
dp=[[[0,0,0,0] for j in range(C+1)] for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    V=A[i-1][j-1]
    dp[i][j][0]=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][0])
    dp[i][j][1]=max(dp[i][j][0]+V,dp[i][j-1][1])
    dp[i][j][2]=max(dp[i][j-1][1]+V,dp[i][j-1][2])
    dp[i][j][3]=max(dp[i][j-1][2]+V,dp[i][j-1][3])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
input=sys.stdin.readline
d=defaultdict(int)
R,C,k=map(int,input().split())
for i in range(k):
  r,c,v=map(int,input().split())
  d[(r-1)*C+c-1]=v
dp=[[[0,0,0,0] for j in range(C+1)] for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    V=d[(i-1)*C+(j-1)]
    dp[i][j][0]=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][0])
    dp[i][j][1]=max(dp[i][j][0]+V,dp[i][j-1][1])
    dp[i][j][2]=max(dp[i][j-1][1]+V,dp[i][j-1][2])
    dp[i][j][3]=max(dp[i][j-1][2]+V,dp[i][j-1][3])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
input=sys.stdin.readline
d=defaultdict(int)
R,C,k=map(int,input().split())
for i in range(k):
  r,c,v=map(int,input().split())
  d[(r-1)*C+c-1]=v
dp=[[[0,0,0,0] for j in range(C+1)] for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    V=d[(i-1)*C+(j-1)]
    dp[i][j][0]=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][0])
    dp[i][j][1]=max(dp[i-1][j][0]+V,dp[i-1][j][1]+V,dp[i-1][j][2]+V,dp[i-1][j][3]+V,dp[i][j-1][0]+V,dp[i][j-1][1])
    dp[i][j][2]=max(dp[i][j-1][1]+V,dp[i][j-1][2])
    dp[i][j][3]=max(dp[i][j-1][2]+V,dp[i][j-1][3])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
R,C,k=map(int,input().split())
A=[[0]*C for i in range(R)]
for i in range(k):
  r,c,v=map(int,input().split())
  A[r-1][c-1]=v
dp=[[[0,0,0,0] for j in range(C+1)] for i in range(R+1)]
for i in range(1,R+1):
  for j in range(1,C+1):
    dp[i][j][0]=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][0])
    dp[i][j][1]=max(dp[i-1][j][0]+A[i-1][j-1],dp[i-1][j][1]+A[i-1][j-1],dp[i-1][j][2]+A[i-1][j-1],dp[i-1][j][3]+A[i-1][j-1],dp[i][j-1][0]+A[i-1][j-1],dp[i][j-1][1])
    dp[i][j][2]=max(dp[i][j-1][1]+A[i-1][j-1],dp[i][j-1][2])
    dp[i][j][3]=max(dp[i][j-1][2]+A[i-1][j-1],dp[i][j-1][3])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

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

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

            dp3[i][j+1] = max(dp3[i][j+1], dp3[i][j])
            dp2[i][j+1] = max(dp2[i][j+1], dp2[i][j])
            dp1[i][j+1] = max(dp1[i][j+1], dp1[i][j])
            dp0[i][j+1] = max(dp0[i][j+1], dp0[i][j])

            dp0[i+1][j] = max(dp0[i+1][j], dp0[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp1[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp2[i][j])
            dp0[i+1][j] = max(dp0[i+1][j], dp3[i][j])
    #print(dp)
    print(max([dp0[R-1][C-1], dp1[R-1][C-1], dp2[R-1][C-1], dp3[R-1][C-1]]))

if __name__ == '__main__':
    main()