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: Runtime Error

Solution code:
import sys


class Solution:
    def dp(self, r, c, cnt):
        if r == self.r:
            return 0
        elif self.rcd[r][c][cnt] != -1:
            return self.rcd[r][c][cnt]
        else:
            maxv = 0
            if self.matrix[r][c] and cnt:
                # take
                if c + 1 < self.c:
                    maxv = max(maxv, self.dp(r, c + 1, cnt - 1) + self.matrix[r][c])
                maxv = max(maxv, self.dp(r + 1, c, 3) + self.matrix[r][c])
            # or not take
            if c + 1 < self.c:
                maxv = max(maxv, self.dp(r, c + 1, cnt))
            maxv = max(maxv, self.dp(r + 1, c, 3))
            self.rcd[r][c][cnt] = maxv
            return maxv

    def run(self):
        self.r, self.c, self.k = map(int, input().split())
        self.matrix = [[0 for _ in range(self.c)] for _ in range(self.r)]
        for _ in range(self.k):
            r, c, v = map(int, input().split())
            self.matrix[r - 1][c - 1] = v

        self.inf = sys.maxsize
        self.rcd = [[[-1 for _ in range(4)] for _ in range(self.c)] for _ in range(self.r)]
        print(self.dp(0, 0, 3))


if __name__ == '__main__':
    solution = Solution()
    solution.run()

Submission status: Runtime Error

Solution code:
import copy
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(4):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = copy.deepcopy(dp[0])
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]
dp[1][0][0] = location[0][0]
if R > 1:
   for r in range(R-1):
      dp[1][r+1][0] = location[r][0] + dp[1][r][0]
      dp[0][r+1][0] = max(dp[1][r][0],dp[0][r][0])
if C > 1:
   for c in range(C-1):
         dp[1][0][c+1] = max(location[0][c+1] + dp[0][0][c], dp[1][0][c])
         dp[2][0][c+1] = max(location[0][c+1] + dp[1][0][c], dp[2][0][c])
         dp[3][0][c+1] = max(location[0][c+1] + dp[2][0][c], dp[3][0][c])
   if R > 1:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1], dp[2][r][c+1], dp[3][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c+1] + location[r+1][c+1], dp[1][r+1][c])
            dp[2][r+1][c+1] = max(dp[1][r+1][c] + location[r+1][c+1], dp[2][r+1][c])
            dp[3][r+1][c+1] = max(dp[2][r+1][c] + location[r+1][c+1], dp[3][r+1][c])
if R==1 and C==1:
   ans = i[2]
else:
   ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1], dp[3][R-1][C-1])
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

r, c, k = map(int, input().split())
itm = [[0]*(c) for _ in range(r)]
for i in range(k):
    ri, ci, vi = map(int, input().split())
    itm[ri-1][ci-1] = vi

dp = np.zeros((3005, 3005, 4), dtype=np.int64)

for i in range(r):
    for j in range(c):
        nowv = itm[i][j]
        dp[i][j][3] = max(dp[i][j][3], dp[i][j][2] + nowv)
        dp[i][j][2] = max(dp[i][j][2], dp[i][j][1] + nowv)
        dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + nowv)

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

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

Submission status: Runtime Error

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

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__X = H * W
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

def main():
    r,c,k=map(int,input().split())
    v = list2D(r, c, 0)

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            if i>0:
                x = 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]=max(dp[i, j, 1],x+val)
                dp[i, j, 0]=max(dp[i, j, 0],x)

            if j>0:
                X = dp[i, j-1, 0]
                Y = dp[i, j-1, 1]
                V = dp[i, j-1, 2]
                Z = dp[i, j-1, 3]
                dp[i, j, 0]=max(dp[i, j, 0],X)
                dp[i, j, 1]=max(dp[i, j, 1],X+val,Y)
                dp[i, j, 2]=max(dp[i, j, 2],Y+val,V)
                dp[i, j, 3]=max(dp[i, j, 3],V+val,Z)

    #print(dp)
    ans=0
    for i in range(4): ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
/**
 *  author: Haunted_Cpp
**/
 
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
 
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
 
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
 
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif

const int MAX_N = 3e3 + 5;
const int MAX_K = 5;

long long dp[MAX_N][MAX_N][MAX_K];

int r, c, k;

int g[MAX_N][MAX_N];

long long solve(int linha, int coluna, int how_many) {
  if (linha == r - 1 && coluna == c - 1) {
    if (how_many + 1 <= 3) return g[linha][coluna];
    return 0;
  }
  if (linha >= r) return -1e18;
  if (coluna >= c) return -1e18;
  long long &res = dp[linha][coluna][how_many];
  if (~res) return res;
  res = -1e18;
  res = max(res, solve(linha + 1, coluna, 0));
  res = max(res, solve(linha, coluna + 1, 0));
  if (how_many + 1 <= 3) {
    res = max(res, solve(linha + 1, coluna, 0) + g[linha][coluna]);
    res = max(res, solve(linha, coluna + 1, how_many + 1) + g[linha][coluna]);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); 
  cin >> r >> c >> k;
  for (int i = 0; i < k; i++) {
    int x, y, w;
    cin >> x >> y >> w; 
    --x; --y;
    g[x][y] = w;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0, 0) << '\n';
  return 0;
}

Submission status: Runtime Error

Solution code:
import copy
R, C, K = map(int,input().split())
i = [list(map(int,input().split())) for k in range(K)]
dp = []
for k in range(4):
   dp.append([])
   for r in range(R):
      dp[k].append([])
      for c in range(C):
         dp[k][r].append([])
         dp[k][r][c] = 0
location = copy.deepcopy(dp[0])
for k in range(K):
   location[i[k][0]-1][i[k][1]-1] = i[k][2]
dp[1][0][0] = location[0][0]
if R > 1:
   for r in range(R-1):
      dp[1][r+1][0] = location[r][0] + max(dp[1][r][0],dp[0][r][0])
      dp[0][r+1][0] = max(dp[1][r][0],dp[0][r][0])
if C > 1:
   for c in range(C-1):
         dp[1][0][c+1] = max(location[0][c+1] + dp[0][0][c], dp[1][0][c])
         dp[2][0][c+1] = max(location[0][c+1] + dp[1][0][c], dp[2][0][c])
         dp[3][0][c+1] = max(location[0][c+1] + dp[2][0][c], dp[3][0][c])
   if R > 1:
      for r in range(R-1):
         for c in range(C-1):
            dp[0][r+1][c+1] = max(dp[0][r+1][c], dp[0][r][c+1], dp[1][r][c+1], dp[2][r][c+1], dp[3][r][c+1])
            dp[1][r+1][c+1] = max(dp[0][r+1][c+1] + location[r+1][c+1], dp[1][r+1][c])
            dp[2][r+1][c+1] = max(dp[1][r+1][c] + location[r+1][c+1], dp[2][r+1][c])
            dp[3][r+1][c+1] = max(dp[2][r+1][c] + location[r+1][c+1], dp[3][r+1][c])
if R==1 and C==1:
   ans = i[2]
else:
   ans = max(dp[0][R-1][C-1], dp[1][R-1][C-1], dp[2][R-1][C-1], dp[3][R-1][C-1])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
R,C,K = map(int,input().split())
a = np.zeros((R+5,C+5),dtype=np.int)
dp = np.zeros((R+5,C+5,4),dtype=np.int)
def chmax(x,y):
    x = max(x,y)
    return x

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

for i in range(R):
    for j in range(C):
        for k in reversed(range(3)):
            #chmax(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            dp[i][j][k+1] = max(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            
        for k in range(4):
            #chmax(dp[i+1][j][0],dp[i][j][k])
            #chmax(dp[i][j+1][k],dp[i][j][k])
            dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k],dp[i][j][k])

ans = 0
for i in range(4):
    #chmax(ans,dp[R-1][C-1][i])
    ans =  max(ans,dp[R-1][C-1][i])
print(ans)

Submission status: Runtime Error

Solution code:
R,C,K = map(int,input().split())
a = [[[0]*4]*3005]*3005
dp = [[[0]*4]*3005]*3005

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

for i in range(R):
    for j in range(C):
        for k in reversed(range(3)):
            #chmax(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            dp[i][j][k+1] = max(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            
        for k in range(4):
            #chmax(dp[i+1][j][0],dp[i][j][k])
            #chmax(dp[i][j+1][k],dp[i][j][k])
            dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k],dp[i][j][k])

ans = 0
for i in range(4):
    #chmax(ans,dp[R-1][C-1][i])
    ans =  max(ans,dp[R-1][C-1][i])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
R,C,K = map(int,input().split())
a = np.zeros((3005,3005),dtype=np.int)
dp = np.zeros((5,3005,3005),dtype=np.int)
def chmax(x,y):
    x = max(x,y)
    return x

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

for i in range(R):
    for j in range(C):
        for k in reversed(range(3)):
            #chmax(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            dp[i][j][k+1] = max(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            
        for k in range(4):
            #chmax(dp[i+1][j][0],dp[i][j][k])
            #chmax(dp[i][j+1][k],dp[i][j][k])
            dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k],dp[i][j][k])

ans = 0
for i in range(4):
    #chmax(ans,dp[R-1][C-1][i])
    ans =  max(ans,dp[R-1][C-1][i])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
R,C,K = map(int,input().split())
a = np.zeros((3005,3005),dtype=np.int)
dp = np.zeros((4,3005,3005),dtype=np.int)
def chmax(x,y):
    x = max(x,y)
    return x

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

for i in range(R):
    for j in range(C):
        for k in reversed(range(3)):
            #chmax(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            dp[i][j][k+1] = max(dp[i][j][k+1],dp[i][j][k]+a[i][j])
            
        for k in range(4):
            #chmax(dp[i+1][j][0],dp[i][j][k])
            #chmax(dp[i][j+1][k],dp[i][j][k])
            dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k],dp[i][j][k])

ans = 0
for i in range(4):
    #chmax(ans,dp[R-1][C-1][i])
    ans =  max(ans,dp[R-1][C-1][i])
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
r, c, k = map(int, input().split())
itm = [[0]*c for _ in range(r)]
for i in range(k):
  ri, ci, vi = map(int, input().split())
  itm[ri-1][ci-1] = vi
dp = [0]*c
for i in range(r):
  que = []
  heapq.heapify(que)
  nowv = itm[i][0]
  if nowv > 0:
    heapq.heappush(que, nowv)
    dp[0] += nowv
  for j in range(1, c):
    nowv = itm[i][j]
    if nowv > 0:
      heapq.heappush(que, nowv)
      if len(que)>3:
        prev = heapq.heappop(que)
      else:
        prev = 0
    sumv1 = dp[j] + nowv#上から
    sumv2 = dp[j-1] + nowv - prev#左から
    if sumv1 >= sumv2:
      dp[j] = sumv1
      que = []
      heapq.heapify(que)
      if nowv > 0:
        heapq.heappush(que, nowv)
    else:
      dp[j] = sumv2
print(dp[-1])

Submission status: Runtime Error

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

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

print(max(dp[R-1][C-1]))R, C, K = map(int, input().split())
goods = [[0] * C for _ in range(R)]
for _ in range(K):
    r, c, v = map(int, input().split())
    goods[r-1][c-1] = v

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

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

Submission status: Runtime Error

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

R, C, K = MI()
item = {(0,0):0}

for i in range(K):
    r_, c_, v_, = MI()
    item[(r_ - 1,c_ - 1)] = v_

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

for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            a = dp[i][j][k]
            if (i,j) in item:
                v = item[(i,j)]
            else:
                v = 0

            if k == 3:
                dp[0][i + 1][j] = max(a, dp[0][i + 1][j])
                continue
            dp[0][i + 1][j] = max(a, a + v, dp[0][i + 1][j])
            dp[k][i][j + 1] = max(a, dp[k][i][j + 1])
            dp[k + 1][i][j + 1] = max(a + v, dp[k + 1][i][j + 1])

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
# 文字列をinput()した場合、末尾に改行が入るので注意
def main(R,C,K,rcv):
  inf=float('inf')
  dp0=[[-inf,-inf,-inf,0] for _ in range(C)]
  dp1=[[-inf]*4 for _ in range(C)]
  dp0[0][3]=0
  rcv.sort(key=lambda x:R*x[0]+x[1])
  i=0
  nowc=0
  while rcv[i][0]==1:
    nextc=rcv[i][1]-1
    for j in range(nowc+1,nextc+1):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][2]=dp0[nowc][2]
    v=rcv[i][2]
    dp0[nextc][0]=max(dp0[nextc][0],dp0[nextc][1]+v)
    dp0[nextc][1]=max(dp0[nextc][1],dp0[nextc][2]+v)
    dp0[nextc][2]=max(dp0[nextc][2],dp0[nextc][3]+v)
    i+=1
    nowc=nextc
    for j in range(nowc+1,C):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][2]=dp0[nowc][2]
  for j in range(1,R):
    for c in range(C):
      dp0[c][3]=max(dp0[c])
      dp0[c][2]=-inf
      dp0[c][1]=-inf
      dp0[c][0]=-inf
    nowc=0
    while i<K and rcv[i][0]==j+1:
      nextc=rcv[i][1]-1
      for k in range(nowc+1,nextc+1):
        dp0[k][0]=dp0[nowc][0]
        dp0[k][1]=dp0[nowc][1]
        dp0[k][2]=dp0[nowc][2]
      v=rcv[i][2]
      dp0[nextc][0]=max(dp0[nextc][0],dp0[nextc][1]+v)
      dp0[nextc][1]=max(dp0[nextc][1],dp0[nextc][2]+v)
      dp0[nextc][2]=max(dp0[nextc][2],dp0[nextc][3]+v)
      i+=1
      nowc=nextc
    for j in range(nowc+1,C):
      dp0[j][0]=dp0[nowc][0]
      dp0[j][1]=dp0[nowc][1]
      dp0[j][2]=dp0[nowc][2]
    #print(dp0)

  print(max(dp0[-1]))


if __name__=='__main__':
  R,C,K=map(int,input().split())
  rcv=[list(map(int,input().split())) for _ in range(K)]
  #import datetime
  #print(datetime.datetime.now())
  main(R,C,K,rcv)
  #print(datetime.datetime.now())

Submission status: Runtime Error

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

for _ in range(k):
    ri,ci,vi=map(int, input().split())
    v[(ri-1,ci-1)]=vi
    
dp=[[0]*4 for _ in range(c+1)]
for j in range(c):
    if (0,j) in v:
        for t in range(1,4):
            dp[j][t]=max(dp[j-1][t-1]+v[(0,j)],dp[j-1][t])
    else:
        dp[j][t]=dp[j-1][t]
import copy
for i in range(1,r):
    dp2=[[dp[x][3] for _ in range(4)] if (i,x) not in v else [dp[x][3]]+[dp[x][3]+v[(i,x)] for _ in range(3)] for x in range(c+1)]
    for j in range(c):
        if (i,j) in v:
            for t in range(1,4):
                dp2[j][t]=max(dp2[j-1][t-1]+v[(i,j)],dp2[j-1][t],dp2[j][t])
        else:
            dp2[j][t]=max(dp2[j-1][t],dp2[j][t])
    dp=dp2

print(max(dp[-2]))

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit
@njit
def main():
    R, C, K = map(int, input().split())
    scores = np.zeros((R,C), np.int64)
    for _ in range(K):
        r, c, v = map(int, input().split())
        scores[r-1][c-1] = v
    dp=np.zeros((R+1,C+1,4), np.int64)

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

    answer = dp[R][C][3]
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

@njit
def main():
    R, C, K = map(int, input().split())
    scores = np.zeros((R,C), np.int64)
    for _ in range(K):
        r, c, v = map(int, input().split())
        scores[r-1][c-1] = v
    dp=np.zeros((R+1,C+1,4), np.int64)

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

    answer = dp[R][C][3]
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    R, C, K = map(int, input().split())
    scores = np.zeros((R,C), np.int64)
    for _ in range(K):
        r, c, v = map(int, input().split())
        scores[r][c] = v
    dp=np.zeros((R+1,C+1,4), np.int64)

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

    answer = dp[R][C][3]
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>

/*
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i,s,e) for((i)=(s);(i)<(e);(i)++)
#define RREP(i,s,e) for((i)=((s)-1);(i)>=(e);(i)--)
#define FOR(i,n) for((i)=(0);(i)<(n);(i)++)
#define RFOR(i,n) for((i)=((n)-1);(i)>=(0);(i)--)
#define MOD 1000000007
#define MAX 5000
signed main(){
    int R,C,K;
    cin>>R>>C>>K;

    int rcv[MAX+1][MAX+1]={{0}};
    int ans[4][MAX+1][MAX+1];
    for(int i=0;i<K;i++){
        int r,c,v;
        cin>>r>>c>>v;
        rcv[r][c]=v;
    }

    queue<int> qx,qy;

    qx.push(1);
    qy.push(1);

    for(int i=1;i<=R;i++){
        for(int j=1;j<=C;j++){
            for(int k=0;k<4;k++){
                ans[k][i][j]=-1;
            }
        }
    }

    ans[0][1][1]=0;
    while(!qx.empty()){
        int nx=qx.front();
        qx.pop();
        int ny=qy.front();
        qy.pop();
        for(int i=0;i<3;i++){
            if(ans[i][ny][nx]==-1) continue;
            if(i<=2){
                if(nx!=C&&(ans[i+1][ny][nx+1]==-1||ans[i+1][ny][nx+1]<ans[i][ny][nx]+rcv[ny][nx])){
                    ans[i+1][ny][nx+1]=ans[i][ny][nx]+rcv[ny][nx];
                    qx.push(nx+1);
                    qy.push(ny);
                }
            }
            if(nx!=C&&(ans[i][ny][nx+1]==-1||ans[i][ny][nx+1]<ans[i][ny][nx])){
                ans[i][ny][nx+1]=ans[i][ny][nx];
                qx.push(nx+1);
                qy.push(ny);
            }
            if(ny!=R&&(ans[0][ny+1][nx]==-1||ans[0][ny+1][nx]<ans[i][ny][nx]+rcv[ny][nx])){
                ans[0][ny+1][nx]=ans[i][ny][nx]+rcv[ny][nx];
                qx.push(nx);
                qy.push(ny+1);
            }
        }
    }
    cout<<max(ans[0][R][C]+rcv[R][C],max(ans[1][R][C]+rcv[R][C],max(ans[2][R][C]+rcv[R][C],ans[3][R][C])))<<endl;
}

Submission status: Runtime Error

Solution code:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>

/*
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i,s,e) for((i)=(s);(i)<(e);(i)++)
#define RREP(i,s,e) for((i)=((s)-1);(i)>=(e);(i)--)
#define FOR(i,n) for((i)=(0);(i)<(n);(i)++)
#define RFOR(i,n) for((i)=((n)-1);(i)>=(0);(i)--)
#define MOD 1000000007
#define MAX 100
signed main(){
    int R,C,K;
    cin>>R>>C>>K;

    int rcv[MAX+1][MAX+1]={{0}};
    int ans[4][MAX+1][MAX+1];
    for(int i=0;i<K;i++){
        int r,c,v;
        cin>>r>>c>>v;
        rcv[r][c]=v;
    }

    queue<int> qx,qy;

    qx.push(1);
    qy.push(1);

    for(int i=1;i<=R;i++){
        for(int j=1;j<=C;j++){
            for(int k=0;k<4;k++){
                ans[k][i][j]=-1;
            }
        }
    }

    ans[0][1][1]=0;
    while(!qx.empty()){
        int nx=qx.front();
        qx.pop();
        int ny=qy.front();
        qy.pop();
        for(int i=0;i<3;i++){
            if(ans[i][ny][nx]==-1) continue;
            if(i<=2){
                if(nx!=C&&(ans[i+1][ny][nx+1]==-1||ans[i+1][ny][nx+1]<ans[i][ny][nx]+rcv[ny][nx])){
                    ans[i+1][ny][nx+1]=ans[i][ny][nx]+rcv[ny][nx];
                    qx.push(nx+1);
                    qy.push(ny);
                }
            }
            if(nx!=C&&(ans[i][ny][nx+1]==-1||ans[i][ny][nx+1]<ans[i][ny][nx])){
                ans[i][ny][nx+1]=ans[i][ny][nx];
                qx.push(nx+1);
                qy.push(ny);
            }
            if(ny!=R&&(ans[0][ny+1][nx]==-1||ans[0][ny+1][nx]<ans[i][ny][nx]+rcv[ny][nx])){
                ans[0][ny+1][nx]=ans[i][ny][nx]+rcv[ny][nx];
                qx.push(nx);
                qy.push(ny+1);
            }
        }
    }
    cout<<max(ans[0][R][C]+rcv[R][C],max(ans[1][R][C]+rcv[R][C],max(ans[2][R][C]+rcv[R][C],ans[3][R][C])))<<endl;
}

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from collections import defaultdict
from numba import njit
def input(): return sys.stdin.readline().rstrip()

@njit
def koushin(dp,item):
    dptmp1 = np.copy(dp)
    dptmp2 = np.copy(dp)
    dptmp3 = np.copy(dp)
    for i in range(len(item)):
        dptmp3[item[i][0]:] = np.fmax(dptmp3[item[i][0]:], dptmp2[item[i][0]]+item[i][1])
        dptmp2[item[i][0]:] = np.fmax(dptmp2[item[i][0]:], dptmp1[item[i][0]]+item[i][1])
        dptmp1[item[i][0]:] = np.fmax(dptmp1[item[i][0]:], dp[item[i][0]]+item[i][1])
    return dptmp3

def main():
    R, C, K = map(int, input().split())
    items = defaultdict(list)
    for _ in range(K):
        r, c, v = map(int, input().split())
        items[r-1].append([c-1, v])
    dp = np.zeros(C, dtype=int)
    for RR in range(R):
        items[RR].sort(key=lambda x: x[0])
        dp=koushin(dp,np.array(items[RR],dtype=int))
    print(dp[-1])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    from sys import stdin
    def input():
        return stdin.readline().strip()

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

    # dp[j][n] = j列にいて、n個itemを拾った時のアイテムの価値の合計の最大値
    # 1行ずつdpを更新する
    # 0行目
    value = v[0][0]
    if value == 0:
        dp = [[0] * 4]
    else:
        dp = [[0, value, value, value]]
    for j in range(1, c):
        value = v[0][j]
        if value == 0:
            dp.append(dp[j-1])
            continue
        a = dp[j-1].copy()
        b = [0, a[0]+value, a[1]+value, a[2]+value]
        for k in range(1, 4):
            if a[k] < b[k]:
                a[k] = b[k]
        dp.append(a)

    print(dp)

    # i行目
    for i in range(1, r):
        pre = dp[0][3]
        value = v[i][0]
        if value == 0:
            new_dp = [[pre] * 4]
        else:
            new_dp = [[pre, pre+value, pre+value, pre+value]]
        for j in range(1, c):
            value = v[i][j]
            if value == 0:
                a = new_dp[j-1].copy()
                b = [dp[j][3]] * 4
                for k in range(4):
                    if a[k] < b[k]:
                        a[k] = b[k]
                new_dp.append(a)
            else:
                a = new_dp[j-1].copy()
                pre = dp[j][3]
                b = [pre, pre+value, pre+value, pre+value]
                d = [a[0], a[0]+value, a[1]+value, a[2]+value]
                for k in range(4):
                    if a[k] < b[k]:
                        a[k] = b[k]
                    if a[k] < d[k]:
                        a[k] = d[k]
                new_dp.append(a)
        dp = new_dp
        print(dp)

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

main()

Submission status: Runtime Error

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]*w for j in H] for i in range(4)]
  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[1][0][0]=dp[0][0][0]
    dp[0][0][0]=0
  for y in H:
    for x in W:
      v=dp[0][y][x]
      dp[0][y][x]=0
      for i in range(4):
        if 0<x:
          if dp[i][y][x]<dp[i][y][x-1]:
            dp[i][y][x]=dp[i][y][x-1]
          if 0<i and v!=0 and dp[i][y][x]<dp[i-1][y][x-1]+v:
            dp[i][y][x]=dp[i-1][y][x-1]+v
        if 0<y:
          if dp[0][y][x]<dp[i][y-1][x]:
            dp[0][y][x]=dp[i][y-1][x]
          if v!=0 and dp[1][y][x]<dp[i][y-1][x]+v:
            dp[1][y][x]=dp[i][y-1][x]+v
  print(max([dp[i][h-1][w-1] for i in range(4)]))
main()