PS/Python

[백준] 17610 - 양팔저울 (파이썬, Python)

w00se 2021. 8. 28. 12:04

https://pixabay.com/ko/photos/우산-햇빛-여자-비-빛-6239364/

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

 

17610번: 양팔저울

무게가 서로 다른 k개의 추와 빈 그릇이 있다. 모든 추의 무게는 정수이고, 그릇의 무게는 0으로 간주한다. 양팔저울을 한 번만 이용하여 원하는 무게의 물을 그릇에 담고자 한다. 주어진 모든 추

www.acmicpc.net

 

전체 코드

제출 언어: PyPy3

시간: 260ms

import sys

if __name__ == "__main__":
    k = int(sys.stdin.readline().strip())

    weights = list(map(int, sys.stdin.readline().strip().split()))

    weights.sort()

    max_num = sum(weights)

    candidates = [ weights[0] ]

    for i in range(1, k):
        cur_num = weights[i]
        temp = [ cur_num ]

        for e in candidates:
            temp += [ cur_num + e, abs(cur_num-e) ]

        candidates += temp

    candidates = list(set(candidates))

    if 0 in candidates:
        candidates.remove(0)

    answer = max_num - len(candidates)

    print(answer)

읽어 주셔서 감사합니다 :)

틀린 부분이 있다면 댓글로 편히 알려주세요😊