제출 #1228922

#제출 시각아이디문제언어결과실행 시간메모리
1228922buixuiaStove (JOI18_stove)C++17
100 / 100
17 ms1608 KiB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+1;
int a[N], b[N], gap[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;
    long long k;cin>>n>>k;
    for(int i = 1; i <= n; ++i)cin>>a[i];
    sort(a+1, a+n+1);
    int m = 0;
    b[m++] = a[1];
    for(int i = 2; i <= n; ++i){
        if(a[i] != a[i - 1]){
            b[m++] = a[i];
        }
    }
    if(k >= m){
        cout<<m<<'\n';
        return 0;
    }
    long long res = m;
    for(int i = 1; i <= m - 1; ++i)gap[i] = b[i] -(b[i-1] + 1);
    sort(gap+1, gap + m);
    int need = m - k;
    for(int i = 1; i <= need; ++i)res += gap[i];
    cout<<res<<'\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...