-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy path[18]_3.java
58 lines (48 loc) · 1.19 KB
/
[18]_3.java
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
class Node {
public int x;
public int y;
Node (int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int result = 0;
ArrayList<Node> data = new ArrayList<Node>();
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
// 문제 정보를 입력 받은 이후에, 데드라인을 기준으로 정렬
for (int i = 0; i < n; i++) {
data.add(new Node(sc.nextInt(), sc.nextInt()));
}
Collections.sort(data, new Comparator<Node>() {
@Override
public int compare(Node a, Node b) {
if (a.x == b.x) {
return Integer.compare(a.y, b.y);
}
return Integer.compare(a.x, b.x);
}
});
for (int i = 0; i < n; i++) {
int a = data.get(i).x;
int b = data.get(i).y;
pq.offer(b);
// 데드라인을 초과하는 경우에는 최소 원소를 제거
if (a < pq.size()) {
pq.poll();
}
}
while (!pq.isEmpty()) {
result += pq.poll();
}
System.out.println(result);
}
}