This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define MP make_pair
#define PB push_back
int N,M;
pair<LL,LL> VC[200000];
int main(){
cin >> N >> M;
for(int i=0; i<N; i++){
cin >> VC[i].first >> VC[i].second;
}
sort(VC, VC+N);
//
LL now = 0;
multiset<pair<int,int>> st;
for(int i=0; i<M; i++){
now += VC[N-1-i].first;
st.insert(MP(VC[N-1-i].second,VC[N-1-i].first));
}
auto itr = st.end();
itr--;
//now -= 2*(itr->first-st.begin()->first);
//cout << now << endl;
LL ans = now-2*(itr->first-st.begin()->first);
LL bef = ans;
for(int i=N-1-M; i>=0; i--){
now += VC[i].first;
st.insert(MP(VC[i].second,VC[i].first));
LL tmpL,tmpR;
tmpL = now;
tmpL -= st.begin()->second;
auto itr = st.end();
itr--;
auto it = st.begin();
it++;
tmpL -= 2*(itr->first-it->first);
tmpR = now;
itr = st.end();
itr--;
tmpR -= itr->second;
itr--;
it = st.begin();
tmpR -= 2*(itr->first-it->first);
if(bef > tmpL && bef > tmpR){
now -= VC[i].first;
st.erase(MP(VC[i].second,VC[i].first));
}
if(tmpL > tmpR){
now -= st.begin()->second;
st.erase(st.begin());
}
else{
it = st.end();
it--;
now -= it->second;
st.erase(it);
}
itr = st.end(); itr--;
ans = max(ans, now-2*(itr->first-st.begin()->first));
bef = now-2*(itr->first-st.begin()->first);
//cout << tmpL << " " << tmpR << endl;
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |