This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,m,ans=-1e18;
pii arr[N];
void Merge(int l,int r,int ql,int qr){
if (ql>qr) return;
multiset<int>st;
int sum=0;
if (l==r){
for (int i=l;i<=ql-1;i++){
sum+=arr[i].x;
st.insert(arr[i].x);
if (st.size()>m){
sum-=(*st.begin());
st.erase(st.begin());
}
}
for (int i=ql;i<=qr;i++){
sum+=arr[i].x;
st.insert(arr[i].x);
if (st.size()>m){
sum-=(*st.begin());
st.erase(st.begin());
}
assert(st.size()>=m);
ans=max(ans,sum-2*(arr[i].y-arr[l].y));
}
return;
}
int mid=(ql+qr)>>1,best=-1e18,from=0;
for (int i=mid;i>=l;i--){
sum+=arr[i].x;
st.insert(arr[i].x);
if (st.size()>m){
sum-=(*st.begin());
st.erase(st.begin());
if (best<sum-2*(arr[mid].y-arr[i].y)){
best=sum-2*(arr[mid].y-arr[i].y);
from=i;
}
}
}
ans=max(ans,best);
Merge(l,from,ql,mid-1);
Merge(from,r,mid+1,qr);
}
bool cmp(pii a,pii b){
if (a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
signed main(){
fast
cin>>n>>m;
for (int i=1;i<=n;i++)
cin>>arr[i].x>>arr[i].y;
sort(arr+1,arr+1+n,cmp);
Merge(1,n,m,n);
cout<<ans<<'\n';
}
Compilation message (stderr)
cake3.cpp: In function 'void Merge(long long int, long long int, long long int, long long int)':
cake3.cpp:27:26: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
27 | if (st.size()>m){
| ~~~~~~~~~^~
cake3.cpp:35:26: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
35 | if (st.size()>m){
| ~~~~~~~~~^~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from cake3.cpp:3:
cake3.cpp:39:29: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
39 | assert(st.size()>=m);
| ~~~~~~~~~^~~
cake3.cpp:48:22: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
48 | if (st.size()>m){
| ~~~~~~~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |