-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1461.cpp
44 lines (36 loc) · 908 Bytes
/
1461.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N,M;
cin >> N >> M;
vector<int> temp1;
vector<int> temp2;
for(int i=0;i<N;i++){
int a;
cin >> a;
if(a<0) temp1.push_back(a);
else temp2.push_back(-a);
}
if(!temp1.empty()) sort(temp1.begin(), temp1.end());
if(!temp2.empty()) sort(temp2.begin(), temp2.end());
int result=0;
for(int i=0;i<temp1.size();i=i+M){
int v = -temp1[i];
result+=2*v;
}
for(int i=0;i<temp2.size();i=i+M){
int v = -temp2[i];
result+=2*v;
}
if(temp1.empty()) result -= -temp2[0];
else if(temp2.empty()) result -= -temp1[0];
else if(temp1[0] < temp2[0]) result -= -temp1[0];
else result -= -temp2[0];
cout << result;
return 0;
}