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

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

V = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0,0,0,0] for _ in range(C+1)]

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

for r in range(1,R+1):
    dp0 = dp1
    dp1 = [[0,0,0,0] for _ in range(C+1)]
    
    for c in range(1,C+1):
        v = V[r][c]
        dprc  = dp1[c]
        dprcL = dp1[c-1]
        if v==0:
            dprc[0] = max(max(dp0[c]), dprcL[0])
            for n in range(1,4): dprc[n] = dprcL[n]
        else:
            maxT = max(dp0[c])
            dprc[0] = max(maxT  ,             dprcL[0])
            dprc[1] = max(maxT+v, dprcL[0]+v, dprcL[1])
            dprc[2] = max(        dprcL[1]+v, dprcL[2])
            dprc[3] = max(        dprcL[2]+v, dprcL[3])
        

print(max(dp1[C]))

Solution (Python):
import sys
input = sys.stdin.readline

h, w, k = map(int, input().split())
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = map(int, input().split())
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*w for i in range(h)] for j in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[0][i][j] = dp[0][i][j-1]
      for k in range(1, 4):
        dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1]+G[i][j], dp[k][i][j-1])
    if i:
      dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j]))
      dp[1][i][j] = max(dp[1][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])+G[i][j])
ans = max(dp[0][h-1][w-1],dp[1][h-1][w-1],dp[2][h-1][w-1],dp[3][h-1][w-1])
print(ans)

Solution (Python):
import sys

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

    
    R,C,K = map(int, input().split())
    
    
    base=5000
    S=[]
    for i in range(K):
        r,c,v = map(int, input().split())
        r-=1
        c-=1
        S.append((r*base+c,v))

    S.sort(key=itemgetter(0))


    p=0
    dp=[0]*C
    for i in range(R):
        dpi=[[0] * 4 for _ in range(C)]
        for j in range(C):
            cv=0
            
            if p<K and i*base+j==S[p][0]:
                cv=S[p][1]
                p+=1
            if j>0:
                dpi[j]=copy(dpi[j-1])
            if i>0:
                dpi[j][0]=max(dp[j],dpi[j][0])
            if cv!=0:
                for k in range(2,-1,-1):
                    dpi[j][k+1]=max(dpi[j][k]+cv,dpi[j][k+1])
                
            dp[j]=max(dpi[j])
    print(dp[-1])



if __name__ == "__main__":
    main()

Solution (Python):
R, C, K = map(int,input().split())
V = [[]for i in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    V[r-1].append([c,v])
cost = [0]*(C+1)
for r in range(R):
    V[r].sort()
    hoge = [0,0,0,0]
    for c,v in V[r]:
        for i in range(3,0,-1):
            hoge[i] = max(cost[c]+v,hoge[i-1]+v,hoge[i])
        hoge[0] = cost[c]
        cost[c] = max(hoge)
    for i in range(1,C+1):
        cost[i] = max(cost[i],cost[i-1])

print(cost[-1])

Solution (Python):
import sys
input = sys.stdin.readline
R, C, ite = map(int, input().split())
items = [[0 for _ in range(C)] for _ in range(R)]
for _ in range(ite):
    r, c, v = map(int, input().split())
    items[r-1][c-1] = v

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

    new_dp = [[0] * 4 for _ in range(C)]
    for j in range(C):
        new_dp[j][0] = max(dp[j])
        if items[i+1][j] > 0:
            new_dp[j][1] = max(dp[j]) + items[i+1][j]

        dp[j] = new_dp[j].copy()

        
print(max(dp[-1]))

Solution (Python):
from collections import defaultdict
R, C, K = map(int, input().split())
rc2v = defaultdict(int)
for _ in range(K):
    r, c, v = map(int, input().split())
    r-=1
    c-=1
    rc2v[(r, c)]=v

dp = [0] * C
for r in range(R):
    dp_ = [[0] * C for _ in range(4)]
    for c in range(C):
        dp_[3][c] = max(dp_[3][c-1], dp[c])
        dp_[2][c] = dp_[2][c-1]
        dp_[1][c] = dp_[1][c-1]
        dp_[0][c] = dp_[0][c-1]
        if (r, c) in rc2v:
            dp_[0][c] = max(dp_[0][c-1], dp_[1][c]+rc2v[(r, c)])
            dp_[1][c] = max(dp_[1][c-1], dp_[2][c]+rc2v[(r, c)])
            dp_[2][c] = max(dp_[2][c-1], dp_[3][c]+rc2v[(r, c)])
        dp[c] = max(dp_[3][c], dp_[2][c], dp_[1][c], dp_[0][c])

print(dp[-1])

Solution (Python):
def resolve():
    r, c, k = map(int, input().split())
    dict = {}
    for i in range(k):
        a,b,z = map(int, input().split())
        a -= 1
        b -= 1
        dict[(a,b)] = z
    now = [[0]*4 for _ in range(c)]
    prev = [0]*c

    if (0,0) in dict:
        now[0][1] = dict[(0,0)]
    for i in range(1, c):
        if (0, i) in dict:
            now[i][0] = now[i-1][0]
            now[i][1] = max(dict[(0,i)]+now[i-1][0] , now[i-1][1])
            if now[i-1][1] != 0:
                now[i][2] = max(dict[(0,i)]+now[i-1][1], now[i-1][2])
            if now[i - 1][2] != 0:
                now[i][3] = max(dict[(0,i)]+now[i-1][2], now[i-1][3])
        else:
            now[i][0] = now[i-1][0]
            now[i][1] = now[i-1][1]
            now[i][2] = now[i-1][2]
            now[i][3] = now[i-1][3]
    for i in range(c):
        prev[i] = max(now[i])
    for i in range(1, r):
        now = [[0] * 4 for _ in range(c)]
        now[0][0] = prev[0]
        if (i, 0) in dict:
            now[0][1] = dict[(i,0)]+now[0][0]

        for j in range(1, c):
            now[j][0] = max(now[j-1][0], prev[j])

            if (i,j) in dict:
                if now[j-1][2] != 0:
                    a3 = now[j-1][2]+dict[(i,j)]
                else:
                    a3 = 0
                if now[j][2] != 0:
                    a4 = now[j][2]+dict[(i,j)]
                else:
                    a4 = 0

                if now[j-1][1] != 0:
                    a1 = now[j-1][1]+dict[(i,j)]
                else:
                    a1 = 0
                if now[j][1] != 0:
                    a2 = now[j][1]+dict[(i,j)]
                else:
                    a2 = 0
                now[j][3] = max(now[j - 1][3], a3, a4)
                now[j][2] = max(now[j-1][2], a1, a2)
                now[j][1] = max(now[j-1][1], now[j-1][0] + dict[(i,j)],now[j][0] + dict[(i,j)])
            else:
                now[j][1] = now[j-1][1]
                now[j][2] = now[j-1][2]
                now[j][3] = now[j-1][3]
        prev = [0]*c

        for j in range(c):
            prev[j] = max(now[j])
    print(prev[-1])
resolve()

Solution (Python):

import sys
def main():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    M = [[0] * C for _ in range(R)]
    for _ in range(K):
        r, c, v = map(int, input().split())
        M[r - 1][c - 1] = v

    
    dp = [[-1] * 4 for _ in range(C)]
    dp[0][0] = 0
    dp[0][1] = M[0][0]
    for i in range(R):
        nxt = [[-1] * 4 for _ in range(C)]
        for j in range(C):
            now = dp[j]
            for k in range(4):
                nowk = now[k]
                if nowk < 0: continue
                
                if i < R - 1:
                    down = nxt[j]
                    
                    down[0] = max(down[0], nowk)
                    
                    v = M[i + 1][j]
                    if v > 0:
                        down[1] = max(down[1], nowk + v)
                
                if j < C - 1:
                    right = dp[j + 1]
                    
                    right[k] = max(right[k], nowk)
                    
                    v = M[i][j + 1]
                    if v > 0 and k < 3:
                        right[k + 1] = max(right[k + 1], nowk + v)
        if i < R - 1: dp = nxt
    print(max(dp[C - 1]))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

OFFSET = (r + 1)
dp = [[-INF] * ((c + 1) * (r + 1)) for i in range(4)]
for i in range(r + 1):
    dp[0][i] = 0
for j in range(c + 1):
    dp[0][OFFSET * j] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        tmp11 = i + 1 + OFFSET * (j + 1)
        tmp = i + 1 + OFFSET * j
        tmp01 = i + OFFSET * (j + 1)

        dp[0][tmp11] = max(dp[0][tmp], dp[0][tmp01], dp[1][tmp01], dp[2][tmp01], dp[3][tmp01])
        dp[1][tmp11] = max(dp[1][tmp], dp[0][tmp] + val,  dp[0][tmp01] + val, dp[1][tmp01] + val, dp[2][tmp01] + val, dp[3][tmp01] + val)
        dp[2][tmp11] = max(dp[2][tmp], dp[1][tmp] + val)
        dp[3][tmp11] = max(dp[3][tmp], dp[2][tmp] + val)


print(max(dp[0][(c + 1) * (r + 1) - 1], dp[1][(c + 1) * (r + 1) - 1], dp[2][(c + 1) * (r + 1) - 1], dp[3][(c + 1) * (r + 1) - 1]))

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
r,c,k = map(int,readline().split())
items = [[0]*c for _ in range(r)]
for i in range(k):
    a,b,v = map(int,readline().split())
    a,b = a-1,b-1
    items[a][b] = v

dp0 = [[0]*(c+1) for j in range(r+1)]
dp1 = [[0]*(c+1) for j in range(r+1)]
dp2 = [[0]*(c+1) for j in range(r+1)]
dp3 = [[0]*(c+1) for j in range(r+1)]

for i in range(1,r+1):
    for j in range(1,c+1):
        
        
        dp1[i][j] = max(dp0[i][j]+items[i-1][j-1],dp1[i][j-1],dp0[i][j-1]+items[i-1][j-1])
        dp2[i][j] = max(dp2[i][j-1],dp1[i][j-1]+items[i-1][j-1])
        dp3[i][j] = max(dp3[i][j-1],dp2[i][j-1]+items[i-1][j-1])
        if i < r:
            dp0[i+1][j] = max(dp0[i][j],dp1[i][j],dp2[i][j],dp3[i][j])
        

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    
    r, c, k = map(int, input().split())
    tizu = [0 for _ in range(r * c)]
    for _ in range(k):
        rr, cc, v = map(int, input().split())
        tizu[(rr - 1) * c + (cc - 1)] = v
        
    dp = [[0, 0, 0, 0] for _ in range(c)]
    dp[0][1] = tizu[0]
    
    for i in range(r):
        for j in range(c):
            v = tizu[i * c + j]
            if v:
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, tmp + v, 0, 0]
                if j != 0:
                    tmp0 = dp[j - 1][0]
                    tmp1 = dp[j - 1][1]
                    tmp2 = dp[j - 1][2]
                    lst = [max(dp[j][0], tmp0), max(dp[j][1], tmp1, tmp0 + v), max(tmp2, tmp1 + v), max(dp[j - 1][3], tmp2 + v)]
                    dp[j] = lst
            else:
                if i != 0:
                    tmp = max(dp[j])
                    dp[j] = [tmp, 0, 0, 0]
                if j != 0:
                    lst = [max(dp[j][0], dp[j-1][0]), dp[j-1][1], dp[j-1][2], dp[j - 1][3]]
                    dp[j] = lst
    print(max(dp[-1]))
    
main()

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

rcv.sort()
rcv.append([R, C, 0])

L = [0] * C
su = 0
n = 0
vv = 0
D = [0, 0, 0]
for i in range(K):
  r, c, v = rcv[i]
  if r != 1: break
  for j in range(3):
    if D[j] < v:
      D[j], v = v, D[j]
  L[c - 1] = sum(D)
n = i

for i in range(1, C):
  L[i] = max(L[i], L[i - 1])

for i in range(2, R + 1):
  L2 = L
  L = [0] * C
  D = [0, 0, 0]
  DP = []
  ln = 0
  for n in range(n, K):
    r, c, v = rcv[n]
    if r != i: break
    if len(DP) == 0:
      L[c - 1] = v + L2[c - 1]
      D[0] = L[c - 1]
      DP.append([L[c - 1], 0, 0])
      continue

    DP.append([0, 0, 0])
    DP[-1][0] = v + L2[c - 1]
    for k in range(3):
      DP[-1][k] = max(DP[-1][k], DP[-2][k])
      if k == 0: continue
      DP[-1][k] = max(DP[-1][k], DP[-2][k - 1] + v)
    L[c - 1] = max(DP[-1])
  L[0] = max(L[0], L2[0])
  for j in range(1, C):
    L[j] = max(L[j], L[j - 1], L2[j])

