From 58a3e897d0027532051505b8c9720965f89d43bc Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Sat, 27 Sep 2025 12:27:23 +0900 Subject: [PATCH] =?UTF-8?q?[20250927]=20BOJ=20/=20G4=20/=20=EB=8F=84?= =?UTF-8?q?=EC=84=9C=EA=B4=80=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...J \353\217\204\354\204\234\352\264\200.md" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "0224LJH/202509/27 BOJ \353\217\204\354\204\234\352\264\200.md" diff --git "a/0224LJH/202509/27 BOJ \353\217\204\354\204\234\352\264\200.md" "b/0224LJH/202509/27 BOJ \353\217\204\354\204\234\352\264\200.md" new file mode 100644 index 00000000..61f15b02 --- /dev/null +++ "b/0224LJH/202509/27 BOJ \353\217\204\354\204\234\352\264\200.md" @@ -0,0 +1,75 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +class Main { + + static int bookCnt, bookMax, plusMax, minusMax,ans; + static PriorityQueue plusPq = new PriorityQueue<>(Collections.reverseOrder()); + static PriorityQueue minusPq = new PriorityQueue<>(Collections.reverseOrder()); + + public static void main(String[] args) throws NumberFormatException, IOException { + init(); + process(); + print(); + + } + + + public static void init() throws NumberFormatException, IOException { + BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + bookCnt = Integer.parseInt(st.nextToken()); + bookMax = Integer.parseInt(st.nextToken()); + plusMax = 0; + minusMax = 0; + st = new StringTokenizer(br.readLine()); + + for (int i = 0; i < bookCnt; i++) { + int num = Integer.parseInt(st.nextToken()); + if (num < 0 ) { + num *= -1; + minusPq.add(num); + minusMax = Math.max(minusMax, num); + } else { + plusPq.add(num); + plusMax = Math.max(plusMax, num); + } + } + + } + + public static void process() throws IOException { + ans = 0; + + while (!minusPq.isEmpty()) { + ans += minusPq.peek(); + for (int i = 0; i < bookMax; i++) { + if (minusPq.isEmpty()) break; + minusPq.poll(); + } + + } + while (!plusPq.isEmpty()) { + ans += plusPq.peek(); + for (int i = 0; i < bookMax; i++) { + if (plusPq.isEmpty()) break; + plusPq.poll(); + } + + } + + ans *= 2; + ans -= Math.max(minusMax, plusMax); + + } + + + public static void print() { + System.out.println(ans); + } +} +```