-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
81 lines (64 loc) Β· 2.03 KB
/
Main.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
72
73
74
75
76
77
78
79
80
81
package Heap.P1202;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N, K;
static Jewel[] jewelArr;
static int[] bag;
public static void main(String[] args) throws Exception{
// System.setIn(new FileInputStream("src/Heap/P1202/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
jewelArr = new Jewel[N];
bag = new int[K];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
jewelArr[i] = new Jewel(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
for (int i = 0; i < K; i++) {
bag[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(jewelArr, Comparator.comparingInt(Jewel::getWeight));
Arrays.sort(bag);
PriorityQueue<Jewel> pq = new PriorityQueue<>(Comparator.comparingInt(Jewel::getValue).reversed());
long sum = 0;
int cursor = 0;
for (int i = 0; i < K; i++) {
while (cursor < N && (bag[i] >= jewelArr[cursor].weight)){
pq.add(jewelArr[cursor++]);
}
if (!pq.isEmpty()){
sum += pq.poll().value;
}
}
System.out.println(sum);
}
}
class Jewel {
int weight;
int value;
public int getWeight() {
return weight;
}
public int getValue() {
return value;
}
public Jewel(int weight, int value) {
this.weight = weight;
this.value = value;
}
@Override
public String toString() {
return "Jewel{" +
"weight=" + weight +
", value=" + value +
'}';
}
}