-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); | ||
|
||
priority_queue <int, vector<int>, greater<int>> pq; //최소 힙으로 우선순위 큐 선언 | ||
int n; | ||
cin >> n; | ||
for (int i = 0; i < n; i++) //우선순위 큐에 다 넣음 | ||
{ | ||
int x; | ||
cin >> x; | ||
pq.push(x); | ||
} | ||
int result = 0; | ||
while(pq.size() > 1) //우선순위 큐에 값이 하나만 남을 때 까지 반복 | ||
{ | ||
// 우선순위 큐에서 가장 작은 두 수를 꺼내서 합침 | ||
int a = pq.top(); | ||
pq.pop(); | ||
int b = pq.top(); | ||
pq.pop(); | ||
int sum = a + b; | ||
pq.push(sum); // 합친 결과를 우선순위 큐에 다시 넣음 | ||
|
||
result += sum; // 결과값을 누적 | ||
} | ||
cout << result; | ||
return 0; | ||
} |