Skip to content

Commit 48d40c6

Browse files
authored
Merge pull request #692 from AlgorithmWithGod/suyeun84
[20250819] BOJ / G4 / 도서관 / 김수연
2 parents 8844497 + e4ebc35 commit 48d40c6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj1461 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
public static void main(String[] args) throws Exception {
12+
nextLine();
13+
int N = nextInt();
14+
int M = nextInt();
15+
int answer = 0;
16+
PriorityQueue<Integer> pos = new PriorityQueue<>(Collections.reverseOrder());
17+
PriorityQueue<Integer> neg = new PriorityQueue<>(Collections.reverseOrder());
18+
nextLine();
19+
for (int i = 0; i < N; i++) {
20+
int num = nextInt();
21+
if (num < 0) neg.add(Math.abs(num));
22+
else pos.add(num);
23+
}
24+
int last = 0;
25+
if (pos.isEmpty()) last = neg.peek();
26+
else if (neg.isEmpty()) last = pos.peek();
27+
else {
28+
last = Math.max(neg.peek(), pos.peek());
29+
}
30+
31+
while (!pos.isEmpty()) {
32+
answer += pos.peek() * 2;
33+
for (int i = 0; i < M; i++) pos.poll();
34+
}
35+
36+
while(!neg.isEmpty()) {
37+
answer += neg.peek() * 2;
38+
for (int i = 0; i < M; i++) neg.poll();
39+
}
40+
41+
System.out.println(answer - last);
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)