#include<bits/stdc++.h>
using namespace std;
#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<
template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}
const int N=202020;
int n,m; ll res=-1e18;
ii cake[N];
ll cost(int i, int j){
if(j-i+1<m) return -1e18;
vi hihi;
foru(k,i,j) hihi.eb(cake[k].fi);
sort(all(hihi),greater<int>());
ll tot=0;
rep(k,m) tot+=hihi[k];
return tot-2*(cake[j].se-cake[i].se);
}
void calc(int L, int R, int optL, int optR){
if(L>R) return;
int M=(L+R)>>1;
ll pos=0, ans=-1e18;
foru(opt,optL,min(optR,M)){
if(maxi(ans,cost(opt,M))){
pos=opt;
}
}
maxi(res,ans);
calc(L,M-1,optL,pos);
calc(M+1,R,pos,optR);
}
void solve(){
cin>>n>>m;
foru(i,1,n) cin>>cake[i].fi>>cake[i].se;
sort(cake+1, cake+1+n, [](ii x, ii y){return x.se<y.se;});
calc(m,n,1,n);
cout<<res<<'\n';
}
int32_t main(){
#define task "test"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
foru(i,1,tc){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int32_t main()':
cake3.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |