baekjoon 20251221
This commit is contained in:
parent
003d253779
commit
cb2a409fa9
44
code_study/Baekjoon/java/_1202.java
Normal file
44
code_study/Baekjoon/java/_1202.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user