반응형
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static class Hw implements Comparable<Hw>{
int d;
int w;
public Hw(int d, int w) {
this.d = d;
this.w = w;
}
@Override
public int compareTo(Hw hw) {
return hw.w < w ? 1 : (hw.w > w ? -1 : 0);
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer str;
List<Hw> hlist = new ArrayList<>();
for(int i = 0; i < n; i++) {
str = new StringTokenizer(br.readLine(), " ");
hlist.add(new Hw(Integer.parseInt(str.nextToken()), Integer.parseInt(str.nextToken())));
}
Collections.sort(hlist, new Comparator<Hw>() {
@Override
public int compare(Hw hw1, Hw hw2) {
return hw1.d >= hw2.d ? 1 : -1;
}
});
PriorityQueue<Hw> queue = new PriorityQueue<>();
queue.add(hlist.remove(0));
while(!hlist.isEmpty()) {
Hw hw = hlist.remove(0);
if(hw.d == queue.size()) {
if(hw.w > queue.peek().w) {
queue.poll();
queue.add(hw);
}
}
else {
queue.add(hw);
}
}
int sum = 0;
while(!queue.isEmpty()) {
sum += queue.poll().w;
}
System.out.println(sum);
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 1918 : 골드2 (0) | 2023.09.20 |
---|---|
[백준] 자바 문제 풀이 17144 : 골드4 (0) | 2023.09.20 |
[백준] 자바 문제 풀이 20040 : 골드4 (0) | 2023.09.20 |
[백준] 자바 문제 풀이 16562 : 골드4 (0) | 2023.09.20 |
[백준] 자바 문제 풀이 3190 : 골드4 (0) | 2023.09.19 |