print(max(L))

Solution (Python):

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)]
prev_dp=[[0]*4 for _ in range(C)]
for y in range(R):
    for x in range(C):
        for i in range(2,-1,-1):
            dp[x][i+1]=max(dp[x][i+1],dp[x][i]+V[y][x])
        if x<C-1:
            for i in range(3,-1,-1):
                dp[x+1][i]=max(dp[x+1][i],dp[x][i])
    if y+1<R:
        prev_dp=dp
        dp=[[0]*4 for _ in range(C)]
        for x in range(C):
            dp[x][0]=max(prev_dp[x])
print(max(dp[-1]))

Solution (Python):

max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def solve(R,C,rcv):

    rows = [list() for _ in range(R)]
    for r,c,v in rcv:
        rows[r-1].append((c-1,v))

    dp = [0]*C

    for row in rows:
        row.sort()
        row.append((C,0))
        ri = 0
        c,v = row[ri]

        t1 = 0
        t2 = 0
        t3 = 0
        for j in range(C):
            if c == j:
                t3 = max2(t2+v,t3)
                t2 = max2(t1+v,t2)
                t1 = max2(dp[j]+v,t1)
                ri += 1
                c,v = row[ri]
            
            dp[j] = max(t3,t2,t1,dp[j])

    return dp[-1]




if __name__ == '__main__':
    R,C,K = map(int,readline().split())
    m = map(int,read().split())
    print(solve(R,C,zip(m,m,m)))

Solution (Python):
import sys
input = sys.stdin.buffer.readline

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

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

    for r in range(R):
        ndp = [[0]*4 for _ in range(C+1)]
        for c in range(C):
            ndp[c+1][0] = max(ndp[c+1][0], max(dp[c+1]))
            ndp[c+1][1] = ndp[c+1][0] + State[c][r]
            for n in range(4):
                if ndp[c][n] > ndp[c+1][n]:
                    ndp[c+1][n] = ndp[c][n]
                if n < 3 and ndp[c][n] + State[c][r] > ndp[c+1][n+1]:
                    ndp[c+1][n+1] = ndp[c][n] + State[c][r]
        dp = ndp
        
    print(max(dp[C]))

if __name__ == "__main__":
    main()

Solution (Python):
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 = list(map(int,read().split()))
d = dict()
it = iter(data)
for ri,ci,vi in zip(it,it,it):
    d[(ri-1)*5000 + (ci-1)] = vi

dp_max = [0] * (c)

for i in range(r):
    dp = [[0,0,0,0] for _ in range(4)]
    for j in range(c):
        dp[0][0] = max(dp[0][0],dp_max[j])
        if( i*5000+j  in d):
            ci = d[i*5000+j]
            
            if(dp[3][0] != 0):
                min_num = min(dp[3][1:4])
                if(min_num < ci):
                    dp[3][0] += ci-min_num
                    for k in range(1,4):
                        if(dp[3][k] == min_num):
                            dp[3][k] = ci
                            break
            if(dp[2][0] != 0):
                if(dp[3][0] <= dp[2][0] + ci):
                    dp[3][0] = dp[2][0] + ci
                    dp[3][1] = dp[2][1]
                    dp[3][2] = dp[2][2]
                    dp[3][3] = ci

            
            if(dp[1][0] != 0):
                if(dp[2][0] <= dp[1][0] + ci):
                    dp[2][0] = dp[1][0] + ci
                    dp[2][1] = dp[1][1]
                    dp[2][2] = ci
            
            if(dp[1][0] <= dp[0][0] + ci):
                dp[1][0] = dp[0][0] + ci
                dp[1][1] = ci
        
        

        dp_max[j] = max(dp[0][0], dp[1][0],dp[2][0],dp[3][0])

    
    

print(dp_max[-1])

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
r,c,k=map(int,input().split())
dp=[[(c+1)*[0]for _ in range(r+1)]for _ in range(4)]
p=[c*[0]for _ in range(r)]
for _ in range(k):
  rr,cc,u=map(int,input().split())
  rr-=1
  cc-=1
  p[rr][cc]=u
ans=0

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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

r, c, k = LI()
maze = [[0] * c for _ in range(r)]
for _ in range(k):
    R, C, V = LI()
    maze[R-1][C-1] = V

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):
        dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + maze[i-1][j-1], maze[i-1][j-1], dp2[i-1][j] + maze[i-1][j-1], dp3[i-1][j] + maze[i-1][j-1])
        dp2[i][j] = max(dp2[i][j-1], dp2[i-1][j] + maze[i-1][j-1], dp1[i][j-1] + maze[i-1][j-1])
        dp3[i][j] = max(dp3[i][j-1], dp3[i-1][j] + maze[i-1][j-1], dp2[i][j-1] + maze[i-1][j-1])
ans = max(dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])
print(ans)

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    r,c,k = map(int,ipt().split())
    pts = [[0]*c for i in range(r)]
    for i in range(k):
        ri,ci,ki = map(int,ipt().split())
        pts[ri-1][ci-1] = ki

    dp = [0]*c
    for i in range(r):
        ndp = [0,0,0,0]
        ndpp = []
        for j in range(c):
            if ndp[0] < dp[j]:
                ndp[0] = dp[j]
            for l in range(3,0,-1):
                if ndp[l] < ndp[l-1]+pts[i][j]:
                    ndp[l] = ndp[l-1]+pts[i][j]
            ndpp.append(max(ndp))
        dp = ndpp*1
    print(dp[c-1])



    return None

if __name__ == '__main__':
    main()

Solution (Python):
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 19 + 7
EPS = 10 ** -10

H, W, K = MAP()
grid = list2d(H+1, W+1, 0)
for i in range(K):
    h, w, v = MAP()
    grid[h][w] = v

dp0 = list2d(H+1, W+1, -INF)
dp1 = list2d(H+1, W+1, -INF)
dp2 = list2d(H+1, W+1, -INF)
dp3 = list2d(H+1, W+1, -INF)
dp0[0][1] = dp0[1][0] = 0
for h in range(1, H+1):
    for w in range(1, W+1):
        dp0[h][w] = max(dp0[h][w-1], dp0[h-1][w], dp0[h-1][w], dp1[h-1][w],  dp2[h-1][w], dp3[h-1][w])
        dp1[h][w] = dp1[h][w-1]
        dp2[h][w] = dp2[h][w-1]
        dp3[h][w] = dp3[h][w-1]
        if grid[h][w]:
            dp1[h][w] = max(dp1[h][w], max(dp0[h][w-1], dp0[h-1][w], dp1[h-1][w], dp2[h-1][w], dp3[h-1][w]) + grid[h][w])
            dp2[h][w] = max(dp2[h][w], dp1[h][w-1] + grid[h][w])
            dp3[h][w] = max(dp3[h][w], dp2[h][w-1] + grid[h][w])
ans = max(dp0[H][W], dp1[H][W], dp2[H][W], dp3[H][W])
print(ans)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    R, C, K = map(int, input().split())
    item = [[0 for _ in range(C)] for r in range(R)]
    for _ in range(K):
        r, c, k = map(int, input().split())
        item[r-1][c-1] = k
    
    DP = [[[-1 for _ in range(C)] for r in range(R)] for i in range(4)]
    DP[0][0][0] = 0
    for r in range(R):
        for c in range(C):
            for i in range(4):
                if DP[i][r][c] > -1:
                    if r + 1 < R: 
                        DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c])
                    if c + 1 < C: 
                        DP[i][r][c+1] = max(DP[i][r][c+1], DP[i][r][c])
                    if i < 3 and item[r][c] > 0:
                        if r + 1 < R: DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c] + item[r][c])
                        if c + 1 < C: DP[i+1][r][c+1] = max(DP[i+1][r][c+1], DP[i][r][c] + item[r][c])

    ans = DP[3][R-1][C-1]
    for i in range(3): ans = max(ans, DP[i][R-1][C-1] + item[R-1][C-1])
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    R, C, K = map(int, input().split())
    d = dict()
    for i in range(K):
        r, c, v = map(int, input().split())
        r -= 1
        c -= 1
        d[(r, c)] = v

    
    prev = [0 for i in range(C)]
    DP = [[0, -1, -1, -1] for i in range(C)]
    for i in range(R):
        DP[0][0] = prev[0]
        if (i, 0) in d:
            DP[0][1] = d[(i, 0)] + prev[0]

        prev[0] = max(DP[0])

        for j in range(1, C):
            if (i, j) in d:
                for k in range(3):
                    if DP[j-1][k] == -1:
                        break
                    else:
                        DP[j][k+1] = max(DP[j-1][k+1], DP[j-1]
                                         [k] + d[(i, j)], prev[j] + d[(i, j)])
            else:
                for k in range(1, 4):
                    DP[j][k] = DP[j-1][k]

            DP[j][0] = max(DP[j-1][0], prev[j])

            prev[j] = max(DP[j])
        
    print(prev[-1])


