알고리즘

백준 14241번 슬라임 합치기 C++

영춘권의달인 2022. 11. 9. 16:20

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

 

14241번: 슬라임 합치기

영선이와 효빈이는 슬라임을 합치는 게임을 하고 있다. 두 사람은 두 슬라임을 골라서 하나로 합쳐야 한다. 게임은 슬라임이 하나 남았을 때 끝난다. 모든 슬라임은 양수 크기를 가지고 있다. 두

www.acmicpc.net

 

우선순위 큐

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>

using namespace std;

int main()
{
	int n;
	cin >> n;
	priority_queue<int> pq;
	for (int i = 0; i < n; i++) {
		int input;
		cin >> input;
		pq.push(input);
	}
	int score = 0;
	while (!pq.empty()) {
		if (pq.size() == 1) break;
		int a = pq.top();
		pq.pop();
		int b = pq.top();
		pq.pop();
		score += a * b;
		pq.push(a + b);
	}
	cout << score;
}