-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
56 lines (46 loc) · 1.63 KB
/
Main.java
File metadata and controls
56 lines (46 loc) · 1.63 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
ArrayList<Integer> arr = new ArrayList<>();
st = new StringTokenizer(br.readLine());
int largest = 0;
for(int i=0; i<N; i++){
int val = Integer.parseInt(st.nextToken());
largest = Math.max((int)Math.abs(val), largest);
arr.add(val);
}
PriorityQueue<Integer> positive = new PriorityQueue<>((e1, e2) -> Integer.compare(e2, e1));
PriorityQueue<Integer> negative = new PriorityQueue<>((e1, e2) -> Integer.compare(e2, e1));
for(int position : arr){
if(position > 0){
positive.add(position);
} else {
negative.add(position * (-1));
}
}
int result = 0;
while(!positive.isEmpty()){
result += positive.poll();
for(int i=0; i<M-1; i++){
positive.poll();
}
}
while(!negative.isEmpty()){
result += negative.poll();
for(int i=0; i<M-1; i++){
negative.poll();
}
}
System.out.println(result*2 - largest);
br.close();
}
}