#백준 1744 – 수


1744 – 문제, 입력, 출력

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int N;
	cin >> N;

	int temp,sum=0,zero=0; //0따로
	priority_queue<int> high_pq; //1이상
	priority_queue<int, vector<int>, greater<int>> low_pq; //-1이하
	for (int i = 0; i < N; i++) {
		cin >> temp;
		if (temp > 0) high_pq.push(temp);
		else if (temp < 0) low_pq.push(temp);
		else zero++;
	}

	int num1, num2;
	while (!
high_pq.empty()) { num1 = high_pq.top(); high_pq.pop(); if (high_pq.empty()) { sum+=num1; break; } num2 = high_pq.top(); high_pq.pop(); if (num2 == 1 || num1 == 1) sum += num1+num2; //둘중 하나라도 1이면 더함 else sum += (num1 * num2); } while (!
low_pq.empty()) { num1 = low_pq.top(); low_pq.pop(); if (low_pq.empty()) { if (zero > 0); //0이 하나라도 있었으면 0x음수 = 0, 아무 연산 안함 else sum += num1; break; } num2 = low_pq.top(); low_pq.pop(); sum+=(num1*num2); } cout << sum; return 0; }