이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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+1; i < r; i++){
v.push_back(pieces[i].first);
}
sort(v.begin(), v.end());
long long sum = pieces[l].first + pieces[r].first;
for(int i = 2; i < m; 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; i <= min(mid-m+1,rb); i++){
long long cur = query(i, mid) + 2ll * pieces[i].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 == -1 ? rb : mindex));
if(mid+1 <= r) ans = max(ans, solve(mid+1, r, mindex == -1 ? lb : 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;
}
컴파일 시 표준 에러 (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... |