#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+1];
for(int i=1;i<=n;i++)cin>>a[i];
int A[n+1];
for(int i=0;i<=n;i++)A[i]=0;
A[n]=1;
int ans=0;
ans+=a[n]+1-a[1];
k--;
while(k--){
vector<pair<int,int>>v;
int suf[n+1];
suf[n]=n;
for(int i=n-1;i>=1;i--){
if(A[i])suf[i]=i;
else suf[i]=suf[i+1];
}
int pret=1;
for(int i=1;i<=n;i++){
if(A[i]){
pret=i;
continue;
}
int R=ans;
int sl=suf[i];
R-=a[sl]+1-a[pret+1];
R+=a[sl]+1-a[i+1];
R+=a[i]+1-a[pret+1];
v.push_back({R,i});
}
sort(v.begin(),v.end());
int b=v[0].second;
A[b]=1;
int sl=-1;
for(int i=b+1;i<=n;i++){
if(A[i]){
sl=i;
break;
}
}
pret=1;
for(int i=1;i<b;i++){
if(A[i]){
pret=i;
}
}
ans-=a[sl]+1-a[pret+1];
ans+=a[sl]+1-a[b+1];
ans+=a[b]+1-a[pret+1];
}
cout<<ans;
}
/*
a[i]+1-a[pret];
pret=i+1;
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
18 ms |
604 KB |
Output is correct |
12 |
Correct |
149 ms |
600 KB |
Output is correct |
13 |
Correct |
244 ms |
604 KB |
Output is correct |
14 |
Correct |
269 ms |
600 KB |
Output is correct |
15 |
Correct |
270 ms |
648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
18 ms |
604 KB |
Output is correct |
12 |
Correct |
149 ms |
600 KB |
Output is correct |
13 |
Correct |
244 ms |
604 KB |
Output is correct |
14 |
Correct |
269 ms |
600 KB |
Output is correct |
15 |
Correct |
270 ms |
648 KB |
Output is correct |
16 |
Correct |
99 ms |
7372 KB |
Output is correct |
17 |
Execution timed out |
1006 ms |
7640 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |