#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
using namespace std;
const int L=1e7+11;
const ll N=2e4+10;
const ll mod=1e9+7;
const ll inf=3e18;
const double pi=3.14159265359;
ll lcm(ll a, ll b){ return (a/__gcd(a,b))*b; }
void solve(){
int n,k;
cin>>n>>k;
int a[n+1];
for(int i=1; i<=n; i++){
cin>>a[i];
}
if(k>=n){
cout<<n<<endl;
return;
}vector<int>b;
int ans=k,d=n-k,i=0;
for(int i=2; i<=n; i++){
b.pb(a[i]-a[i-1]);
}sort(b.begin(), b.end());
while(i<d){
ans+=b[i];
i++;
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", " w", stdout);
int tt=1;
// cin>>tt;
while(tt--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
14 ms |
2268 KB |
Output is correct |
17 |
Correct |
15 ms |
2272 KB |
Output is correct |
18 |
Correct |
15 ms |
2364 KB |
Output is correct |
19 |
Correct |
14 ms |
2272 KB |
Output is correct |
20 |
Correct |
14 ms |
2272 KB |
Output is correct |
21 |
Correct |
15 ms |
2344 KB |
Output is correct |
22 |
Correct |
14 ms |
2368 KB |
Output is correct |
23 |
Correct |
17 ms |
2240 KB |
Output is correct |
24 |
Correct |
16 ms |
2272 KB |
Output is correct |