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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define forall(it, v) for(auto it = v.begin(); it != v.end(); ++it)
#define dforn(i, n) for(int i = ((int) n) - 1; i >= 0; i--)
#define db(v) cerr << #v << " = " << v << endl
#define pb push_back
typedef pair<int, int> pi;
typedef long long ll;
#define ff first
#define ss second
const int MAXN = 256;

int n, A[MAXN], v, mx = -1;
vector<pi> ans, segm;

void solve(bool inv = false){
  int last = 0;
  forr(i, 1, n){
    if(A[i-1] > A[i]){
      segm.pb(make_pair(last, i-1)); // cerrado cerrado
      last = i;
    }
  }
  segm.pb(make_pair(last, n - 1));
  int m = segm.size();
  forn(i, m - 1){
    int v = segm[i].ss;
    forr(w, segm[i+1].ff, segm[i+1].ss + 1){
      if(inv) ans.pb(make_pair(n - 1 - v, n - 1 - w));
      else ans.pb(make_pair(v, w));
    }
  }
}

int main() {  
  scanf("%d", &n);
  forn(i, n){
    scanf("%d", &A[i]);
    if( abs(A[i]) > mx ){
      mx = abs(A[i]);
      v = i;
    }
  }
  forn(i, n){
    if( (ll) A[i] * A[v] < 0 ){
      ans.pb(make_pair(v, i));
    }
  }
  bool rev = false;
  if(A[v] < 0){
    reverse(A, A + n);
    forn(i, n){
      A[i] = -A[i];
    }
    rev = true;
  }
  solve(rev);
  printf("%d\n", (int)ans.size());
  forn(i, ans.size()){
    printf("%d %d\n", ans[i].ff + 1, ans[i].ss + 1);
  }
  return 0;
}