#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define dec(x, y) fixed << setprecision((y)) << (x)
#define xx first
#define yy second
#define srt(v) sort((v).begin(), (v).end())
#define srtr(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define popb pop_back
#define sz(a) (int)(a).size()
#define len(a) (int)(a).length()
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, m, s;
pll x[300010];
set<pll> naja, najb, raz;
ll rez;
int main() {
ios;
cin >> n >> m >> s;
for(int i=0; i<n; ++i) {
cin >> x[i].xx >> x[i].yy;
}
sort(x, x+n, greater<pll>());
for(int i=0; i<m; ++i) {
rez+=x[i].xx;
raz.insert({x[i].yy-x[i].xx, (ll)i});
}
for(int i=m; i<n; ++i) {
naja.insert({x[i].xx, (ll)i});
najb.insert({x[i].yy, (ll)i});
}
while(true) {
if(s==0) break;
bool uzeo1=0, uzeo2=0;
pll pr, dr, tr;
if(!najb.empty()) {
pr=*prev(najb.end());
uzeo1=1;
}
if(!raz.empty() && !naja.empty()) {
dr=*prev(raz.end());
tr=*prev(naja.end());
uzeo2=1;
}
if(!uzeo1 && !uzeo2) break;
else if(!uzeo2 && pr.xx>0LL) {
rez+=pr.xx;
--s;
if(!najb.empty()) najb.erase(prev(najb.end()));
auto it=naja.find({x[pr.yy].xx, pr.yy});
if(it!=naja.end() && !naja.empty()) naja.erase(it);
}
else if(!uzeo1 && dr.xx+tr.xx>0LL) {
rez+=dr.xx+tr.xx;
--s;
if(!raz.empty()) raz.erase(prev(raz.end()));
if(!naja.empty()) naja.erase(prev(naja.end()));
raz.insert({x[tr.yy].yy-x[tr.yy].xx, tr.yy});
}
else if(pr.xx>dr.xx+tr.xx && pr.xx>0LL) {
rez+=pr.xx;
--s;
if(!najb.empty()) najb.erase(prev(najb.end()));
auto it=naja.find({x[pr.yy].xx, pr.yy});
if(it!=naja.end() && !naja.empty()) naja.erase(it);
}
else if(dr.xx+tr.xx>0LL) {
rez+=dr.xx+tr.xx;
--s;
if(!raz.empty()) raz.erase(prev(raz.end()));
if(!naja.empty()) naja.erase(prev(naja.end()));
raz.insert({x[tr.yy].yy-x[tr.yy].xx, tr.yy});
}
else break;
}
cout << rez;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
5 ms |
844 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
716 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
716 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
716 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
892 KB |
Output isn't correct |
12 |
Incorrect |
6 ms |
1052 KB |
Output isn't correct |
13 |
Incorrect |
29 ms |
3780 KB |
Output isn't correct |
14 |
Incorrect |
106 ms |
11588 KB |
Output isn't correct |
15 |
Correct |
233 ms |
24392 KB |
Output is correct |
16 |
Incorrect |
412 ms |
26160 KB |
Output isn't correct |
17 |
Incorrect |
293 ms |
28392 KB |
Output isn't correct |
18 |
Incorrect |
322 ms |
31072 KB |
Output isn't correct |
19 |
Incorrect |
381 ms |
34116 KB |
Output isn't correct |
20 |
Incorrect |
480 ms |
39808 KB |
Output isn't correct |