코딩테스트/그리디|힙
[그리디] 백준 1715 카드 정렬하기 C++
유(YOO)
2023. 9. 27. 15:01
https://www.acmicpc.net/problem/1715
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
int x;
priority_queue<int, vector<int>, greater<>> pq;
for(int i=0; i<n; i++) {
cin >> x;
pq.push(x);
}
long long answer = 0;
while(!pq.empty()) { // 카드 묶음 순서대로 비교X
if (pq.size() == 1) break;
int x1 = pq.top();
pq.pop();
int x2 = pq.top();
pq.pop();
answer += x1 + x2;
pq.push(x1+x2);
}
cout << answer;
return 0;
}