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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

R,C,K = IL()
data = [[0 for i in range(C)] for j in range(R)]
for _ in range(K):
  j,i,v = IL()
  data[j-1][i-1] = v

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import numpy as np
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

d = defaultdict(int)
for i in range(K):
    a, b, c = list(map(int, stdin.readline().split()))
    d[(a,b)] = c

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

dp = np.zeros((R+1, C+1, 4))

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


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

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

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

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

items = defaultdict(int)
for r, c, v in X:
    items[(r, c)] = v

for i in range(1, R + 1):
    for j in range(1, C + 1):
        for k in range(4):
            # Horizontal
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k])

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

        for k in reversed(range(4)):
            if items[(i, j)] > 0 and k < 3:
                dp[i][j][k + 1] = max(
                    dp[i][j][k + 1], dp[i][j][k] + items[(i, j)])

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

R,C,K=map(int,input().split())
V=[[0]*C for _ in range(R)]
for i in range(K):
    r,c,v=map(int,input().split())
    r-=1
    c-=1
    V[r][c]=v
dp=[[[0]*4 for _ in range(C)] for _ in range(R)]
for y in range(R):
    for x in range(C):
        for i in range(2,-1,-1):
            dp[y][x][i+1]=max(dp[y][x][i+1],dp[y][x][i]+V[y][x])
        if x<C-1:
            for i in range(3,-1,-1):
                dp[y][x+1][i]=max(dp[y][x+1][i],dp[y][x][i])
    if y+1<R:
        for x in range(C):
            dp[y+1][x][0]=max(dp[y][x])
