-
Notifications
You must be signed in to change notification settings - Fork 0
/
도서관_1461.java
52 lines (45 loc) · 1.56 KB
/
도서관_1461.java
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
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class 도서관_1461 {
public static void main(String args[]) throws Exception {
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());
List<Integer> plus = new ArrayList<>();
List<Integer> minus = new ArrayList<>();
st = new StringTokenizer(br.readLine(), " ");
while (N-- > 0) {
int tmp = Integer.parseInt(st.nextToken());
if (tmp > 0) {
plus.add(tmp);
} else {
minus.add(-1 * tmp);
}
}
Collections.sort(plus, Collections.reverseOrder());
Collections.sort(minus, Collections.reverseOrder());
System.out.println(plus.size() > 0 && (minus.size() == 0 || plus.get(0) > minus.get(0)) ?
check(minus, plus, M) : check(plus, minus, M));
}
static int check(List<Integer> a, List<Integer> b, int M) {
int answer = 0;
int idx = 0;
while (idx < a.size()) {
answer += a.get(idx) * 2;
idx += M;
}
idx = 0;
while (idx < b.size()) {
answer += b.get(idx) * 2;
idx += M;
}
answer -= b.get(0);
return answer;
}
}