#include<iostream>
#include<vector>
#include<algorithm>
#define MAXN 100010
using namespace std;
vector<pair<int,int>> order;
vector<int> indx;
int a[MAXN];
int main()
{
int n,k;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(i>1)order.push_back({a[i]-a[i-1],i});
}
if(k==1){cout<<a[n]+1-a[1]<<"\n";return 0;}
if(k==n){cout<<n<<"\n";return 0;}
sort(order.begin(),order.end());
int cntr=0;
long long ans=0;
for(int i=order.size()-1;i>=0;i--)
{
int val=order[i].first;
int ind=order[i].second;
cntr++;
indx.push_back(ind);
if(cntr>=k-1)break;
}
sort(indx.begin(),indx.end());
indx.push_back(0);
int curr=0,last=1;
for(int i=1;i<=n;i++)
{
if(i==indx[curr])
{
ans+=a[i-1]-a[last]+1;
last=i;
curr++;
}
}
ans+=a[n]-a[last]+1;
cout<<ans<<"\n";
}
Compilation message
stove.cpp: In function 'int main()':
stove.cpp:27:13: warning: unused variable 'val' [-Wunused-variable]
27 | int val=order[i].first;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
532 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
532 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
17 ms |
2524 KB |
Output is correct |
17 |
Correct |
16 ms |
2524 KB |
Output is correct |
18 |
Correct |
15 ms |
2548 KB |
Output is correct |
19 |
Correct |
17 ms |
2744 KB |
Output is correct |
20 |
Correct |
19 ms |
3040 KB |
Output is correct |
21 |
Correct |
22 ms |
3288 KB |
Output is correct |
22 |
Correct |
23 ms |
3284 KB |
Output is correct |
23 |
Correct |
23 ms |
3288 KB |
Output is correct |
24 |
Correct |
34 ms |
3280 KB |
Output is correct |