-
Notifications
You must be signed in to change notification settings - Fork 0
/
1744.cpp
64 lines (58 loc) · 1002 Bytes
/
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
58
59
60
61
62
63
64
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
const int INF = 987654321;
vector<int> a;
vector<int> b;
int n,t;
bool hasZero = false;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &t);
if (t > 0)
a.push_back(t);
else if (t < 0)
b.push_back(t);
else
hasZero = true;
}
a.push_back(-INF);
b.push_back(INF);
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
sort(b.begin(), b.end());
int total = 0;
for (int i = 0; i < a.size()-1; i++) {
if (a[i] == 1 && a[i + 1] == 1) {
total += 2;
i++;
}
else if(a[i+1] == -INF){
total += a[i];
break;
}
else if (a[i + 1] == 1) {
total += a[i];
}
else {
total += a[i + 1] * a[i];
i++;
}
}
for (int i = 0; i < b.size()-1; i++) {
if (b[i + 1] == INF) {
if (hasZero)
break;
total += b[i];
}
else {
total += b[i] * b[i + 1];
i++;
}
}
printf("%d\n", total);
return 0;
}