Submission #954731

#TimeUsernameProblemLanguageResultExecution timeMemory
954731starchanSchools (IZhO13_school)C++17
100 / 100
102 ms16588 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pq priority_queue
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define MX (int)3e5+5
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
struct ritul
{
	int Limit;
	int sz;
	int sum;
	pq<int> chungus;
	void init(int x)
	{
		Limit = x;
		sz = 0;
		while(!chungus.empty())
			chungus.pop();
		sum = 0;
		return;
	}
	void push(int x)
	{
		chungus.push(-x);
		sz++;
		sum+=x;
		if(sz > Limit)
		{
			sum+=chungus.top();
			sz--;
			chungus.pop();
		}
		return;
	}
	int ans()
	{
		return sum;
	}
};
signed main()
{
	fast(); ritul work1, work2;
	int n, A, B; cin >> n >> A >> B;
	vector<int> a(n+1), b(n+1), d(n+1, 0), e(n+1, 0); vector<in> c(n+1);
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i] >> b[i];
		c[i] = {b[i]-a[i], i};
	}
	if((A == 0) && (B == 0))
	{
		cout << "0\n";
		return 0;
	}
	else if((A == 0))
	{
		b[0] = INF;
		sort(b.rbegin(), b.rend());
		int ans = 0;
		for(int i = 1; i <= B; i++)
			ans+=b[i];
		cout << ans << "\n";
		return 0;
	}
	else if((B == 0))
	{
		a[0] = INF;
		sort(a.rbegin(), a.rend());
		int ans = 0;
		for(int i = 1; i <= A; i++)
			ans+=a[i];
		cout << ans << "\n";
		return 0;
	}
	c[0] = {-INF, -INF};
	sort(c.begin(), c.end());
	work1.init(A);
	for(int i = 1; i <= n; i++)
	{
		work1.push(a[c[i].s]);
		d[i] = work1.ans();
	}
	work2.init(B);
	for(int i = n; i >= 1; i--)
	{
		work2.push(b[c[i].s]);
		e[i] = work2.ans();
	}
	int ans = -1;
	for(int i = A; i <= n-B; i++)
		ans = max(ans, d[i]+e[i+1]);
	cout << ans << "\n";
	return 0;
}			
#Verdict Execution timeMemoryGrader output
Fetching results...