maneedhar
3/15/2020 - 2:03 PM

Selling Products

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

int distids(vector<int>v, int n, int m){
    int count = 0;
    map <int,int>um;
    for (int i=0; i<n; i++){
        um[v[i]]++;
    }
    vector<pair<int, int>>p;
    for (auto i = um.begin(); i != um.end(); i++){
        p.push_back(make_pair(i->second, i->first));
    }
    sort (p.begin(), p.end());
    for (int i=0; i<p.size(); i++){
        if (p[i].first <= m){
            m -= p[i].first;
            count++;
        }
        else{
            return p.size() - count;
        }
    }
    return p.size() - count;
}

int main() {
    int n;
    cin>>n;
    vector<int>v(n);
    for (int i=0; i<n; i++){
        cin>>v[i];
    }
    int m;
    cin>>m;
    cout<<distids(v, n, m);
	return 0;
}