-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp1202.java
More file actions
56 lines (52 loc) · 1.87 KB
/
p1202.java
File metadata and controls
56 lines (52 loc) · 1.87 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
import java.util.*;
import java.io.*;
public class p1202 {
static class Pair implements Comparable<Pair>{
int w, c;
public Pair (int w, int c) {
this.w = w;
this.c = c;
}
public int compareTo(Pair o) {
return w - o.w;
}
}
static int n, k;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
PriorityQueue<Pair> jew = new PriorityQueue<>();
PriorityQueue<Integer> bag = new PriorityQueue<>();
while (n-- > 0) {
st = new StringTokenizer(br.readLine());
int w = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
jew.offer(new Pair(w, c));
}
while (k-- > 0) {
bag.offer(Integer.parseInt(br.readLine()));
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
long ans = 0;
while (!bag.isEmpty()) {
int bagSize = bag.poll();
while (!jew.isEmpty()) {
Pair cur = jew.poll();
// 꺼낸 보석의 무게가 가방의 무게를 초과한다면 보석을 다시 집어 넣고 다음 보석을 탐색한다.
if (cur.w > bagSize) {
jew.offer(cur);
break;
} else {
// 나중에 오름차순으로 빼기 위해서 -1을 곱하고 넣어준다.
pq.offer(cur.c * -1);
}
}
if (!pq.isEmpty()) {
ans += pq.poll();
}
}
System.out.println(ans * -1);
}
}