|
| 1 | +package study.week05; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.PriorityQueue; |
| 7 | + |
| 8 | +// 컵라면 |
| 9 | +public class BOJ_1781 { |
| 10 | + |
| 11 | + static class Problem implements Comparable<Problem> { |
| 12 | + |
| 13 | + int deadLine; |
| 14 | + int cup; |
| 15 | + |
| 16 | + public Problem(int deadLine, int cup) { |
| 17 | + this.deadLine = deadLine; |
| 18 | + this.cup = cup; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public int compareTo(Problem o) { |
| 23 | + if (this.deadLine == o.deadLine) { |
| 24 | + return o.cup - this.cup; |
| 25 | + } |
| 26 | + return this.deadLine - o.deadLine; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static PriorityQueue<Problem> pq; |
| 31 | + static int N; |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 35 | + |
| 36 | + N = Integer.parseInt(br.readLine()); |
| 37 | + pq = new PriorityQueue<>(); |
| 38 | + |
| 39 | + for (int i = 0; i < N; i++) { |
| 40 | + String[] s = br.readLine().split(" "); |
| 41 | + int deadLine = Integer.parseInt(s[0]); |
| 42 | + int cup = Integer.parseInt(s[1]); |
| 43 | + pq.add(new Problem(deadLine, cup)); |
| 44 | + } |
| 45 | + |
| 46 | + PriorityQueue<Integer> select = new PriorityQueue<>(); |
| 47 | + |
| 48 | + while(!pq.isEmpty()){ |
| 49 | + Problem problem = pq.poll(); |
| 50 | + int deadLine = problem.deadLine; |
| 51 | + int cup = problem.cup; |
| 52 | + |
| 53 | + if(select.size() < deadLine){ |
| 54 | + select.offer(cup); |
| 55 | + }else{ |
| 56 | + if(!select.isEmpty() && select.peek() < cup){ // 만약 선택한 문제 중 가장 적은 컵라면보다 현재 추가하려는 problem 의 컵라면 개수가 더 많다면 |
| 57 | + select.poll(); |
| 58 | + select.offer(cup); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + long result = 0; |
| 64 | + for(int cup : select){ |
| 65 | + result += cup; |
| 66 | + } |
| 67 | + |
| 68 | + System.out.println(result); |
| 69 | + } |
| 70 | +} |
0 commit comments