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;
int n,m;
vector<pair<int,int> > pieces;
long long query(int l, int r){
vector<int> v;
for(int i = l; i <= r; i++){
v.push_back(pieces[i].first);
}
sort(v.begin(), v.end());
long long sum = 0ll;
for(int i = 0; i < m-2; i++){
sum += 1ll * v.back();
v.pop_back();
}
return sum;
}
long long solve(int l, int r, int lb, int rb){
int mid = (l + r)/2;
long long ans = -1e18;
int mindex = -1;
for(int i = lb+1; i <= min(mid-m+2,rb); i++){
long long cur = query(i, mid-1) + 1ll * pieces[i-1].first + 1ll * pieces[mid].first + 2ll * pieces[i-1].second - 2ll * pieces[mid].second;
if(cur > ans){
ans = cur;
mindex = i;
}
}
if(l <= mid-1) ans = max(ans, solve(l, mid-1, lb, mindex));
if(mid+1 <= r) ans = max(ans, solve(mid+1, r, mindex, rb));
return ans;
}
int main(){
scanf("%d%d",&n,&m);
int v,c;
for(int i = 0; i < n; i++){
scanf("%d%d",&v,&c);
pieces.emplace_back(v,c);
}
stable_sort(pieces.begin(), pieces.end(), [](pair<int,int> x, pair<int,int> y){
return x.second < y.second;
});
long long ans = solve(m-1,n-1,0,n-1);
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
cake3.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&v,&c);
~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |