알고리즘 문제풀이

아이들과 선물상자[백준 23757]

wiojfe 2024. 9. 16. 16:55

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

 

파이썬의 우선순위 큐인 heapq 사용하면 쉽게 풀 수 있다. 

import heapq 
n,m = map(int, input().split())
giftcnt = list(map(int ,input().split()))
wantcnt = list(map(int ,input().split()))
heap = []
for i in giftcnt :    heap.append(i*(-1))
heapq.heapify(heap)
ans = 1 
for i in wantcnt :
    minus_k= heapq.heappop(heap) 
    k = (-1)*minus_k 
    if i > k : 
        print(0)
        exit() 
    else :
        k = k-i 
        heapq.heappush(heap,(-1)*k)
print(1)