-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2579_baek.py
More file actions
53 lines (39 loc) · 1.07 KB
/
2579_baek.py
File metadata and controls
53 lines (39 loc) · 1.07 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
input = sys.stdin.readline
n = int(input())
s = [int(input()) for _ in range(n)]
res = 0
def jump(idx, step, total):
global res
if idx >= n:
return
if idx == n - 1:
res = max(res, total + s[idx])
return
# 한 칸 오르기 (step+1)
if step < 2:
jump(idx + 1, step + 1, total + s[idx])
# 두 칸 오르기 (step 리셋)
jump(idx + 2, 1, total + s[idx])
# 시작점은 idx=0 또는 idx=1로 시도할 수 있음
jump(0, 1, 0) # 0번째 계단에서 시작
res = max(res, 0) # 아무것도 안 밟은 경우도 고려
print(res)
### 동적할당 방법
import sys
input = sys.stdin.readline
n = int(input())
s = [int(input()) for _ in range(n)] # 각 계단의 점수
# 예외 처리: 계단이 1개 또는 2개일 경우
if n == 1:
print(s[0])
elif n == 2:
print(s[0] + s[1])
else:
dp = [0] * n
dp[0] = s[0]
dp[1] = s[0] + s[1]
dp[2] = max(s[0] + s[2], s[1] + s[2])
for i in range(3, n):
dp[i] = max(dp[i-2] + s[i], dp[i-3] + s[i-1] + s[i])
print(dp[-1])