

풀이
- 가장 먼 곳의 위치에 있는 책은 갔다가 돌아올 필요가 없기때문에 마지막에 왕복거리에서 빼줄 num을 구한다.
- 한 번에 들 수 있는 책의 개수 m만큼 반복하며 우선순위큐에서 수를 제거하지만 m권중 가장 먼곳에 있는 값만 answer에 왕복 거리를 추가
package problem_solving.greedy;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class BaekJoon_1461 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int m = Integer.parseInt(sc.next());
PriorityQueue<Integer> plusP = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> minusP = new PriorityQueue<>(Collections.reverseOrder());
for(int i= 0 ; i < n ; i++) {
int num = Integer.parseInt(sc.next());
if( num < 0 ) {
minusP.offer(Math.abs(num));
}else {
plusP.offer(num);
}
}
int num = 0 ;
if( !plusP.isEmpty() && !minusP.isEmpty()) {
num = Math.max(plusP.peek(), minusP.peek());
} else if( !plusP.isEmpty()) {
num = plusP.peek();
} else if( !minusP.isEmpty()) {
num = minusP.peek();
}
int answer= 0 ;
while(!plusP.isEmpty()) {
for(int i= 0 ; i < m ; i++) {
int temp = plusP.poll();
if( i == 0 ) {
answer+=temp*2;
}
if( plusP.isEmpty()) {
break;
}
}
}
while(!minusP.isEmpty()) {
for(int i= 0 ; i < m ; i++) {
int temp = minusP.poll();
if( i == 0 ) {
answer+=temp*2;
}
if( minusP.isEmpty()) {
break;
}
}
}
answer-=num ;
System.out.println(answer);
}
}