#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
os << '{';
for(int i=0;i<sz(a);i++)
{
if(i>0&&i<sz(a)-1)
os << ", ";
os << a[i];
}
os << '}';
return os;
}
int main()
{
int n,m,s;
scanf("%i %i %i",&n,&m,&s);
vector<pair<int,int> > pairs;
for(int i=0;i<n;i++)
{
int a,b;
scanf("%i %i",&a,&b);
pairs.emplace_back(a,b);
}
ll sol=0;
sort(all(pairs));
reverse(all(pairs));
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > taken;
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > not_taken_b;
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > not_taken_a;
for(int i=0;i<m;i++)
{
pair<int,int> p=pairs[i];
taken.insert({p.s-p.f,{p.f,p.s}});
sol+=(ll)p.f;
}
for(int i=m;i<n;i++)
{
pair<int,int> p=pairs[i];
not_taken_b.insert({p.s,{p.f,p.s}});
not_taken_a.insert({p.f,{p.f,p.s}});
}
for(int i=0;i<s;i++)
{
if(not_taken_a.size()==0)
assert(0);
int o1=(*not_taken_b.begin()).f;
int o2=(*taken.begin()).s.s-(*taken.begin()).s.f+(*not_taken_a.begin()).f;
if(o1>=o2)
{
if(o1>0)
{
sol+=(ll)o1;
int a=(*not_taken_b.begin()).s.f,b=(*not_taken_b.begin()).s.s;
not_taken_b.erase(not_taken_b.find({b,{a,b}}));
not_taken_a.erase(not_taken_a.find({a,{a,b}}));
}
}
else
{
if(o2>0)
{
sol+=(ll)o2;
taken.erase(taken.begin());
int a=(*not_taken_a.begin()).s.f,b=(*not_taken_a.begin()).s.s;
not_taken_b.erase(not_taken_b.find({b,{a,b}}));
not_taken_a.erase(not_taken_a.find({a,{a,b}}));
taken.insert({b-a,{a,b}});
}
}
}
printf("%lld\n",sol);
return 0;
}
Compilation message
school.cpp: In function 'int main()':
school.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&n,&m,&s);
~~~~~^~~~~~~~~~~~~~~~~~~~~
school.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&a,&b);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
888 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
4 ms |
760 KB |
Output is correct |
11 |
Correct |
7 ms |
1016 KB |
Output is correct |
12 |
Correct |
7 ms |
1016 KB |
Output is correct |
13 |
Correct |
25 ms |
3568 KB |
Output is correct |
14 |
Correct |
113 ms |
11120 KB |
Output is correct |
15 |
Correct |
261 ms |
23152 KB |
Output is correct |
16 |
Correct |
494 ms |
24804 KB |
Output is correct |
17 |
Correct |
340 ms |
26772 KB |
Output is correct |
18 |
Correct |
341 ms |
29284 KB |
Output is correct |
19 |
Correct |
372 ms |
32148 KB |
Output is correct |
20 |
Correct |
449 ms |
37796 KB |
Output is correct |