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>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
multiset<ll>st;
pair<ll,ll>a[200005];
ll n,m,ans=-9999999999999999;
void solve(ll l,ll r,ll x,ll y){
l = max(l , x + m - 1);
if(l > r || y < x + m - 1)return;
ll k = (l + r) / 2;
ll mx = -9999999999999999 , ind = y;
st.clear();
ll cur = 0;
for(int i=k; i>=x; i--){
ll mn = 0;
if(st.size())mn = (*st.begin());
if(mn < a[i].s || ((int)st.size() < m)){
cur += a[i].s;
if(st.size() >= m){st.erase(st.begin());cur -= mn;}
st.insert(a[i].s);
}
if(st.size() >= m && cur - 2 * (a[k].f - a[i].f) > mx)
mx = cur - 2 * (a[k].f - a[i].f),
ind = i;
}
if(mx == -9999999999999999)return;
ans = max(ans , mx);
if(l == r)return;
solve(l , k , x , ind);
solve(k + 1 , r , ind , y);
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=1; i<=n; i++){
cin >> a[i].s >> a[i].f;
}
sort(a + 1 , a + n + 1);
solve(1 , n , 1 , n);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'void solve(long long int, long long int, long long int, long long int)':
cake3.cpp:22:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(st.size() >= m){st.erase(st.begin());cur -= mn;}
~~~~~~~~~~^~~~
cake3.cpp:25:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(st.size() >= m && cur - 2 * (a[k].f - a[i].f) > mx)
~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |