Submission #1140774

#TimeUsernameProblemLanguageResultExecution timeMemory
1140774huoiStove (JOI18_stove)C++17
100 / 100
14 ms2376 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define INF 9e18

void solve() {
    int n, k;
    cin >> n >> k;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    n = a.size();

    vector<int> gaps;
    for (int i = 1; i < n; i++) {
        gaps.push_back(a[i] - a[i - 1] - 1);
    }

    sort(gaps.rbegin(), gaps.rend());

    int ans = (a[n - 1] + 1) - a[0];
    for (int i = 0; i < k - 1; i++) {
        ans -= gaps[i];
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...