Submission #553158

#TimeUsernameProblemLanguageResultExecution timeMemory
553158sandry24Stove (JOI18_stove)C++17
100 / 100
18 ms2072 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

void solve(){
    int n, k;
    cin >> n >> k;
    vi a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];
    ll ans = n;
    vi diff(n-1);
    for(int i = 1; i < n; i++){
        diff[i-1] = a[i] - a[i-1] - 1;
    }
    sort(diff.begin(), diff.end());
    int j = 0;
    while(n != k){
        n--;
        ans += diff[j];
        j++;
    }
    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...