forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.py
35 lines (31 loc) Β· 1 KB
/
1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# ν
μ€νΈ μΌμ΄μ€(Test Case) μ
λ ₯
for tc in range(int(input())):
# κΈκ΄ μ 보 μ
λ ₯
n, m = map(int, input().split())
array = list(map(int, input().split()))
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 2μ°¨μ DP ν
μ΄λΈ μ΄κΈ°ν
dp = []
index = 0
for i in range(n):
dp.append(array[index:index + m])
index += m
# λ€μ΄λλ―Ή νλ‘κ·Έλλ° μ§ν
for j in range(1, m):
for i in range(n):
# μΌμͺ½ μμμ μ€λ κ²½μ°
if i == 0:
left_up = 0
else:
left_up = dp[i - 1][j - 1]
# μΌμͺ½ μλμμ μ€λ κ²½μ°
if i == n - 1:
left_down = 0
else:
left_down = dp[i + 1][j - 1]
# μΌμͺ½μμ μ€λ κ²½μ°
left = dp[i][j - 1]
dp[i][j] = dp[i][j] + max(left_up, left_down, left)
result = 0
for i in range(n):
result = max(result, dp[i][m - 1])
print(result)