-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1744.cpp
More file actions
38 lines (33 loc) · 847 Bytes
/
1744.cpp
File metadata and controls
38 lines (33 loc) · 847 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n; cin >> n;
vector<int> numbers;
for (int i = 0; i < n; i++) {
int num; cin >> num; numbers.push_back(num);
}
sort(numbers.begin(), numbers.end()); // 오름차순
int ans = 0, left = 0, right = n - 1;
// 음수 : 1보다 작은 두 수를 곱하면 항상 최대
for (left; left < right; left += 2) {
if (numbers[left] < 1 && numbers[left + 1] < 1) {
ans += numbers[left] * numbers[left + 1];
}
else break;
}
// 양수 : 1보다 큰 두 수를 곱하면 항상 최대
for (right; right > 0; right -= 2) {
if (numbers[right] > 1 && numbers[right - 1] > 1) {
ans += numbers[right] * numbers[right - 1];
}
else break;
}
// 처리되지 못한 수들은 단순히 더한다
for (left; left <= right; left++) {
ans += numbers[left];
}
cout << ans;
return 0;
}