Problem p03495 (C++)

Buggy submission (status: Runtime Error):
// gauravsinghh
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ld long double
#define db double
#define pii pair<int, int>
#define pll pair<long long, long long>
#define sii set<int>
#define sll set<long long>
#define vii vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define mll map<long long, long long>
#define lob lower_bound
#define upb upper_bound
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define pf push_front
#define ins insert
#define mp make_pair
#define w(t)                                                                   \
  cin >> t;                                                                    \
  while (t--)
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define os cout << " "
#define bitcount __builtin_popcount
#define gcd __gcd
#define endl "\n"
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define ford(container, it)                                                    \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define fors(container, it, a, b)                                              \
  for (__typeof(container.begin()) it = a; it != b; it++)
#define MOD 1000000007
#define MAXI (1 << 32)
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define inf 999999999999999999
#define siz 100005
#define SIZ 1000005
#define SIZE 200005
#define fbo find_by_order
#define ook order_of_key
#define sz(s) (int)s.size()
#define mem(n, m) memset(n, m, sizeof(n))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define ffo(i, a, b) for (int i = a; i >= b; i--)
#define MAX 100005
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

void io_set() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return a.ss < b.ss; }

// ll p[20000005]={0};
// vector<ll>ans;
/*void sieve(){
    ll i,j;
    p[0]=p[1]=1;
    for(i=2;i*i<=20000005;i++){
        if(!p[i]){
            for(j=2*i;j<20000005;j+=i){
                p[j]=1;
            }
        }
    }
    fo(i,2,20000005){
        if(!p[i]){
            ans.pb(i);
        }
    }
}*/

signed main() {
  IOS io_set();
  set<ll> s;
  vector<pll> v;
  ll n, k, x;
  cin >> n >> k;
  map<ll, ll> m;
  rep(i, n) {
    cin >> x;
    s.insert(x);
    m[x]++;
  }
  if (k >= sz(s)) {
    cout << 0 << endl;
    return 0;
  } else {
    ll x = sz(s) - k;
    for (auto it : s) {
      v.eb(it, m[it]);
    }
    ll c = 0;
    sort(all(v), comp);
    rep(i, x) { c += v[i].ss; }
    cout << c << endl;
  }
  return 0;
}


Fixed submission:
// gauravsinghh
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ld long double
#define db double
#define pii pair<int, int>
#define pll pair<long long, long long>
#define sii set<int>
#define sll set<long long>
#define vii vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define mll map<long long, long long>
#define lob lower_bound
#define upb upper_bound
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define pf push_front
#define ins insert
#define mp make_pair
#define w(t)                                                                   \
  cin >> t;                                                                    \
  while (t--)
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define os cout << " "
#define bitcount __builtin_popcount
#define gcd __gcd
#define endl "\n"
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define ford(container, it)                                                    \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define fors(container, it, a, b)                                              \
  for (__typeof(container.begin()) it = a; it != b; it++)
#define MOD 1000000007
#define MAXI (1 << 32)
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define inf 999999999999999999
#define siz 100005
#define SIZ 1000005
#define SIZE 200005
#define fbo find_by_order
#define ook order_of_key
#define sz(s) (int)s.size()
#define mem(n, m) memset(n, m, sizeof(n))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define ffo(i, a, b) for (int i = a; i >= b; i--)
#define MAX 100005
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

void io_set() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return a.ss < b.ss; }

// ll p[20000005]={0};
// vector<ll>ans;
/*void sieve(){
    ll i,j;
    p[0]=p[1]=1;
    for(i=2;i*i<=20000005;i++){
        if(!p[i]){
            for(j=2*i;j<20000005;j+=i){
                p[j]=1;
            }
        }
    }
    fo(i,2,20000005){
        if(!p[i]){
            ans.pb(i);
        }
    }
}*/

signed main() {
  IOS
      // io_set();
      set<ll>
          s;
  vector<pll> v;
  ll n, k, x;
  cin >> n >> k;
  map<ll, ll> m;
  rep(i, n) {
    cin >> x;
    s.insert(x);
    m[x]++;
  }
  if (k >= sz(s)) {
    cout << 0 << endl;
    return 0;
  } else {
    ll x = sz(s) - k;
    for (auto it : s) {
      v.eb(it, m[it]);
    }
    ll c = 0;
    sort(all(v), comp);
    rep(i, x) { c += v[i].ss; }
    cout << c << endl;
  }
  return 0;
}