-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path1202.java
More file actions
76 lines (62 loc) · 2.16 KB
/
Copy path1202.java
File metadata and controls
76 lines (62 loc) · 2.16 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright@2023 Jihoon Lucas Kim <jihoon.lucas.kim@gmail.com>
// 보석 도둑
// https://www.acmicpc.net/problem/1202
// 힌트
// 1.가방과 상자 정렬을 무게에 대해서 오름차순으로 정렬하자.
// 2. 허용 무게가 가장 작은 가방부터 가방에서 담을 수 있는 보석의 가치를 모두 담는다.
// 3. 담을 수 있는 가치 중에서 가장 비싼 것을 해당 가방에 넣는다. (이를 위해 우선순위 큐를 이용하면 편리하다.)
// 4. 2~3을 반복하며 가방에서 담을 수 있는 보석에 대한 index를 하나씩 키워가며 추가로 담을 수 있는 보석을 우선순위 큐에 넣어준다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.PriorityQueue;
class Gem implements Comparable<Gem>{
int weight;
int price;
public Gem(int weight, int price) {
this.weight = weight;
this.price = price;
}
@Override
public int compareTo(Gem o) {
return this.weight - o.weight;
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
ArrayList<Gem> mv = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
mv.add(new Gem(M, V));
}
int[] bag = new int[K];
for (int i = 0; i < K; i++) {
bag[i] = Integer.parseInt(br.readLine());
}
Collections.sort(mv);
Arrays.sort(bag);
long answer = 0;
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
int index = 0;
for (int i = 0; i < K; i++) {
while (index < N && mv.get(index).weight <= bag[i]) {
q.add(mv.get(index).price);
index += 1;
}
if (q.size() > 0) {
answer += q.poll();
}
}
System.out.println(answer);
}
}