/*
ID: Yassine BenYounes
TASK: guard
LANG: C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
vi primes;
const int ppppp = 3e6+9;
ll mod = 1e9+7;
bool prime[ppppp];
ll fact[ppppp];
void sieve(int n){
memset(prime, 1, sizeof prime);
prime[1] = 0;
for(int i = 2; i < n;i++){
if(!prime[i])continue;
primes.pb(i);
for(int j = 2*i;j < n;j+=i){
prime[j] = 0;
}
}
}
vi factors(int n){
vi res;
for(int i = 0; i < primes.size() && primes[i]*primes[i] <= n;i++){
if(n % primes[i] == 0)res.pb(primes[i]);
while(n % primes[i] == 0){
n /= primes[i];
}
}
if(n > 1)res.pb(n);
return res;
}
ll binpow(ll a, ll b){
if(b == 0)return 1;
ll x = binpow(a, b/2) % mod;
ll g = x*x;
g %= mod;
if(b%2 == 0)return g;
g*=a;
g%=mod;
return g;
}
ll modinv(ll a){
return binpow(a, mod-2);
}
ll sub(ll a, ll b){
ll c = (a%mod)-(b%mod);
while(c < 0)c+=mod;
return c;
}
ll add(ll a, ll b){
return (((a%mod)+(b%mod)) % mod);
}
ll mult(ll a, ll b){
a %= mod;
b %= mod;
ll c = a*b;
c %= mod;
return c;
}
ll divide(ll a, ll b){
a %= mod;
b = modinv(b) % mod;
ll c = a*b;
c%=mod;
return c;
}
void pre_fact(int n){
fact[0]=1;
for(int i = 1; i < n;i++){
fact[i] = mult(fact[i-1],i);
}
}
ll choose(ll k, ll n){
ll c = mult(mult(fact[n],modinv(fact[k])), modinv(fact[n-k]));
return c;
}
ll perm(ll n){
return fact[n];
}
ll arrange(ll k, ll n){
ll c = mult(fact[n],modinv(fact[n-k]));
return c;
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 50;
const int LOG = 25;
const int inf = 1e9;
int arr[mx];
void run_case(){
int n, k;cin >> n >> k;vi v;
for(int i = 0; i < n;i++){
cin >> arr[i];
if(i>0)v.pb(arr[i]-arr[i-1]-1);
}
sort(all(v), greater<int>());
ll ans = 0;
for(int i = 0; i < min(n-1, k-1);i++){
ans += v[i];
}
cout << arr[n-1] - arr[0] +1 - ans << endl;
}
int main(){
//freopen("promote.in", "r", stdin);
//freopen("promote.out", "w", stdout);
//init();
speed;
//sieve(ppppp);
//pre_fact(ppppp);
int t;
//cin >> t;
t=1;
while(t--){
run_case();
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message
stove.cpp: In function 'std::vector<int> factors(int)':
stove.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < primes.size() && primes[i]*primes[i] <= n;i++){
| ~~^~~~~~~~~~~~~~~
stove.cpp: In function 'void init()':
stove.cpp:131:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:133:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
18 ms |
2136 KB |
Output is correct |
17 |
Correct |
18 ms |
2124 KB |
Output is correct |
18 |
Correct |
18 ms |
2108 KB |
Output is correct |
19 |
Correct |
17 ms |
2144 KB |
Output is correct |
20 |
Correct |
17 ms |
2156 KB |
Output is correct |
21 |
Correct |
17 ms |
2148 KB |
Output is correct |
22 |
Correct |
20 ms |
2152 KB |
Output is correct |
23 |
Correct |
17 ms |
2172 KB |
Output is correct |
24 |
Correct |
17 ms |
2116 KB |
Output is correct |