45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
import java.util.*;
|
|
|
|
class Jewel implements Comparable<Jewel> {
|
|
int weight, cost;
|
|
|
|
public Jewel(int weight, int cost) {
|
|
this.weight = weight;
|
|
this.cost = cost;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(Jewel o) {
|
|
return Integer.compare(o.cost, this.cost);
|
|
}
|
|
}
|
|
|
|
public class _1202 {
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
int N = sc.nextInt();
|
|
int K = sc.nextInt();
|
|
|
|
ArrayList<Jewel> jewels = new ArrayList<>();
|
|
for(int i=0; i<N; i++) jewels.add(new Jewel(sc.nextInt(), sc.nextInt()));
|
|
Collections.sort(jewels, (a,b) -> Integer.compare(a.weight, b.weight));
|
|
|
|
int[] bags = new int[K];
|
|
for(int i=0; i<K; i++) bags[i] = sc.nextInt();
|
|
Arrays.sort(bags);
|
|
|
|
sc.close();
|
|
|
|
long ans = 0;
|
|
int idx = 0;
|
|
PriorityQueue<Jewel> pq = new PriorityQueue<>();
|
|
|
|
for (int m : bags) {
|
|
while(idx < N && m >= jewels.get(idx).weight) pq.add(jewels.get(idx++));
|
|
if(!pq.isEmpty()) ans += pq.poll().cost;
|
|
}
|
|
|
|
System.out.println(ans);
|
|
}
|
|
}
|