-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathProgrammers and Artists.cpp
62 lines (51 loc) · 1.52 KB
/
Programmers and Artists.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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxN = 2e5+5;
int N, A, B;
pii cand[maxN];
multiset<int> initial_artist_set(){
vector<int> all;
for(int i = A; i < N; i++)
all.push_back(cand[i].second);
sort(all.begin(), all.end());
reverse(all.begin(), all.end());
multiset<int> best;
for(int i = 0; i < B; i++)
best.insert(all[i]);
return best;
}
int main(){
scanf("%d %d %d", &A, &B, &N);
for(int i = 0, x, y; i < N; i++){
scanf("%d %d", &x, &y);
cand[i] = {x, y};
}
sort(cand, cand+N, [](const pii a, const pii b){
return a.first == b.first ? a.second < b.second : a.first > b.first;
});
ll pref = 0;
priority_queue<int> prefix_deltas;
for(int i = 0; i < A; i++){
pref += cand[i].first;
prefix_deltas.push(cand[i].second - cand[i].first);
}
multiset<int> remaining_art = initial_artist_set();
ll suf = accumulate(remaining_art.begin(), remaining_art.end(), 0LL);
ll ans = pref + suf;
for(int i = A; i < A+B; i++){
const int x = cand[i].first;
const int y = cand[i].second;
pref += x;
prefix_deltas.push(y - x);
pref += prefix_deltas.top();
prefix_deltas.pop();
auto worst_artist = remaining_art.lower_bound(y);
int art_value = *worst_artist;
remaining_art.erase(worst_artist);
suf -= art_value;
ans = max(ans, pref + suf);
}
printf("%lld\n", ans);
}