#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
/*
if(n<=20){
int ans=1e18;
for(int i=0;i<(1LL<<n);i++){
if(__builtin_popcount(i)>k)continue;
if(__builtin_popcount(i)<k)continue;
int R=0;
int pret=0;
for(int j=0;j<n;j++){
if(i&(1LL<<j)){
// ovde gasim
R+=(a[j]+1)-(a[pret]);
pret=j+1;
}
}
//return 0;
if(i&(1LL<<(n-1))){
//cout<<i<<" "<<R;
//cout<<endl;
ans=min(ans,R);
//cout<<i<<" "<<R;
//cout<<endl;
}
}
cout<<ans;
return 0;
}*/
priority_queue<pair<int,pair<int,int>>>pq;
pq.push({(a[n-1]+1)-a[0],{0,n-1}});
int pozicija[n];
for(int i=0;i<n;i++)pozicija[i]=0;
pozicija[n-1]=1;
k--;
while(k--){
int vrednost=pq.top().first;
int l=pq.top().second.first;
int r=pq.top().second.second;
//cout<<vrednost<<" "<<l<<" "<<r;
//cout<<":::";
pq.pop();
int pret=-1;
for(int i=l;i>=0;i--){
if(pozicija[i]){
pret=i;
break;
}
}
if(pret==-1)pret=0;
else pret++;
int R=1e18;
for(int i=l;i<r;i++){
int V=(a[i]+1)-a[pret];
int V1=(a[r]+1)-a[i+1];
R=min(R,V+V1);
}
int ind=-1;
for(int i=l;i<r;i++){
int V=(a[i]+1)-a[pret];
int V1=(a[r]+1)-a[i+1];
if(R==V+V1){
ind=i;
}
}
pozicija[ind]=1;
pq.push({(a[ind]+1)-a[pret],{l,ind}});
pq.push({(a[r]+1)-a[ind+1],{ind+1,r}});
}
int ans=0;
int pret=0;
for(int i=0;i<n;i++){
if(pozicija[i]){
ans+=(a[i]+1)-a[pret];
pret=i+1;
}
}
cout<<ans;
}
Compilation message
stove.cpp: In function 'int main()':
stove.cpp:45:13: warning: unused variable 'vrednost' [-Wunused-variable]
45 | int vrednost=pq.top().first;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |