Source Code
#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int n, k, arr[N], freq[N];

void solve(){
    cin >> n >> k;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
        ++freq[arr[i]];
    }
    int ans = 0;
    for(int i = 0; i < k; ++i){
        int x = 0;
        for(int j = 0; n >= k - i && freq[j]; ++j){
            ++x;
            --freq[j];
            --n;
        }
        ans += x;
    }
    cout << ans;
}

int main(){
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
Copy
Bitar The Handy Man ItsAboudTime
GNU G++17
17 ms
1.4 MB
Accepted