print(max(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
R,C,K=map(int,input().split())
l=[[[0]*4 for i in range(C)] for j in range(R)]
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
if m[0][0]:
    l[0][0][1]=m[0][0]
for i in range(1,C):
    l[0][i][1]=max(l[0][i-1][1],m[0][i])
    l[0][i][2]=max(l[0][i-1][2],m[0][i]+l[0][i-1][1])
    l[0][i][3]=max(l[0][i-1][3],m[0][i]+l[0][i-1][2])
for i in range(1,R):
    l[i][0][1]=max(l[i-1][0])+m[i][0]
    for j in range(1,C):
        if m[i][j]:
            ma=max(l[i-1][j])
            mi=m[i][j]
            if ma<l[i][j-1][0]:
                l[i][j][0]=l[i][j-1][0]
            else:
                l[i][j][0]=ma
            l[i][j][1]=max(l[i][j-1][0]+m[i][j],l[i][j-1][1],ma+mi)
            if l[i][j-1][1]+mi>l[i][j-1][2]:
                l[i][j][2]=l[i][j-1][1]+mi
            else:
                l[i][j][2]=l[i][j-1][2]
            if l[i][j-1][2]+mi>l[i][j-1][3]:
                l[i][j][3]=l[i][j-1][2]+mi
            else:
                l[i][j][3]=l[i][j-1][3]
        else:
            l[i][j]=l[i][j-1].copy()
            ma=max(l[i-1][j])
            if l[i][j][0]<ma:
                l[i][j][0]=ma
print(max(l[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
def calc(r, c):
    return (r * (10 ** 4)) + c


row, column, k = map(int, input().split())

my_d = dict()

for _ in range(k):
    r, c, v = map(int, input().split())
    if calc(r-1, c-1) in my_d:
        my_d[calc(r-1, c-1)].append(v)
    else:
        my_d[calc(r-1, c-1)] = [v]

for item in my_d:
    my_d[item].sort()

dp = [[[0 for _ in range(4)] for _ in range(column)] for _ in range(row)]

for i in range(row):
    for j in range(column):
        for kosu in range(4):
            if kosu == 0:
                dp[i][j][0] = 0 if i == 0 else max(dp[i-1][j])
            elif kosu == 1:
                cand1 = 0 if i == 0 else max(dp[i-1][j])
                cand2 = 0 if j == 0 else dp[i][j-1][1]
                cand3 = 0 if calc(i, j) not in my_d else my_d[calc(i, j)][-1]
                dp[i][j][1] = max(cand1 + cand3, cand2)
            elif kosu == 2:
                cand1 = 0 if i == 0 else max(dp[i-1][j])
                cand2 = 0 if j == 0 else dp[i][j-1][1]
                cand3 = 0 if calc(i, j) not in my_d else my_d[calc(i, j)][-1]
                cand4 = 0 if i == 0 else max(dp[i-1][j])
                cand5 = 0 if j == 0 else dp[i][j-1][2]
                cand6 = 0 if (calc(i, j) not in my_d or len(
                    my_d[calc(i, j)]) < 2) else sum(my_d[calc(i, j)][-2:])
                dp[i][j][2] = max(cand5, cand6 + cand4, cand2 + cand3)
            elif kosu == 3:
                cand1 = 0 if i == 0 else max(dp[i-1][j])
                cand2 = 0 if j == 0 else dp[i][j-1][1]
                cand3 = 0 if calc(i, j) not in my_d else my_d[calc(i, j)][-1]
                cand4 = 0 if i == 0 else max(dp[i-1][j])
                cand5 = 0 if j == 0 else dp[i][j-1][2]
                cand6 = 0 if (calc(i, j) not in my_d or len(
                    my_d[calc(i, j)]) < 2) else sum(my_d[calc(i, j)][-2:])
                cand7 = 0 if i == 0 else max(dp[i-1][j])
                cand8 = 0 if j == 0 else dp[i][j-1][3]
                cand9 = 0 if (calc(i, j) not in my_d or len(
                    my_d[calc(i, j)]) < 3) else sum(my_d[calc(i, j)][-3:])
                dp[i][j][3] = max(cand8, cand7+cand9, cand5+cand3, cand2+cand6)

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import numpy as np
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

d = defaultdict(int)
for i in range(K):
    a, b, c = list(map(int, input().split()))
    d[(a,b)] = c

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

dp = np.zeros((R+1, C+1, 4))

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


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

Submission status: Time Limit Exceeded

Solution code:
r,c,k = map(int, input().split())
rcv = [list(map(int, input().split())) for i in range(k)]
d = dict()
for rr,cc,vv in rcv:
    d[(rr-1,cc-1)] = vv

DP_now = [[0]*4 for i in range(c)]
DP_prev = [[0]*4 for i in range(c)]

for i in range(r):
    for j in range(c):
        if (i,j) in d:
            v = d[(i,j)]
            max_top = max(DP_prev[j]) if i!=0 else 0
            DP_now[j][0] = max_top
            DP_now[j][1] = max_top + v
            for k in range(4):
                if j != 0:
                    if DP_now[j][k] < DP_now[j-1][k]:
                        DP_now[j][k] = DP_now[j-1][k]
                    if k!=0 and DP_now[j][k] < DP_now[j-1][k-1] + v:
                        DP_now[j][k] =  DP_now[j-1][k-1] + v
        else:
            max_top = max(DP_prev[j]) if i!=0 else 0
            DP_now[j][0] = max_top
            for k in range(4):
                if j != 0 and DP_now[j][k] < DP_now[j-1][k]:
                    DP_now[j][k] = DP_now[j-1][k]
    DP_prev = DP_now
    DP_now = [[0]*4 for i in range(c)]
ans = max(DP_prev[-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(k)]
    dp=np.array([[[0,0,0,0,0]for _ in range(c+1)]for _ in range(r+1)])
    b=[[0 for _ in range(c+1)] for _ in range(r+1)]
    for x,y,z in a:
        b[x][y]=z
    for i in range(1,r+1):
        for j in range(1,c+1):
            v=b[i][j]
            if b==0:
                dp[i][j][0]=max(dp[i][j-1][0],dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])
                dp[i][j][1]=dp[i][j-1][1]
                dp[i][j][2]=dp[i][j-1][2]
                dp[i][j][3]=dp[i][j-1][3]
            else:
                dp[i][j][0]=max(dp[i][j-1][0],dp[i-1][j][0],dp[i-1][j][1],dp[i-1][j][2],dp[i-1][j][3])
                dp[i][j][1]=max(dp[i][j-1][1],dp[i][j-1][0]+v,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][2]=max(dp[i][j-1][2],dp[i][j-1][1]+v)
                dp[i][j][3]=max(dp[i][j-1][3],dp[i][j-1][2]+v)
    print(max(dp[i][j]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

rck = {}
for i in range(K):
    tmp = list(map(int, input().split()))
    rck[(tmp[0], tmp[1])] = tmp[2]


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

# print(rck)
for r_i in range(1, R+1):
    for c_i in range(1, C+1):

        tmp0 = max(dp[c_i-1][r_i][0], dp[c_i][r_i-1][3])
        tmp1 = max(dp[c_i-1][r_i][1], dp[c_i][r_i-1][3])
        tmp2 = max(dp[c_i-1][r_i][2], dp[c_i][r_i-1][3])
        tmp3 = max(dp[c_i-1][r_i][3], dp[c_i][r_i-1][3])
        # print(c_i, r_i)
        # print(tmp0, tmp1, tmp2, tmp3)

        index = (r_i, c_i)

        if index in rck:
            dp[c_i][r_i][3] = max(tmp3, tmp2+rck[index])
            dp[c_i][r_i][2] = max(tmp2, tmp1+rck[index])
            dp[c_i][r_i][1] = max(tmp1, tmp0+rck[index])
            dp[c_i][r_i][0] = tmp0
        else:
            dp[c_i][r_i][3] = tmp3
            dp[c_i][r_i][2] = tmp2
            dp[c_i][r_i][1] = tmp1
            dp[c_i][r_i][0] = tmp0

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import numpy as np
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

d = defaultdict(int)
for i in range(K):
    a, b, c = list(map(int, input().split()))
    d[(a,b)] = c

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

dp = list(np.zeros((R+1, C+1, 4)))

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


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

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    r, c, k = map(int, readline().split())
    items = dict()
    for _ in range(k):
        row, col, value = map(int, readline().split())
        items[(row, col)] = value

    dp = [[[0] * 4 for _ in range(c + 2)] for _ in range(r + 2)]

    for row in range(1, r + 1):
        for col in range(1, c + 1):
            if (row, col) in items:
                value = items[(row, col)]
            else:
                value = 0
            for i in range(3):
                dp[row][col][1] = max(dp[row][col][1],dp[row][col][0] + value)
                dp[row][col][i + 1] = max(dp[row][col - 1][i + 1], dp[row][col - 1][i] + value)

            dp[row + 1][col][0] = max(dp[row][col])

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

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

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

    for i in range(R):
        for j in range(C):
            if i == 0:
                break
            if G[i][j] is not None:
                if dp[i][j + 1][0] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0])
                if dp[i][j + 1][1] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1])
                if dp[i][j + 1][2] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2])
                if dp[i][j + 1][3] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3])
            else:

                if dp[i][j + 1][0] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0])
                if dp[i][j + 1][1] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1])
                if dp[i][j + 1][2] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2])
                if dp[i][j + 1][3] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3])

        for j in range(C):
            if G[i][j] is not None:

                if dp[i + 1][j + 1][0] is not None and dp[i + 1][j][0] is not None:
                    if dp[i + 1][j][1] is not None:
                        if dp[i + 1][j + 1][1] is not None:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + G[i][j], dp[i + 1][j][1])
                        else:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j][1], dp[i + 1][j][0] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][1] is not None:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + G[i][j])
                        else:
                            dp[i + 1][j + 1][1] = dp[i + 1][j][0] + G[i][j]

                if dp[i + 1][j + 1][1] is not None and dp[i + 1][j][1] is not None:
                    if dp[i + 1][j][2] is not None:
                        if dp[i + 1][j + 1][2] is not None:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][1] + G[i][j], dp[i + 1][j][2])
                        else:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j][2], dp[i + 1][j][1] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][2] is not None:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][1] + G[i][j])
                        else:
                            dp[i + 1][j + 1][2] = dp[i + 1][j][1] + G[i][j]

                if dp[i + 1][j + 1][2] is not None and dp[i + 1][j][2] is not None:
                    if dp[i + 1][j][3] is not None:
                        if dp[i + 1][j + 1][3] is not None:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][2] + G[i][j], dp[i + 1][j][3])
                        else:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j][3], dp[i + 1][j][2] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][3] is not None:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][2] + G[i][j])
                        else:
                            dp[i + 1][j + 1][3] = dp[i + 1][j][2] + G[i][j]


            else:
                if dp[i + 1][j][0] is not None:
                    if dp[i + 1][j + 1][0] is not None:
                        dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i + 1][j][0])
                    else:
                        dp[i + 1][j + 1][0] = dp[i + 1][j][0]

                if dp[i + 1][j][1] is not None:
                    if dp[i + 1][j + 1][1] is not None:
                        dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][1])
                    else:
                        dp[i + 1][j + 1][1] = dp[i + 1][j][1]

                if dp[i + 1][j][2] is not None:
                    if dp[i + 1][j + 1][2] is not None:
                        dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][2])
                    else:
                        dp[i + 1][j + 1][2] = dp[i + 1][j][2]

                if dp[i + 1][j][3] is not None:
                    if dp[i + 1][j + 1][3] is not None:
                        dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][3])
                    else:
                        dp[i + 1][j + 1][3] = dp[i + 1][j][3]

    res = 0
    for d in dp[R][C]:
        if d is not None:
            res = max(res, d)

    print(res)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
