-
Notifications
You must be signed in to change notification settings - Fork 0
/
1744.cpp
57 lines (53 loc) · 1.1 KB
/
1744.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <queue>
using namespace std;
int n;
long long ans;
int zeroCount = 0;
priority_queue<int> positive;
priority_queue<int> negative;
int main(void){
cin >> n;
int num;
for(int i=0; i<n; i++){
cin >> num;
if(num > 0){
positive.push(num);
}
else if(num < 0){
negative.push(-num);
}
else{zeroCount++;}
}
int op1, op2;
while(positive.size()){
op1 = positive.top();
positive.pop();
if(positive.empty()){
ans += op1;
break;
}
op2 = positive.top();
positive.pop();
if(op1 == 1 || op2 == 1){
ans += op1+op2;
}
else
ans += op1 * op2;
}
while(negative.size()){
op1 = negative.top();
negative.pop();
if(negative.empty()){
if(zeroCount == 0){
ans += (-op1);
}
break;
}
op2 = negative.top();
negative.pop();
ans += op1 * op2;
}
cout << ans << endl;
return 0;
}