#include <bits/stdc++.h>
using namespace std;
// Simple Define
#define ALISA_MIKHAILOVNA_KUJOU \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define ARYA_IS_THE_BEST freopen("input.inp", "r", stdin)
#define SHE_IS_MY_WIFE freopen("output.out", "w", stdout)
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define vi vector<int>
#define vstr vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define pill pair<int, long long>
#define vpill vector<pair<int, long long>>
#define vll vector<long long>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define fd(i, a, b) for (int i = (a); i >= (b); i--)
#define el cout << "\n"
#define elif else if
// bitwise operator
#define MASK(i) (1 << (i))
#define c_bit(i) __builtin_popcountll(i)
#define BIT(x, i) ((x) & MASK(i))
#define SET_ON(x, i) ((x) | MASK(i))
#define SET_OFF(x, i) ((x) & ~MASK(i))
#define getbit(x, i) ((x >> i) & 1)
void solve()
{
int n, k; cin>>n>>k;
vll t(n,0);
for (ll& x: t) cin>>x;
if (k>=n){
cout<<n;
return;
}
ll ans=(t[n-1]+1)-t[0];
vll gaps;
for (int i=0;i<n-1;i++) gaps.push_back(t[i+1]-(t[i]+1));
sort(rall(gaps));
for (int i=0;i<k-1 && i<gaps.size();i++) ans-=gaps[i];
cout<<ans;
}
int main()
{
ALISA_MIKHAILOVNA_KUJOU;
int t = 1;
// cin >> t;
while (t--)
solve();
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... |