• 문제 링크
https://www.acmicpc.net/problem/17503
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read();
Pair[] arr = new Pair[read()];
for (int i = 0; i < arr.length; i++) arr[i] = new Pair(read(), read());
Arrays.sort(arr);
bw.write(String.valueOf(getMin(n, m, arr)));
bw.flush();
}
private static int getMin(int n, int m, Pair[] arr) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
int sum = 0;
for (Pair p : arr) {
pq.add(p.v);
sum += p.v;
if (pq.size() > n) sum -= pq.remove();
if (pq.size() == n && sum >= m) return p.c;
}
return -1;
}
private static class Pair implements Comparable<Pair> {
int v, c;
Pair(int v, int c) {
this.v = v;
this.c = c;
}
@Override
public int compareTo(Pair o) {
return this.c - o.c;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 11645 I’ve Been Everywhere, Man - Data Structure / Java (0) | 2025.02.08 |
---|---|
[백준] 29813 최애의 팀원 - Data Structure / Java (0) | 2025.02.07 |
[백준] 30614 Port Robot - Data Structure / Java (0) | 2025.02.05 |
[백준] 14455 Don't Be Last! - Data Structure / Java (0) | 2025.02.04 |
[백준] 2511 카드놀이 - Implementation / Java (0) | 2025.02.03 |
댓글