File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments