February 08, 2021
>>> import sys
>>> sys.getrecursionlimit()
1000
>>> sys.setrecursionlimit(2000) # 최대 재귀호출 횟수를 2000으로 늘림
26
# 3
n = int(input())
dp = [0] * 30001
for i in range(2, n + 1):
dp[i] = dp[i - 1] + 1
if i % 2 == 0:
dp[i] = min(dp[i], dp[i // 2] + 1)
if i % 3 == 0:
dp[i] = min(dp[i], dp[i // 3] + 1)
if i % 5 == 0:
dp[i] = min(dp[i], dp[i // 5] + 1)
print(dp[n])
4
1 3 1 5
# 8
6
2 4 3 5 8 4
# 13
1로 만들기
보다는 개미 전사
가 쉬웠다.n = int(input())
data = list(map(int, input().split()))
dp = [0] * 100
dp[0] = data[0]
dp[1] = max(data[0], data[1])
for i in range(2, n):
dp[i] = max(dp[i - 1], dp[i - 2] + data[i])
print(dp[n - 1])
3
# 5
7
# 85
n = int(input())
dp = [0] * 1001
dp[1] = 1
dp[2] = 3
for i in range(3, n + 1):
dp[i] = (dp[i - 2] * 2 + dp[i - 1]) % 796796
print(dp[n])
2 15
2
3
# 5
3 4
3
5
7
# -1
5 60
5
6
8
9
10
# 6
n, m = map(int, input().split())
money = [int(input()) for _ in range(n)]
dp = [10001] * (m + 1)
dp[0] = 0
for i in range(n):
for j in range(money[i], m + 1):
dp[j] = min(dp[j], dp[j - money[i]] + 1)
print(-1) if dp[m] == 10001 else print(dp[m])
Source