-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueUse.java
More file actions
62 lines (48 loc) · 1.47 KB
/
PriorityQueueUse.java
File metadata and controls
62 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
public class PriorityQueueUse {
public static int solution(int max, List<Lecture> arr, int n) {
int answer = 0;
Collections.sort(arr);
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
int j = 0;
for (int i = max; i > 0; i--) {
for (; j < n; j++) {
if (arr.get(j).period < i) {
break;
}
queue.offer(arr.get(j).money);
}
if (!queue.isEmpty()) {
answer += queue.poll();
}
}
return answer;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int max = Integer.MIN_VALUE;
List<Lecture> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
int m = scanner.nextInt();
int p = scanner.nextInt();
if (p > max) {
max = p;
}
arr.add(new Lecture(m, p));
}
System.out.println(solution(max, arr, n));
}
public static class Lecture implements Comparable<Lecture> {
int money;
int period;
public Lecture(int money, int period) {
this.money = money;
this.period = period;
}
@Override
public int compareTo(Lecture o) {
return o.period - this.period;
}
}
}