코딩테스트/구현|기타
[정렬] 백준 1744 수 묶기 C++
유(YOO)
2020. 12. 25. 20:14
https://www.acmicpc.net/problem/1744
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> num;
int result, sum = 0;
void cal() {
int a=0, b=0, c=0;
sort(num.begin(), num.end());
for (int i = num.size() - 1; i > 0; i -= 2)
{
a = num[i];
b = num[i - 1];
if ((a > 1) && (b > 1))
{
c = sum - a - b + a * b;
if (c > sum)
{
sum = c;
}
}
else {break;}
}
for (int i = 0; i < num.size()-1; i += 2)
{
a = num[i];
b = num[i + 1];
if ((a <= 0) && (b <= 0))
{
c = sum - a - b + a * b;
if (c > sum)
{
sum = c;
}
}
else {break;}
}
result = sum;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, temp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
num.push_back(temp);
sum += temp;
}
cal();
cout << result;
return 0;
}