main()

Solution (Python):
def main():
    R, C, K = map(int, input().split())
    V = {}
    for _ in range(K):
        r, c, v = map(int, input().split())
        V[(r - 1, c - 1)] = v
    dp = [(0, 0, 0, 0) for _ in range(C)]
    for r in range(R):
        p0, p1, p2, p3 = 0, 0, 0, 0
        for c in range(C):
            m = max(dp[c])
            if (r, c) in V:
                v = V[(r, c)]
                n0, n1, n2, n3 = max(m, p0), max(p1, max(p0, m) + v), max(p2, p1 + v), max(p3, p2 + v)
            else:
                n0, n1, n2, n3 = max(m, p0), p1, p2, p3
            dp[c] = (n0, n1, n2, n3)
            p0, p1, p2, p3 = n0, n1, n2, n3

    return max(dp[-1])

print(main())

Solution (Python):
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 and table[x]:
        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: ]))

Solution (Python):
def main():
    R, C, K = map( int, input().split())
    V = [[0]*C for _ in range(R)]
    for _ in range(K):
        r, c, v = map( int, input().split())
        V[r-1][c-1] = v
    dp0 = [[0]*(C+1) for _ in range(R+1)]
    dp1 = [[0]*(C+1) for _ in range(R+1)]
    dp2 = [[0]*(C+1) for _ in range(R+1)]
    dp3 = [[0]*(C+1) for _ in range(R+1)]
    for r in range(1,R+1):
        m = 0
        for c in range(1,C+1):
            m = max(m,dp0[r-1][c], dp1[r-1][c], dp2[r-1][c], dp3[r-1][c])
            dp0[r][c] = max(m, dp0[r][c-1])
            dp1[r][c] = max(dp1[r][c-1], dp0[r][c]+V[r-1][c-1])
            dp2[r][c] = max(dp2[r][c-1], dp1[r][c-1]+V[r-1][c-1])
            dp3[r][c] = max(dp3[r][c-1], dp2[r][c-1]+V[r-1][c-1])
    
    
    
    
    print( max( max(dp0[R]), max(dp1[R]), max(dp2[R]), max(dp3[R])))
if __name__ == '__main__':
    main()

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

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

for r,c,v in RCV:
    V[r][c] = v

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

for r in range(1,R+1):
    for c in range(C+1):
        ans[0][r][c] = max(ans[0][r-1][c],ans[1][r-1][c],ans[2][r-1][c],ans[3][r-1][c])
    i = 1
    for c in range(1,C+1):
        ans[i][r][c] = max(ans[i][r][c],ans[i][r][c-1],ans[i-1][r][c]+V[r][c])
    for i in range(2,4):
        for c in range(1,C+1):
            ans[i][r][c] = max(ans[i][r][c],ans[i][r][c-1],ans[i-1][r][c-1]+V[r][c])

out = max(ans[0][-1][-1],ans[1][-1][-1],ans[2][-1][-1],ans[3][-1][-1])
print(out)

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    h, w, k = nm()
    d = dict()
    for _ in range(k):
        r, c, v = nm()
        d[r * 10000 + c - 10001] = v
    prv = [[0]*4 for _ in range(w+1)]
    nxt = [[0]*4 for _ in range(w+1)]
    for i in range(h):
        for j in range(w):
            f = d.get(i * 10000 + j, 0)
            t = max(prv[j])
            for v in range(4):
                nxt[j][v] = max(nxt[j-1][v], t)
                if v:
                    nxt[j][v] = max(nxt[j][v], nxt[j-1][v-1] + f, t + f)
        prv, nxt = nxt, prv
    print(max(prv[w-1]))

    return

solve()