R, C, K = map(int, input().split(" "))
board = [ [0]*(C+1) for _ in range(R+1) ]
for _ in range(K) :
    i, j, v = map(int, input().split(" "))
    board[i][j] = v

score = [ [ [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) : 
        
        descent = max(score[i-1][j])

        score[i][j][3] = max(score[i][j-1][3], score[i][j-1][2] + board[i][j])
        score[i][j][2] = max(score[i][j-1][2], score[i][j-1][1] + board[i][j])
        score[i][j][1] = max(descent + board[i][j], score[i][j-1][1], score[i][j-1][0]+board[i][j])
        score[i][j][0] = max(descent, score[i][j-1][0])

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

Submission status: Time Limit Exceeded

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+1)] for _ in range(R+1)]
for x in range(R):
    for y in range(C):
        cost[x-1][y] = max(cost[x-1][y])
        for z in range(4):
            if z == 0:
                cost[x][y][z] = max(cost[x][y-1][z],cost[x-1][y]) 
            else:
                cost[x][y][z] = max(cost[x][y-1][z],cost[x][y-1][z-1]+M[x][y],cost[x-1][y]+M[x][y])

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
(r,c,_),*d = [list(map(int, s.split())) for s in open(0)]

v = dict()
for dd in d:
    v[dd[0]-1, dd[1]-1] = dd[2]

