From ee004fda023192a8c4a8bee13d41af70f3ac20db Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 29 Aug 2025 20:26:10 +0900 Subject: [PATCH] =?UTF-8?q?[20250829]=20BOJ=20/=20G5=20/=20=EC=83=9D?= =?UTF-8?q?=EC=9D=BC=EC=84=A0=EB=AC=BC=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35\354\235\274\354\204\240\353\254\274.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "lkhyun/202508/29 BOJ G5 \354\203\235\354\235\274\354\204\240\353\254\274.md" diff --git "a/lkhyun/202508/29 BOJ G5 \354\203\235\354\235\274\354\204\240\353\254\274.md" "b/lkhyun/202508/29 BOJ G5 \354\203\235\354\235\274\354\204\240\353\254\274.md" new file mode 100644 index 00000000..c4154455 --- /dev/null +++ "b/lkhyun/202508/29 BOJ G5 \354\203\235\354\235\274\354\204\240\353\254\274.md" @@ -0,0 +1,45 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N,D; + static class present{ + int price,value; + + present(int price, int value){ + this.price = price; + this.value = value; + } + } + static present[] arr; + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + D = Integer.parseInt(st.nextToken()); + arr = new present[N]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + arr[i] = new present(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())); + } + Arrays.sort(arr,(a,b) -> Integer.compare(a.price,b.price)); + long cur = 0,max = 0; + int left = 0,right = 0; + while(right < N){ + if(arr[right].price - arr[left].price < D){ + cur += arr[right++].value; + max = Math.max(max,cur); + }else{ + cur -= arr[left++].value; + } + } + bw.write(max+""); + bw.close(); + } +} +```