Problem:
Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

10 3
5 1 3 2 4 1 1 2 3 4

Sample Output 3

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Memory Limit Exceeded)

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int, int >
#define F first
#define S second
#define int long long int
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define endl '\n'
using namespace std;
const int N=2e5+100,mod=1e9+7;
deque <int> dp[N][3];
vector <int> g[N];
ll power(ll n,ll k){
    if (k==0) return 1;
    if (k%2==1){
        ll x=power(n,k/2);
        return x*x%mod*n%mod;
    }
    ll x=power(n,k/2);
    return x*x%mod;
}
ll merg(ll v,ll u){
    for (int i=0;i<dp[u][0].size();i++){
        ll z=((dp[v][0][i])*(dp[u][0][i]))%mod;
        z%=mod;
        ll t=(dp[v][1][i]*(dp[u][0][i]))%mod+(dp[u][1][i]*(dp[v][0][i]))%mod;
        t%=mod;
        ll k=(dp[v][2][i]*(dp[u][1][i]+dp[u][0][i]))%mod+(dp[v][1][i]*dp[u][1][i])%mod;
        dp[v][0][i]=z;
        dp[v][1][i]=t;
        dp[v][2][i]=k%mod;
    }
}
ll dfs(ll v,ll p){
    for (auto u : g[v]){
        dfs(u,v);
    }
    for (auto u : g[v]){
        if (dp[v][0].size()<dp[u][0].size()){
            swap(dp[v][0],dp[u][0]);
            swap(dp[v][1],dp[u][1]);
            swap(dp[v][2],dp[u][2]);
        }
        merg(v,u);
    }
    dp[v][0].push_front(power(2,mod-2));
    dp[v][1].push_front(power(2,mod-2));
    dp[v][2].push_front(0);
    for (int i=1;i<dp[v][2].size();i++){
        if (dp[v][2][i]!=0){
            dp[v][0][i]+=dp[v][2][i];
            dp[v][0][i]%=mod;
            dp[v][2][i]=0;
        }
    }
}
int32_t main(){
    sync;
    ll n;
    cin >> n;
    for (int i=1;i<=n;i++){
        ll x;
        cin >> x;
        g[x].pb(i);
    }
    dfs(0,0);
    ll ans=0;
    for (int i=0;i<dp[0][1].size();i++){
        ans+=dp[0][1][i]%mod*power(2,n+1)%mod;
       // cout << i << " " << dp[0][1][i] << " " << dp[0][1][i]*power(2,n+1)%mod << endl;
        ans%=mod;
    }
    cout << ans%mod << endl;
}