1715번 - 카드 정렬하기
#include
using namespace std;
int main() {
int N, input;
cin >> N;
priority_queue,greater> pq;
for (int i = 0; i < N; ++i) {
cin >> input;
pq.push(input);
}
int sum = 0;
int new_value= 0;
while (!pq.empty()) {
int a = pq.top();
pq.pop();
int b = pq.top();
new_value = a + b;
sum += new_value;
pq.push(new_value);
if (pq.size() == 1) {
if (N == 1)
cout << input;
else
cout << sum;
return 0;
메모리 초과가 계속 뜨는데,,살려주세요 선배님들
댓글을 작성하려면 로그인해야 합니다.
pliot1017 2년 전
#include
#include
#include
using namespace std;
int main() {
int N, input;
cin >> N;
priority_queue,greater> pq;
for (int i = 0; i < N; ++i) {
cin >> input;
pq.push(input);
}
int sum = 0;
int new_value= 0;
while (!pq.empty()) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
new_value = a + b;
sum += new_value;
pq.push(new_value);
if (pq.size() == 1) {
pq.pop();
}
}
if (N == 1)
cout << input;
else
cout << sum;
return 0;
}
메모리 초과가 계속 뜨는데,,살려주세요 선배님들