dp = np.zeros([r,4], dtype=int)
for i in range(c):
    for j in range(r):
        if j > 0:
            dp[j,0] = max(dp[j,0], max(dp[j-1]))
        if v.get((j,i), False):
            dp[j,1:] = np.maximum(dp[j,1:], dp[j,:-1] + v[(j,i)])
print(np.max(dp))

Submission status: Time Limit Exceeded

Solution code:
r,c,k=map(int,input().split())
item=[[0]*c for i in range(r)]
for i in range(k):
    x,y,z=map(int,input().split())
    item[x-1][y-1]=z
dp=[[[0,0,0,0] for j in range(c)] for i in range(r)]
if item[0][0]>0:
    dp[0][0]=[0,item[0][0],0,0]
for i in range(r):
    for j in range(c):
        for l in range(4):
            if l<3:
                #した行く分にはOK
                if i!=r-1:
                    dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][l])
                    if item[i+1][j]>0:
                        dp[i+1][j][1]=max(dp[i+1][j][0],dp[i][j][l])+item[i+1][j]
                #右は？
                if j!=c-1:
                    dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j][l])
                    if item[i][j+1]>0:
                        dp[i][j+1][l+1]=max(dp[i][j+1][l+1],dp[i][j][l]+item[i][j+1])
            else:
                #した行く分にはOK
                if i!=r-1:
                    dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][l])
                    if item[i+1][j]>0:
                        dp[i+1][j][1]=max(dp[i+1][j][0],dp[i][j][l])+item[i+1][j]
                #右は？
                if j!=c-1:
                    dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j][l])
