-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1202.java
71 lines (52 loc) · 1.68 KB
/
1202.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
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
String[] in = br.readLine().split(" ");
int N = Integer.parseInt(in[0]);
int K = Integer.parseInt(in[1]);
LinkedList<Gem> gems = new LinkedList<>();
for (int n = 0; n < N; n++) {
in = br.readLine().split(" ");
int M = Integer.parseInt(in[0]);
int V = Integer.parseInt(in[1]);
gems.add(new Gem(M, V));
}
int[] bags = new int[K];
for (int k = 0; k < K; k++) {
int C = Integer.parseInt(br.readLine());
bags[k] = C;
}
Collections.sort(gems);
Arrays.sort(bags);
long answer = 0L;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int bag : bags) {
while (!gems.isEmpty() && gems.peek().M <= bag) {
pq.add(-gems.remove().V);
}
if (!pq.isEmpty()) {
answer += -pq.remove();
}
}
bw.write(answer + "\n");
bw.close();
}
private static class Gem implements Comparable<Gem> {
int M;
int V;
public Gem(int M, int V) {
this.M = M;
this.V = V;
}
@Override
public int compareTo(Gem other) {
if (this.M == other.M) {
return this.V - other.V;
}
return this.M - other.M;
}
}
}