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

int main(){
    int t = 1;
    //cin >> t;
    while(t--){
         int n, k;
         cin >> n >> k;

         int a[n];
         map<int,int>mp;
         for(int i = 0 ; i < n ; i++){
            scanf("%d",a+i);
            mp[a[i]]++;
         }

         if(!mp[0]){
            cout<<0<<endl;
            continue;
         }

         int ind, mx = 0;
         for(int i = 0 ; i <= n + 1 ; i++){
            mx = max(mp[i], mx);
            if(!mp[i]){
                ind = i;
                break;
            }
         }

         long long ans = 0;
         for(int i = 0 ; i < k; i++){
             int sum = 0;
             for(int j = 0 ; j < ind && mp[j]; j++){
                 sum++;
                 mp[j]--;
             }
             ans += sum;
         }

         cout<<ans<<endl;
    }
}
Copy
Bitar The Handy Man Abdullahbitar
GNU G++17
8 ms
1.4 MB
Wrong Answer