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")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'
llo n,m;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>m;
	vector<pair<llo,llo>> ss;
	for(llo i=0;i<n;i++){
		llo aa,bb;
		cin>>aa>>bb;
		ss.pb({bb,-aa});
	}
	sort(ss.begin(),ss.end());
	for(int i=0;i<n;i++){
		ss[i].b=-ss[i].b;
	}
	llo la=-1;
	llo ans=0;
/*	for(auto j:ss){
		cout<<j.a<<"."<<j.b<<endl;
	}*/
	for(llo i=0;i<n;i++){
		llo su=ss[i].b+2*ss[i].a;
		priority_queue<llo> tt;
		pair<llo,llo> ma={-1,-1};
		for(llo j=i+1;j<n;j++){
			while(tt.size()>m-2){
				su+=tt.top();
				tt.pop();
			}
		//	if(j>=la){
				if(tt.size()>=m-2){
					pair<llo,llo> cur={su+ss[j].b-ss[j].a*2,j};
					if(cur.a>ma.a){
						ma=cur;
					}
				/*	if(cur.a==20){
						cout<<i<<":"<<j<<endl;
					}*/
				}
			//}
			su+=ss[j].b;
			tt.push(-ss[j].b);
		}
		ans=max(ans,ma.a);
		la=ma.b;
	}
	cout<<ans<<endl;
 
 
	return 0;
}
 
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:35:19: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long long int'} [-Wsign-compare]
   35 |    while(tt.size()>m-2){
      |          ~~~~~~~~~^~~~
cake3.cpp:40:17: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'llo' {aka 'long long int'} [-Wsign-compare]
   40 |     if(tt.size()>=m-2){
      |        ~~~~~~~~~^~~~~
cake3.cpp:25:6: warning: variable 'la' set but not used [-Wunused-but-set-variable]
   25 |  llo la=-1;
      |      ^~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |