백준 14225 부분수열의 합

고장난 고양이·2022년 10월 29일
0

알고리즘_python

목록 보기
78/84
post-thumbnail

문제

https://www.acmicpc.net/problem/14225

코드

from itertools import combinations
dp=[0]*200001
dp[0]=1
n=int(input())
data=list(map(int,input().split()))

for i in range(1,n+1):
    for j in combinations(data,i):
        dp[sum(j)]=1
print(dp.index(0))

조합을 이용해서 수의 합을 구해낸다.
처음에는리스트에 조합의 합을 넣어가면서 없는 숫자를 비교했는데 그럴경우 시간초과가 난다.
이를 해결하기위해, 최대 100000개의 숫자를 주는 만큼 최대합이 200000임으로 그만큼 리스트값을 부여한다.
index(0)을하면 0값을 가진 맨앞원소의 위치를 반환한다.

profile
개발새발X발일지

0개의 댓글