-
Notifications
You must be signed in to change notification settings - Fork 0
/
[BOJ] 1202 보석도둑(힙).cpp
49 lines (42 loc) · 1018 Bytes
/
[BOJ] 1202 보석도둑(힙).cpp
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
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
struct jewel {
int weight; // 무게
int price; // 가격
int check; // 0==보석, 1==가방
};
bool comp(jewel a, jewel b) {
return a.weight < b.weight || (a.weight == b.weight && a.check < b.check);
}
int main () {
int N, K;
cin >> N >> K;
vector<jewel> a(N+K);
for(int i = 0; i < N; i++) {
cin >> a[i].weight >> a[i].price;
}
for (int i = 0; i < K; i++) {
cin >> a[i+N].weight;
a[i+N].check = 1;
}
sort(a.begin(), a.end(), comp); // 가격이 낮은 보석부터
priority_queue<int> q; // 가방 무게보다 작은 것만 들어감.
long long ans = 0;
for (auto &p : a) {
if (p.check == 0) { // 보석이면 무게를 넣는다.
q.push(p.price);
}
else {
if (!q.empty()) {
ans += (long long)q.top();
q.pop();
}
}
}
cout << ans << endl;
return 0;
}