[C++] 백준 14235번 크리스마스 선물

xyzw·2025년 2월 14일
0

algorithm

목록 보기
28/61

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

풀이

최대 힙을 이용하였다.

a가 자연수일 때: 힙에 a개의 선물을 추가한다.
a가 0일 때: 힙의 top 요소를 제거하여 아이들에게 준 선물을 가치를 출력한다.

#include <iostream>
#include <queue>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    priority_queue<int> pq;
    int n, a;
    cin >> n;
    
    while(n--){
       cin >> a;
       if(!a) {
           if(pq.empty()) cout << "-1\n";
           else {
               cout << pq.top() << "\n";
               pq.pop();
           }
       } else {
           int x;
           while(a--){
               cin >> x;
               pq.push(x);
           }
       }
    }
    
    return 0;
}

0개의 댓글