Submission #337009

#TimeUsernameProblemLanguageResultExecution timeMemory
337009alishahali1382Cake 3 (JOI19_cake3)C++14
100 / 100
1898 ms41052 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, LOG=20;

ll ans=-INF;
int n, m, k, u, v, x, y, t, a, b, shit;
pii A[MAXN];
int mark[MAXN];

struct Set{
//	multiset<int> X, Y;
	priority_queue<pii, vector<pii>, greater<pii>> X;
	priority_queue<pii> Y;  
	ll sum=0, sz=0;
	inline void add(int i){
//		if (shit++==10000000) cout<<1/0;
		int x=A[i].second;
		X.push({x, i});
		mark[i]=1;
		sum+=x;
		sz++;
		while (sz>k){
			pii p=X.top();
			X.pop();
			if (mark[p.second]!=1) continue ;
			sz--;
			sum-=p.first;
			mark[p.second]=2;
			Y.push(p);
		}
	}
	inline void rem(int i){
		int x=A[i].second;
//		if (shit++==10000000) cout<<1/0;
		if (mark[i]==2){
			mark[i]=0;
			return ;
		}
		mark[i]=0;
		sum-=x;
		sz--;
		while (sz<k && Y.size()){
			pii p=Y.top();
			Y.pop();
			if (mark[p.second]!=2) continue ;
			mark[p.second]=1;
			sz++;
			sum+=p.first;
			X.push(p);
		}
	}
} S;
inline void Move(int L, int R, int l, int r){
	while (l<L) S.add(--L);
	while (R<r) S.add(R++);
	while (L<l) S.rem(L++);
	while (r<R) S.rem(--R);
}
void divide(int tl, int tr, int optl, int optr){
	if (tr<tl) return ;
	int mid=(tl+tr)>>1, opt=-1;
	ll val=-INF;
	for (int i=tl; i<=mid; i++) S.add(i);
	for (int i=optl; i<=min(mid-k+1, optr); i++){
		ll c=S.sum - 2*(A[mid].first-A[i].first);
		if (c>val){
			val=c;
			opt=i;
		}
		S.rem(i);
	}
	ans=max(ans, val);
	for (int i=optl; i<=min(mid-k+1, optr); i++) S.add(i);
	Move(optl, mid+1, opt, mid+1);
	divide(mid+1, tr, opt, optr);
	Move(opt, mid+1, optl, tl);
	divide(tl, mid-1, optl, opt);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//	freopen("test.in", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<=n; i++) cin>>A[i].second>>A[i].first;
	sort(A+1, A+n+1);
	for (int i=1; i<k; i++) S.add(i);
	divide(k, n, 1, n-k+1);
	cout<<ans<<"\n";
	
	return 0;
}
/*
5 3
2 1
4 2
6 4
8 8
10 16



*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...