print(max(dp[r-1][c-1]))

Submission status: Time Limit Exceeded

Solution code:
R,C,K=map(int,input().split())
RCV=[list(map(int,input().split())) for i in range(K)]
V=[[0]*(C+1) for i in range(R+1)]
for r,c,v in RCV:
    V[r][c]=v
d=[[[0]*4 for i in range(C+1)] for i in range(R+1)]
for i in range(R):
    for j in range(C):
        d[i+1][j+1][0]=max(d[i][j+1])
        for k in range(3):
            d[i+1][j+1][k+1]=max(d[i+1][j][k+1],max(d[i][j+1])+V[i+1][j+1],d[i+1][j][k]+V[i+1][j+1])
print(max(d[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

rows, cols, k = [int(z) for z in input().strip().split()]
value = np.zeros((rows, cols), dtype=np.int)
dp = np.zeros((rows, cols, 4), dtype=np.int)
for i in range(k):
    r, c, v = [int(z) for z in input().strip().split()]
    value[r - 1, c - 1] = v

if value[0, 0] > 0:
    dp[0, 0, 1] = value[0, 0]

for r in range(1, rows):
    dp[r, 0, 0] = max(dp[r - 1, 0, 0], dp[r - 1, 0, 1], dp[r - 1, 0, 2], dp[r - 1, 0, 3])
    if value[r, 0] > 0:
        dp[r, 0, 1] = dp[r, 0, 0] + value[r, 0]

for c in range(1, cols):
    dp[0, c, 0] = dp[0, c - 1, 0]
    if value[0, c] > 0:
        dp[0, c, 1] = max(dp[0, c - 1, 1], dp[0, c - 1, 0] + value[0, c])
        dp[0, c, 2] = max(dp[0, c - 1, 2], dp[0, c - 1, 1] + value[0, c])
        dp[0, c, 3] = max(dp[0, c - 1, 3], dp[0, c - 1, 2] + value[0, c])
    else:
        dp[0, c, 1] = dp[0, c - 1, 1]
        dp[0, c, 2] = dp[0, c - 1, 2]
        dp[0, c, 3] = dp[0, c - 1, 3]

for r in range(1, rows):
    for c in range(1, cols):
        up = max(dp[r - 1, c, 0], dp[r - 1, c, 1], dp[r - 1, c, 2], dp[r - 1, c, 3])
        dp[r, c, 0] = max(dp[r, c - 1, 0], up)
        if value[r, c] > 0:
            dp[r, c, 1] = max(dp[r, c - 1, 1], dp[r, c - 1, 0] + value[r, c], up + value[r, c])
            dp[r, c, 2] = max(dp[r, c - 1, 2], dp[r, c - 1, 1] + value[r, c])
            dp[r, c, 3] = max(dp[r, c - 1, 3], dp[r, c - 1, 2] + value[r, c])
        else:
            dp[r, c, 1] = dp[r, c - 1, 1]
            dp[r, c, 2] = dp[r, c - 1, 2]
            dp[r, c, 3] = dp[r, c - 1, 3]

print(int(np.max(dp[rows - 1, cols - 1, :])))

Submission status: Time Limit Exceeded

Solution code:
from numba import njit

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

rcvs.sort(key = lambda t: t[1] * 30000 + t[0])

@njit()
def solve(rcvs):
    dp = [[0] * (R+1) for _ in range(4)]
    index = 0
    for c in range(C+1):
        while index < K and rcvs[index][1] == c:
            x, _, v = rcvs[index]
            if dp[3][x] < dp[2][x] + v:
                dp[3][x] = dp[2][x] + v
            if dp[2][x] < dp[1][x] + v:
                dp[2][x] = dp[1][x] + v
            if dp[1][x] < dp[0][x] + v:
                dp[1][x] = dp[0][x] + v
            index += 1
            for r in range(x, R):
                dp[0][r+1] = max(dp[0][r], dp[1][r], dp[2][r], dp[3][r])
            #print(dp)   
        
    ans = max([max(row) for row in dp])
    return ans

print(solve(rcvs))