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>
#define endl "\n"
#define finp for(int i=0; i<n; i++)
#define fknp for(int k=0; k<n; k++)
#define yeap cout<<"YES"<<endl
#define nope cout<<"NO"<<endl
#define inpintn int n; cin>>n
#define inpintarrn int arr[n]; for(int i=0; i<n; i++){cin>>arr[i];}
#define inpllarrn long long arr[n]; for(int i=0; i<n; i++){cin>>arr[i];}
#define ll long long
#define pb push_back
#define answer cout<<ans<<endl
#define INF (ll)(1e18)
using namespace std;
int main ()
{
ll n, k;
cin>>n>>k;
ll arr[n];
ll brr[n-1];
vector<ll> v;
vector<ll> v1;
for(int i=0; i<n; i++){
if(i==0){
cin>>arr[i];
} else {
cin>>arr[i];
brr[i-1] = arr[i] - arr[i-1];
}
}
ll strt = arr[0];
for(int i=0; i<n-1; i++){
if(i==n-2){
if(brr[i]==1){
ll aa = arr[i+1]+1;
v.push_back(aa);
v1.push_back(strt);
} else {
ll aa = arr[i]+1;
v.push_back(aa);
v1.push_back(strt);
strt = arr[n-1];
aa = strt+1;
v.push_back(aa);
v1.push_back(strt);
}
} else if(brr[i]!=1){
ll aa = arr[i]+1;
v.push_back(aa);
v1.push_back(strt);
strt = arr[i+1];
}
}
ll siz = v.size();
ll crr[siz-1];
ll x = siz - 1;
for(int i=0; i<siz-1; i++){
crr[i] = v1[i+1] - v[i];
}
sort(crr, crr+x);
ll startt = 0;
ll ans = n;
if(siz<=k){
cout<<ans<<endl;
} else {
while(siz>k){
ans+=crr[startt];
startt++;
siz--;
}
cout<<ans<<endl;
}
// for(int i=0; i<v.size(); i++){
// cout << v[i] << " ";
// }
// cout<<endl;
// for(int j=0; j<v1.size(); j++){
// cout << v1[j] << " ";
// }
// cout<<endl;
// for(int i=0; i<siz-1; i++){
// cout << crr[i] << " ";
// }
// cout<<endl;
// cout<<ans<<endl;
// cout << v1[2] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |