Source Code
#include <iostream>
#include <map>

using namespace std;

map < int, int > p;
int main() {
	int n, m;
	int a[100001] = {};
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		p[a[i]]++;
	}
	int ans = 0;
	int x = 0;
	for (int i = 0; i < m; i++) {
		x = 0;
		for (int j = 0; n >= m - i && p[j]; j++) {
			x++;
			n--;
			p[j]--;
		}
		ans += x;
	}
	cout << ans;
}
Copy
Bitar The Handy Man lafi-Odeh
GNU G++17
30 ms
1.7 MB
Accepted