From e4ebc353e1ad3eafce513301e384773bd8bc731d Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 19 Aug 2025 16:14:38 +0900 Subject: [PATCH] =?UTF-8?q?[20250819]=20BOJ=20/=20G4=20/=20=EB=8F=84?= =?UTF-8?q?=EC=84=9C=EA=B4=80=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \353\217\204\354\204\234\352\264\200.md" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "suyeun84/202508/19 BOJ G4 \353\217\204\354\204\234\352\264\200.md" diff --git "a/suyeun84/202508/19 BOJ G4 \353\217\204\354\204\234\352\264\200.md" "b/suyeun84/202508/19 BOJ G4 \353\217\204\354\204\234\352\264\200.md" new file mode 100644 index 00000000..74b2299c --- /dev/null +++ "b/suyeun84/202508/19 BOJ G4 \353\217\204\354\204\234\352\264\200.md" @@ -0,0 +1,44 @@ +```java +import java.util.*; +import java.io.*; + +public class boj1461 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + public static void main(String[] args) throws Exception { + nextLine(); + int N = nextInt(); + int M = nextInt(); + int answer = 0; + PriorityQueue pos = new PriorityQueue<>(Collections.reverseOrder()); + PriorityQueue neg = new PriorityQueue<>(Collections.reverseOrder()); + nextLine(); + for (int i = 0; i < N; i++) { + int num = nextInt(); + if (num < 0) neg.add(Math.abs(num)); + else pos.add(num); + } + int last = 0; + if (pos.isEmpty()) last = neg.peek(); + else if (neg.isEmpty()) last = pos.peek(); + else { + last = Math.max(neg.peek(), pos.peek()); + } + + while (!pos.isEmpty()) { + answer += pos.peek() * 2; + for (int i = 0; i < M; i++) pos.poll(); + } + + while(!neg.isEmpty()) { + answer += neg.peek() * 2; + for (int i = 0; i < M; i++) neg.poll(); + } + + System.out.println(answer - last); + } +} +```