반응형
BOJ 2109 풀이 코드
import java.io.*;
import java.util.*;
public class Main {
static class Lecture {
int cost;
int day;
Lecture(int cost, int day) {
this.cost = cost;
this.day = day;
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
PriorityQueue<Lecture> deadline = new PriorityQueue<>((o1, o2) -> o1.day - o2.day);
for (int i = 0; i < n; i++) {
StringTokenizer str = new StringTokenizer(br.readLine(), " ");
deadline.add(new Lecture(Integer.parseInt(str.nextToken()), Integer.parseInt(str.nextToken())));
}
PriorityQueue<Lecture> rst = new PriorityQueue<>((o1, o2) -> o1.cost - o2.cost);
if (!deadline.isEmpty()) {
rst.add(deadline.poll());
while (!deadline.isEmpty()) {
Lecture l = deadline.poll();
if (l.day == rst.size()) {
if (l.cost > rst.peek().cost) {
rst.poll();
rst.add(l);
}
} else {
rst.add(l);
}
}
}
int total = 0;
while (!rst.isEmpty()) {
total += rst.poll().cost;
}
bw.write(total + "\n");
bw.flush();
bw.close();
br.close();
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 10775 : 골드2 (0) | 2023.09.23 |
---|---|
[백준] 자바 문제 풀이 11505 : 골드1 (0) | 2023.09.23 |
[백준] 자바 문제 풀이 17822 : 골드2 (0) | 2023.09.22 |
[백준] 자바 문제 풀이 1715 : 골드4 (0) | 2023.09.22 |
[백준] 자바 문제 풀이 12837 : 골드1 (0) | 2023.09.22 |