#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n-1)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=2;
        tmp=-10001010;
        int cnt=1,tt;
        while(1)
        {
            if(tmp!=in[0])   tt=cnt;
            if(mx>n||((int)tmp==(int)in[0]&&(cnt-tt+1)==sc))
                break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=1;
            cnt++;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;    tmp=-10001010;
        // printf("%lld\n",mx);
        while(1)
        {
            if((int)tmp==(int)in[0]) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=1;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n-1)
    {
        mx=n;
        sort(in,in+mx, greater<int>()); printf("%lld",in[0]);
    }
    else
    {
        mx=2;
        tmp=-10001010;
        int cnt=0,tt;
        if(in[0]<in[1]) cnt++;
        for(i=0; i<n-1; i++)
        {
            if(in[i]>in[i+1])
            {
                cnt++;  in[i+1]=in[i];
            }
            else
            {
                if(i>0)
                cnt=1;
            }  // tmp=in[i];
            if(cnt==sc||i==n-2) {printf("%d",in[i+1]);    break;}
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n-1)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=2;
        tmp=-10001010;
        int cnt=1,tt;
        while(1)
        {
            if(tmp!=in[0])   tt=cnt; //printf("%d %d\n",tt,cnt);
            if(mx>n||((int)tmp==(int)in[0]&&(cnt-tt)==sc))
                break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=1;
            cnt++;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;    tmp=-10001010;
        // printf("%lld\n",mx);
        while(1)
        {
            if((int)tmp==(int)in[0]) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=sc;
            if(mx>n) mx=n;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if((int)sc>=(int)n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;    tmp=-1000001010;
        // printf("%lld\n",mx);
        while(1)
        {
            if((int)tmp==(int)in[0]) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=sc;
            if(mx>n) mx=n;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if((int)sc>=(int)n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;
        // printf("%lld\n",mx);
        while(1)
        {
            if(mx>n) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());

            mx+=sc;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if((int)sc>=(int)n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;
        // printf("%lld\n",mx);
        while(1)
        {
            if(mx>n||(int)tmp==(int)in[0]) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());

            mx+=sc;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n)
    {
        mx=n;
        sort(in,in+mx, greater<int>());
    }
    else
    {
        mx=sc+1;    tmp=-10001010;
        // printf("%lld\n",mx);
        while(1)
        {
            if(mx>n||(int)tmp==(int)in[0]) break;
            tmp=in[0];
            sort(in,in+mx, greater<int>());
            mx+=1;
        }
    }
    printf("%lld",in[0]);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,sc,mx;
    scanf("%lld %lld",&n,&sc);
    long long int in[n],tmp;
    for(i=0; i<n; i++)
    {
        scanf("%lld",&in[i]);
    }
    if(sc>=n-1)
    {
        mx=n;
        sort(in,in+mx, greater<int>()); printf("%lld",in[0]);
    }
    else
    {
        mx=2;
        tmp=-10001010;
        int cnt=0,tt;
        if(in[0]<in[1]) cnt++;
        for(i=0; i<n-1; i++)
        {
            if(in[i]>in[i+1])
            {
                cnt++;  in[i+1]=in[i];
            }
            else
            {
                if(i>0)
                cnt=0;
            }//tmp=in[i];
            if(cnt==sc||i==n-2) {printf("%d",in[i+1]);    break;}
        }

    }

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
   long long int n,i,sc,mx;
   scanf("%Ld %Ld",&n,&sc);
   long long int in[n];
    for(i=0;i<n;i++)
   {
    scanf("%Ld",&in[i]);
   }
   if((int)sc>(int)n) mx=n;
    else mx=sc;
  // printf("%Ld\n",mx);
   sort(in,in+mx, greater<int>());
   printf("%Ld",in[0]);
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = v[0] > v[1]? 0 : 1 ;
	int next_player = 1 - cur_win;

	while(count != k && x != n){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1) % n;
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1) % n;
		} else{
			cur_win = next_player;
			count = 1;
			next_player = (next_player + 1) % n;
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = 0;
	int next_player = 1;

	while(count != k && x != n){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1) % n;
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1) % n;
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1) % n;
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = v[0] > v[1]? 0 : 1 ;
	int next_player = 1 - cur_win;

	while(count != k && x != n){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1);
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1);
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1);
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = v[0] > v[1]? 0 : 1 ;
	int next_player = 1 - cur_win;

	while(count != k && x != n){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1) % n;
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1) % n;
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1) % n;
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	if(n == 2){
		cout << max(v[0], v[1]);
		return 0;
	}

	int count = 0;
	int cur_win = 0;
	int next_player = 1;

	while(count != k){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1) % n;
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1) % n;
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1) % n;
		}
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = v[0] > v[1]? 0 : 1 ;
	int next_player = 1 - cur_win;

	if(n == 2){
		cout << max(v[0], v[1]);
		return 0;
	}

	while(count != k && x != n){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1);
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1);
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1);
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main(){

	int n;
	long long k;
	vector<int> v;

	cin >> n >> k;

	for(int i = 0; i< n; i++){
		int x;
		cin >> x;
		v.push_back(x);
	}

	int count = 0, x = 0;
	int cur_win = v[0] > v[1]? 0 : 1 ;
	int next_player = 1 - cur_win;

	while(count != k && x != n -1){
		if(v[cur_win] > v[next_player]){
			count++;
			next_player = (next_player + 1);
		} else if(v[cur_win] == v[next_player]){
			next_player = (next_player + 1);
		} else{
			cur_win = next_player;
			count = 0;
			next_player = (next_player + 1);
		}
		x++;
	}

	cout << v[cur_win] << endl;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=0;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				break;
			}
		}
		if(count==0) cout<<arr[n-1]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n],i,win,temp;
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		sort(arr,arr+n);
		cout <<arr[n-1]<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		bool flag=false;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=0;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				flag = true;
				break;
			}
		}
		if(flag==false){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n],i,temp;
	for(i=0 ; i<n ; i++){
		cin>>arr[i];
	}
	if(k>=n-1){
		temp=arr[0];
		for(i=1; i<n ; i++){
			if(temp<arr[i]) temp=arr[i];
		}
		cout<<temp<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		bool flag=false;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=1;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				flag = true;
				break;
			}
		}
		if(flag==false){
			long long big=0;
			for(i=0 ; i<n ; i++){
				if(arr[i]>big) big=arr[i];
			}
			cout<<big<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		bool flag=false;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=0;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				flag = true;
				break;
			}
		}
		if(k>=n-1){
			long long big=0;
			for(i=0 ; i<n ; i++){
				if(arr[i]>big) big=arr[i];
			}
			cout<<big<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n]={0},i,temp;
	for(i=0 ; i<n ; i++){
		cin>>arr[i];
	}
	if(k>=n-1){
		sort(arr,arr+n);
		cout <<arr[n-1]<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=0;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				break;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n]={},i,temp;
	for(i=0 ; i<n ; i++){
		cin>>arr[i];
	}
	if(k>=n-1){
		temp=0;
		for(i=0; i<n ; i++){
			if(temp<arr[i]) temp=arr[i];
		}
		cout<<temp<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n]={0},i,win,temp;
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		sort(arr,arr+n);
		cout <<arr[n-1]<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long n,k;
	cin >> n>>k;
	long long i,arr[n];
	for(i=0 ; i<n ; i++) cin>>arr[i];
	if(k>=n-1){
		long long big=0;
		for(i=0 ; i<n ; i++){
			if(arr[i]>big) big=arr[i];
		}
		cout<<big<<endl;
	}
	else{
		int count=0;
		bool flag=false;
		long long big=arr[0];
		for(i=1 ; i<n ; i++){
			if(arr[i]>=big){
				big=arr[i];
				count=0;
			}
			else{
				count++;
			}
			if(count==k){
				cout<<big<<endl;
				flag = true;
				break;
			}
		}
		if(flag==false){
			long long big=0;
			for(i=0 ; i<n ; i++){
				if(arr[i]>big) big=arr[i];
			}
			cout<<big<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long n,k;
	cin >>n>>k;
	int arr[n]={0},i,temp;
	for(i=0 ; i<n ; i++){
		cin>>arr[i];
	}
	if(k>=n-1){
		temp=0;
		for(i=0; i<n ; i++){
			if(temp<arr[i]) temp=arr[i];
		}
		cout<<temp<<endl;
	}
	else{
		temp=arr[0];
		int count=0,flag=1;
		for(i=1 ; i<n ; i++){
			if(temp<arr[i]){
				temp=arr[i];
				count=0;
			}
			else count++;
			if(count==k){
				cout<<temp<<endl;
				flag=0;
				break;
			}
		}
		if(flag){
			sort(arr,arr+n);
			cout<<arr[n-1]<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, k;
    cin >> n >> k;
    vector<int> v1;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        v1.push_back(x);
    }
    int x = v1[0];
    int m = x;
    int d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }

    for(int i=1; i<n; i++)
    {
        if(d==k)
        {
            cout<<x<<endl;
            return 0;
        }
        int p=max(x,v1[i]);
        x=p;
        if((x-m)==0)
        {
           d++;
           m=p;
        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  long long int   n, k;
    cin >> n >> k;
    vector<ll> v1;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        v1.push_back(x);
    }
    ll x = v1[0];
    ll m = x;
    ll d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }
    if(k>=n)
    {
        sort(v1.begin(),v1.end());
        cout<<v1[n-1]<<endl;
        return 0;
    }

    for(ll i=1; i<n; i++)
    {
        if(d==k)
        {
            cout<<x<<endl;
            return 0;
        }
        ll p=max(x,v1[i]);
        x=p;
        if((x-m)==0)
        {
           d++;
           m=p;
        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  long long int   n, k;
    cin >> n >> k;
    vector<ll> v1;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        v1.push_back(x);
    }
    ll x = v1[0];
    ll m = x;
    ll d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }
    int test=1;

    for(ll i=1; i<n; i++)
    {
        if(d==k)
        {
            test=0;
            cout<<x<<endl;
            return 0;
        }
        ll p=max(x,v1[i]);
        x=p;
        if((x-m)==0)
        {
           d++;
           m=p;
        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
        }
    }
    sort(v1.begin(),v1.end());
    if(test)
    {
        cout<<v1[n-1];
        return 0;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  long long int   n, k;
    cin >> n >> k;
    vector<ll> v1;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        v1.push_back(x);
    }
    ll x = v1[0];
    ll m = x;
    ll d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }
    if(k>=n)
    {
        for(long long int i=1; i<n; i++)
        {
            if(d==n-1)
            {
                cout<<x<<endl;
                return 0;
            }
           ll p=max(x,v1[i]);
            x=p;
            if((x-m)==0)
            {
                d++;
                m=p;
            } else if (x-m!=0 && i==1)
            {
                d++;
                m=p;
            } else
            {
                d=0;
            }
        }
    }

    for(ll i=1; i<n; i++)
    {
        if(d==k)
        {
            cout<<x<<endl;
            return 0;
        }
        ll p=max(x,v1[i]);
        x=p;
        if((x-m)==0)
        {
           d++;
           m=p;
        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, k;
    cin >> n >> k;
    vector<int> v1;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        v1.push_back(x);
    }
    int x = v1[0];
    int m = x;
    int d = 0;
    if(n==2)
    {
        cout<<max(v1[0],v1[1])<<endl;
        return 0;
    }

    for(int i=1; i<n; i++)
    {
        if(d==k)
        {
            cout<<x<<endl;
            return 0;
        }
        int p=max(x,v1[i]);
        x=p;
        if((x-m)==0)
        {
           d++;
           m=p;
        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  long long int   n, k;
    cin >> n >> k;
    vector<ll> v1;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        v1.push_back(x);
    }
    ll x = v1[0];
    ll m = x;
    ll d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }
    int test=1;

    for(ll i=1; i<n; i++)
    {
        if(d==k)
        {
            test=0;

            cout<<x<<endl;
            return 0;
        }
        ll p=max(x,v1[i]);

        x=p;
        if((x-m)==0)
        {
           d++;

        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=1;
            m=p;
        }

    }
    sort(v1.begin(),v1.end());
    if(test)
    {
        cout<<v1[n-1];

        return 0;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  long long int   n, k;
    cin >> n >> k;
    vector<ll> v1;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        v1.push_back(x);
    }
    ll x = v1[0];
    ll m = x;
    ll d = 0;
    if(n==2)
    {
        if(v1[0]>v1[1])
        {
            cout<<v1[0]<<endl;
            return 0;
        }
        cout<<v1[1];
        return 0;
    }
    int test=1;

    for(ll i=1; i<n; i++)
    {
        if(d==k)
        {
            test=0;

            cout<<x<<endl;
            return 0;
        }
        ll p=max(x,v1[i]);
        ////cout<<p<<endl;
        x=p;
        if((x-m)==0)
        {
           d++;

        } else if (x-m!=0 && i==1)
        {
            d++;
            m=p;
        } else
        {
            d=0;
            m=p;
        }

    }
    sort(v1.begin(),v1.end());
    if(test)
    {
        cout<<v1[n-1];

        return 0;
    }
}


#include <iostream>
#include <limits>
using namespace std;

/*
  n people

*/

int main() {
  // freopen_s("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  long long int row;
  cin >> n >> row;
  int strongest;
  cin >> strongest;
  long long int in_row = 1;
  for (int i = 0; i < n - 1; ++i) {
    int p;
    cin >> p;
    if (strongest > p) {
      ++in_row;
    } else {
      in_row = 1;
      strongest = p;
    }
    if (in_row == row) {
      cout << strongest << endl;
      return 0;
    }
  }
  cout << strongest << endl;
  return 0;
}


#include <iostream>
#include <limits>
using namespace std;

/*
  n people

*/

int main() {
  // freopen_s("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  long long int row;
  cin >> n >> row;
  int strongest;
  cin >> strongest;
  long long int in_row = 0;
  for (int i = 0; i < n - 1; ++i) {
    int p;
    cin >> p;
    if (strongest > p) {
      ++in_row;
    } else {
      in_row = 1;
      strongest = p;
    }
    if (in_row == row) {
      cout << strongest << endl;
      return 0;
    }
  }
  cout << strongest << endl;
  return 0;
}


#include <iostream>
#include <limits>
using namespace std;

/*
  n people

*/

int main(){
  int n;
  long long int row;
  cin>>n>>row;
  int strongest;
  cin>>strongest;
  long long int in_row = 0;
  for(int i = 0; i < n - 1 ; ++i){
    int p;
    cin>>p;
    if(strongest > p){
      ++in_row;
    }else{
      in_row == 0;
      strongest = p;
    }
    if(in_row == row){
      cout<<strongest<<endl;
      return 0;
    }
  }
  cout<<strongest<<endl;
  return 0;
}


#include <iostream>
#include <limits>
using namespace std;

/*
  n people

*/

int main(){
  int n;
  long long int row;
  cin>>n>>row;
  int strongest;
  cin>>strongest;
  long long int in_row = 0;
  for(int i = 0; i < n - 1 ; ++i){
    int p;
    cin>>p;
    if(strongest > p){
      ++in_row;
    }else{
      in_row == 1;
      strongest = p;
    }
    if(in_row == row){
      cout<<strongest<<endl;
      return 0;
    }
  }
  cout<<strongest<<endl;
  return 0;
}


#include <iostream>
#include <limits>
using namespace std;

/*
  n people

*/

int main(){
  int n;
  long long int row;
  cin>>n>>row;
  int strongest = numeric_limits<int>::min();
  for(int i = 0; i < n ; ++i){
    int p;
    cin>>p;
    strongest = max(p,strongest);
  }
  cout<<strongest<<endl;
  return 0;
}


/*
**   Contest : Codeforces Round #443 (Div. 2)
**   Problem : B. Table Tennis
**
**    Author : Dhiraj Govindvira
**      Date : 29-Apr-2020 ~ Wednesday
**      Time : 02:09:24 PM
*/

#if __has_include("D:/dhiraj/Programming/debug.h")
    #include "D:/dhiraj/Programming/debug.h"
#else
    #include <bits/stdc++.h>
    using namespace std;
    #define d(...) 11
#endif

// Loops --------------------------------------------------------------------

#define loop(i,s,e)         for(ll i=(s); i<(e); i++)
#define rloop(i,s,e)        for(ll i=(s); i>=(e); i--)
#define iter(it,var)        for(auto it=var.begin(); it!=var.end(); it++)
#define riter(it,var)       for(auto it=var.rbegin(); it!=var.rend(); it++)

// Extra --------------------------------------------------------------------

#define ull                 unsigned long long int
#define ll                  long long int
#define ld                  long double
#define endl                '\n'
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()

// Template -----------------------------------------------------------------

template <typename T>
inline bool inrange(T a, T l, T r) {
    if(l>r) swap(l,r);
    return (a>=l and a<=r);
}
template <typename T, typename U>
inline bool exist(T val, U &any) {
    return (any.find(val) != any.end());
}
// multiple input
template <typename T>
inline void inp(T &any) {
    cin >> any;
}
template <typename T, typename... U>
inline void inp(T &a, U &...b) {
    cin >> a; inp(b...);
}
// scan vector
template <typename T>
inline istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x : a) in >> x;
    return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}

// Solve --------------------------------------------------------------------

void solve(ll &T)
{
    ll n, k;
    std::cin>>n>>k;

    vector<ll> a(n);
    std::cin>>a;

    deque<ll> dq;
    for(ll x : a) {
        dq.push_back(x);
    }

    ll ans = -1;
    map<ll,ll> fre;
    ll cnt = n;
    while(cnt--)
    {
        d(dq);
        ll x = dq.front(); dq.pop_front();
        ll y = dq.front(); dq.pop_front();

        dq.push_front(max(x,y));
        dq.push_back(min(x,y));

        fre[max(x,y)]++;
        if(fre[max(x,y)]>=k)
        {
            ans = max(x,y);
            std::cout<<ans<<endl;
            return;
        }
    }

    std::cout<<*max_element(all(a))<<endl;
}

// Main ---------------------------------------------------------------------

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt","r",stdin);
        freopen("D:/dhiraj/Programming/o1.txt","w",stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 1;
    loop(i,1,t+1)
    {
        cerr<<"Case #"<<i<<"\n";
        solve(i);
    }
    return 0;
}


/*
**   Contest : Codeforces Round #443 (Div. 2)
**   Problem : B. Table Tennis
**
**    Author : Dhiraj Govindvira
**      Date : 29-Apr-2020 ~ Wednesday
**      Time : 02:09:24 PM
*/

#if __has_include("D:/dhiraj/Programming/debug.h")
    #include "D:/dhiraj/Programming/debug.h"
#else
    #include <bits/stdc++.h>
    using namespace std;
    #define d(...) 11
#endif

// Loops --------------------------------------------------------------------

#define loop(i,s,e)         for(ll i=(s); i<(e); i++)
#define rloop(i,s,e)        for(ll i=(s); i>=(e); i--)
#define iter(it,var)        for(auto it=var.begin(); it!=var.end(); it++)
#define riter(it,var)       for(auto it=var.rbegin(); it!=var.rend(); it++)

// Extra --------------------------------------------------------------------

#define ull                 unsigned long long int
#define ll                  long long int
#define ld                  long double
#define endl                '\n'
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()

// Template -----------------------------------------------------------------

template <typename T>
inline bool inrange(T a, T l, T r) {
    if(l>r) swap(l,r);
    return (a>=l and a<=r);
}
template <typename T, typename U>
inline bool exist(T val, U &any) {
    return (any.find(val) != any.end());
}
// multiple input
template <typename T>
inline void inp(T &any) {
    cin >> any;
}
template <typename T, typename... U>
inline void inp(T &a, U &...b) {
    cin >> a; inp(b...);
}
// scan vector
template <typename T>
inline istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x : a) in >> x;
    return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}

// Solve --------------------------------------------------------------------

void solve(ll &T)
{
    ll n, k;
    std::cin>>n>>k;

    vector<ll> a(n);
    std::cin>>a;

    deque<ll> dq;
    for(ll x : a) {
        dq.push_back(x);
    }

    ll ans = -1;
    map<ll,ll> fre;
    ll cnt = 2*n;
    while(cnt--)
    {
        d(dq);
        ll x = dq.front(); dq.pop_front();
        ll y = dq.front(); dq.pop_front();

        dq.push_front(max(x,y));
        dq.push_back(min(x,y));

        fre[max(x,y)]++;
        if(fre[max(x,y)]>=k)
        {
            ans = max(x,y);
            std::cout<<ans<<endl;
            return;
        }
    }

    std::cout<<*max_element(all(a))<<endl;
}

// Main ---------------------------------------------------------------------

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt","r",stdin);
        freopen("D:/dhiraj/Programming/o1.txt","w",stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 1;
    loop(i,1,t+1)
    {
        cerr<<"Case #"<<i<<"\n";
        solve(i);
    }
    return 0;
}


/*
**   Contest : Codeforces Round #443 (Div. 2)
**   Problem : B. Table Tennis
**
**    Author : Dhiraj Govindvira
**      Date : 29-Apr-2020 ~ Wednesday
**      Time : 02:09:24 PM
*/

#if __has_include("D:/dhiraj/Programming/debug.h")
    #include "D:/dhiraj/Programming/debug.h"
#else
    #include <bits/stdc++.h>
    using namespace std;
    #define d(...) 11
#endif

// Loops --------------------------------------------------------------------

#define loop(i,s,e)         for(ll i=(s); i<(e); i++)
#define rloop(i,s,e)        for(ll i=(s); i>=(e); i--)
#define iter(it,var)        for(auto it=var.begin(); it!=var.end(); it++)
#define riter(it,var)       for(auto it=var.rbegin(); it!=var.rend(); it++)

// Extra --------------------------------------------------------------------

#define ull                 unsigned long long int
#define ll                  long long int
#define ld                  long double
#define endl                '\n'
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()

// Template -----------------------------------------------------------------

template <typename T>
inline bool inrange(T a, T l, T r) {
    if(l>r) swap(l,r);
    return (a>=l and a<=r);
}
template <typename T, typename U>
inline bool exist(T val, U &any) {
    return (any.find(val) != any.end());
}
// multiple input
template <typename T>
inline void inp(T &any) {
    cin >> any;
}
template <typename T, typename... U>
inline void inp(T &a, U &...b) {
    cin >> a; inp(b...);
}
// scan vector
template <typename T>
inline istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x : a) in >> x;
    return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}

// Solve --------------------------------------------------------------------

void solve(ll &T)
{
    ll n, k;
    std::cin>>n>>k;

    vector<ll> a(n);
    std::cin>>a;

    deque<ll> dq;
    for(ll x : a) {
        dq.push_back(x);
    }

    ll ans = -1;
    map<ll,ll> fre;
    while(1)
    {
        ll x = dq.front(); dq.pop_front();
        ll y = dq.front(); dq.pop_front();

        dq.push_front(max(x,y));
        dq.push_back(min(x,y));

        fre[max(x,y)]++;
        if(fre[max(x,y)]>=k)
        {
            ans = max(x,y);
            break;
        }
    }
    std::cout<<ans<<endl;
}

// Main ---------------------------------------------------------------------

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt","r",stdin);
        freopen("D:/dhiraj/Programming/o1.txt","w",stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 1;
    loop(i,1,t+1)
    {
        cerr<<"Case #"<<i<<"\n";
        solve(i);
    }
    return 0;
}


/*
**   Contest : Codeforces Round #443 (Div. 2)
**   Problem : B. Table Tennis
**
**    Author : Dhiraj Govindvira
**      Date : 29-Apr-2020 ~ Wednesday
**      Time : 02:09:24 PM
*/

#if __has_include("D:/dhiraj/Programming/debug.h")
    #include "D:/dhiraj/Programming/debug.h"
#else
    #include <bits/stdc++.h>
    using namespace std;
    #define d(...) 11
#endif

// Loops --------------------------------------------------------------------

#define loop(i,s,e)         for(ll i=(s); i<(e); i++)
#define rloop(i,s,e)        for(ll i=(s); i>=(e); i--)
#define iter(it,var)        for(auto it=var.begin(); it!=var.end(); it++)
#define riter(it,var)       for(auto it=var.rbegin(); it!=var.rend(); it++)

// Extra --------------------------------------------------------------------

#define ull                 unsigned long long int
#define ll                  long long int
#define ld                  long double
#define endl                '\n'
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()

// Template -----------------------------------------------------------------

template <typename T>
inline bool inrange(T a, T l, T r) {
    if(l>r) swap(l,r);
    return (a>=l and a<=r);
}
template <typename T, typename U>
inline bool exist(T val, U &any) {
    return (any.find(val) != any.end());
}
// multiple input
template <typename T>
inline void inp(T &any) {
    cin >> any;
}
template <typename T, typename... U>
inline void inp(T &a, U &...b) {
    cin >> a; inp(b...);
}
// scan vector
template <typename T>
inline istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x : a) in >> x;
    return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}

// Solve --------------------------------------------------------------------

void solve(ll &T)
{
    ll n, k;
    std::cin>>n>>k;

    vector<ll> a(n);
    std::cin>>a;

    deque<ll> dq;
    for(ll x : a) {
        dq.push_back(x);
    }

    ll ans = -1;
    map<ll,ll> fre;
    ll cnt = n;
    while(cnt--)
    {
        d(dq);
        ll x = dq.front(); dq.pop_front();
        ll y = dq.front(); dq.pop_front();

        dq.push_front(max(x,y));
        dq.push_back(min(x,y));

        fre[max(x,y)]++;
        if(fre[max(x,y)]>=k)
        {
            ans = max(x,y);
            std::cout<<ans<<endl;
            return;
        }
    }

    std::cout<<*max_element(all(a))<<endl;
}

// Main ---------------------------------------------------------------------

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt","r",stdin);
        freopen("D:/dhiraj/Programming/o1.txt","w",stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 4;
    loop(i,1,t+1)
    {
        cerr<<"Case #"<<i<<"\n";
        solve(i);
    }
    return 0;
}


/*
**   Contest : Codeforces Round #443 (Div. 2)
**   Problem : B. Table Tennis
**
**    Author : Dhiraj Govindvira
**      Date : 29-Apr-2020 ~ Wednesday
**      Time : 02:09:24 PM
*/

#if __has_include("D:/dhiraj/Programming/debug.h")
    #include "D:/dhiraj/Programming/debug.h"
#else
    #include <bits/stdc++.h>
    using namespace std;
    #define d(...) 11
#endif

// Loops --------------------------------------------------------------------

#define loop(i,s,e)         for(ll i=(s); i<(e); i++)
#define rloop(i,s,e)        for(ll i=(s); i>=(e); i--)
#define iter(it,var)        for(auto it=var.begin(); it!=var.end(); it++)
#define riter(it,var)       for(auto it=var.rbegin(); it!=var.rend(); it++)

// Extra --------------------------------------------------------------------

#define ull                 unsigned long long int
#define ll                  long long int
#define ld                  long double
#define endl                '\n'
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()

// Template -----------------------------------------------------------------

template <typename T>
inline bool inrange(T a, T l, T r) {
    if(l>r) swap(l,r);
    return (a>=l and a<=r);
}
template <typename T, typename U>
inline bool exist(T val, U &any) {
    return (any.find(val) != any.end());
}
// multiple input
template <typename T>
inline void inp(T &any) {
    cin >> any;
}
template <typename T, typename... U>
inline void inp(T &a, U &...b) {
    cin >> a; inp(b...);
}
// scan vector
template <typename T>
inline istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x : a) in >> x;
    return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T,U> &a) {
    in >> a.first >> a.second;
    return in;
}

// Solve --------------------------------------------------------------------

void solve(ll &T)
{
    ll n, k;
    std::cin>>n>>k;

    vector<ll> a(n);
    std::cin>>a;

    deque<ll> dq;
    for(ll x : a) {
        dq.push_back(x);
    }

    ll ans = -1;
    map<ll,ll> fre;
    ll cnt = 2*n;
    while(cnt--)
    {
        d(dq);
        ll x = dq.front(); dq.pop_front();
        ll y = dq.front(); dq.pop_front();

        dq.push_front(max(x,y));
        dq.push_back(min(x,y));

        fre[max(x,y)]++;
        if(fre[max(x,y)]>=k)
        {
            ans = max(x,y);
            std::cout<<ans<<endl;
            return;
        }
    }

    ll val = -1;
    for(auto x : fre) {
        if(x.second > ans)
        {
            ans = max(ans, x.second);
            val = x.first;
        }
    }
    std::cout<<val<<endl;
}

// Main ---------------------------------------------------------------------

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt","r",stdin);
        freopen("D:/dhiraj/Programming/o1.txt","w",stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 1;
    loop(i,1,t+1)
    {
        cerr<<"Case #"<<i<<"\n";
        solve(i);
    }
    return 0;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009];
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=1;
    while ((i<=k)&&(maxr<maxn)) {
        if (a[i+1]>maxr) {
        maxr=max(maxr,a[i+1]);
        i=1;
        }
        else {
        i++;
        }
    }
    cout<<maxr;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009],turn;
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=1;
    turn=0;
    while ((turn<k)&&(maxr<maxn)) {
        if (a[i+1]>maxr) {
        maxr=max(maxr,a[i+1]);
        turn=1;
        i++;
        }
        else {
        i++;
        }
    }
    cout<<maxr;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009];
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=1;
    while ((i<=k)&&(maxr<maxn)) {
        maxr=max(maxr,a[i+1]);
        i++;
    }
    cout<<maxr;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009];
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=0;
    while ((i<=k)&&(maxr<maxn)) {
        if (a[i+1]>maxr) {
        maxr=max(maxr,a[i+1]);
        i=1;
        }
        else {
        i++;
        }
    }
    cout<<maxr;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009],turn;
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=1;
    turn=0;
    while ((turn<k)&&(maxr<maxn)) {
        if (a[i+1]>maxr) {
        maxr=max(maxr,a[i+1]);
        turn=1;
        i++;
        }
        else {
        i++;
        turn++;
        }
    }
    cout<<maxr;
}


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

int main(){
    long long n,k,i,maxr=0,maxn=0,a[100009],turn;
    cin>>n>>k;
    for (i=1;i<=n;i++) {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    maxr=a[1];
    i=1;
    turn=0;
    while ((turn<=k)&&(maxr<maxn)) {
        if (a[i+1]>maxr) {
        maxr=max(maxr,a[i+1]);
        turn=1;
        i++;
        }
        else {
        i++;
        }
    }
    cout<<maxr;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
     if(k>=n-1)
    {
    	cout<<*(max_element(b,b+n));
    	return 0;
    }
    ll win=qu.front();
    qu.pop();
    for(int i=0;i<n;i++)
    {
    	qu.push(min(win,qu.front()));
        win=max(win,qu.front());
        freq[max(win,qu.front())-1]++;
        if(freq[win-1]>=k)
        {
        	cout<<win;
        	break;
        }
        qu.pop();
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
    ll win=qu.front();
    qu.pop();
    for(int i=0;i<n;i++)
    {
    	qu.push(min(win,qu.front()));
        win=max(win,qu.front());
        freq[max(win,qu.front())-1]++;
        freq[min(win,qu.front())-1]++;
        if(freq[win-1]>=k)
        {
        	cout<<win;
        	break;
        }
        qu.pop();
    }
    if(k>n)
    {
    	cout<<*(max_element(b,b+n));
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
	ll n,k,count=0;
	cin>>n>>k;
	ll a[n];
    for(ll i=0;i<n;i++)
    {
    	cin>>a[i];
    }
    ll max=a[0];
    if(k>=n)
    {
       cout<<*(max_element(a,a+n));
    }
    else
    {
    	for(int i=1;i<2*n;i++)
    	{
           if(max>a[i])
           {
           	 count++;
           }
           else
           {
           	 max=a[i];
           	 count=0;
           }
           if(count==k)
           {
           	 break;
           }
    	}
    }
    cout<<max;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
    ll win=qu.front();
    qu.pop();
    for(int i=0;i<n;i++)
    {
    	qu.push(min(win,qu.front()));
        win=max(win,qu.front());
        freq[max(win,qu.front())-1]++;
        if(freq[win-1]>=k)
        {
        	cout<<win;
        	break;
        }
        qu.pop();
    }
    if(k>=n-1)
    {
    	cout<<*(max_element(b,b+n));
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
	ll n,k,count=0;
	cin>>n>>k;
	vector<ll> a(n);
	ll b[n];
    for(ll i=0;i<n;i++)
    {
    	cin>>a[i];
    	b[i]=a[i];
    }
    ll max=a[0];
    if(k>=n)
    {
       cout<<*(max_element(b,b+n));
    }
    else
    {
       for(ll i=0;i<n;i++)
       {
       	for(ll j=0;j<n;j++)
       	{
       		if(a[i]>a[(j+i)%n])
       		{
       			count++;
       		}
       	}
       	if(count>=k)
       	{
       		cout<<a[i];
       		break;
       	}
       }
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
    for(ll i=0;i<n;i++)
    {
    	cin>>a[i];
    }
    if(k>=n)
    {
       cout<<*(max_element(a,a+n));
    }
    else
    {
    	cout<<*(max_element(a,a+k));
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
	ll n,k,count=0;
	cin>>n>>k;
	vector<ll> a(n);
	ll b[n];
    for(ll i=0;i<n;i++)
    {
    	cin>>a[i];
    	b[i]=a[i];
    }
    ll max=a[0];
    if(k>=n)
    {
       cout<<*(max_element(b,b+n));
    }
    else
    {
       for(ll i=0;i<n;i++)
       {
       	count=0;
       	for(ll j=1;j<n;j++)
       	{
       		if(a[i]>a[(j+i)%n])
       		{
       			count++;
       		}
       		else
       		{
       			break;
       		}
       	}
       	if(count>=k)
       	{
       		cout<<a[i];
       		break;
       	}
       }
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
    ll win=qu.front();
    for(int i=0;i<n;i++)
    {
    	qu.pop();
        win=max(win,qu.front());
        freq[win-1]++;
        if(freq[win-1]>=k)
        {
        	cout<<win;
        	break;
        }
    }
    if(k>n)
    {
    	cout<<*(max_element(b,b+n));
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
    ll win=qu.front();
    qu.pop();
    for(int i=0;i<n;i++)
    {
    	qu.push(min(win,qu.front()));
        win=max(win,qu.front());
        freq[max(win,qu.front())-1]++;
        if(freq[win-1]>=k)
        {
        	cout<<win;
        	break;
        }
        qu.pop();
    }
    if(k>n)
    {
    	cout<<*(max_element(b,b+n));
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
	ll n,k,count=0;
	cin>>n>>k;
	ll a[n];
    for(ll i=0;i<n;i++)
    {
    	cin>>a[i];
    }
    ll max=a[0];
    if(k>=n)
    {
       cout<<*(max_element(a,a+n));
    }
    else
    {
    	for(int i=1;i<2*n;i++)
    	{
           if(max>a[i])
           {
           	 count++;
           }
           else
           {
           	 max=a[i];
           	 count=0;
           }
           if(count==k)
           {
           	 break;
           }
    	}
    	cout<<max;
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1e6+1;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll b[n],freq[n]={0};
    queue<ll> qu;
    for(int i=0;i<n;i++)
    {
    	cin>>b[i];
    	qu.push(b[i]);
    }
     if(k>=n-1)
    {
    	cout<<*(max_element(b,b+n));
    	return 0;
    }
    ll plr2;
    ll win=qu.front();
    while(true)
    {
    	qu.pop();
    	plr2=qu.front();
    	qu.push(min(win,plr2));
    	win=max(win,plr2);
    	freq[win-1]++;
    	if(freq[win-1]==k)
    	{
    		cout<<win;
    		break;
    	}
    }
}


#include<bits/stdc++.h>
#define r_0 return 0
#define pb push_back
#define ll long long

using namespace std;
const ll N = 1e6;
ll  cnt,mn,mx;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
      cout.tie(0);

           //freopen( "input.txt", "r", stdin );
            //freopen( "output.txt", "w", stdout );

    ll n, m,x,z = 0;
    cin >> n  >> m;
    vector<ll>v;
   for( int i = 0; i < n; i ++ )
      cin >> x, v.pb( x );
    for( int i = 0;  i < v.size();i ++ )
    {
        for( int j = i + 1; j < v.size(); j ++ )
        {
            if( v[i] > v[j] )
                cnt ++;
                if( cnt == m )
                    return cout << v[i], 0;
        }
    }
    if( m >= n or n - 1 == m )
        cout << *max_element( v.begin(), v.end() );
    else{
          cout << cnt;
    }
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int n; cin>>n;
    int arr[n];
    map<int,int> freq;
    for(int i=0;i<n;i++){ cin>>arr[i]; freq[arr[i]]++; }
    int answer = 0, standard_value = 0;

    map<int,int>::iterator itr = freq.begin();
    standard_value = itr->second;
    ++itr;
    for(;itr!=freq.end();itr++){
        answer += min(standard_value, itr->second);
    }
    cout<<answer;
    return 0;
}


//INSPIRED

#include<bits/stdc++.h>

using namespace std;

int main(){

    int n; cin>>n;
    int arr[n];
    map<int,int> freq;
    for(int i=0;i<n;i++){ cin>>arr[i]; freq[arr[i]]++; }
    int answer = 0;

    map<int,int>::iterator itr = freq.begin();
    for(;itr!=freq.end();itr++){
        answer = max(answer,itr->second);
    }
    answer = n- answer;
    cout<<answer;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int n; cin>>n;
    int arr[n];
    map<int,int> freq;
    for(int i=0;i<n;i++){ cin>>arr[i]; freq[arr[i]]++; }
    int answer = 0, residue = 0;

    map<int,int>::iterator itr = freq.begin();
    // for(;itr!=freq.end();itr++){
    //     answer = max(answer,itr->second);
    // }
    // answer = n- answer;
    // cout<<answer;
    residue = itr->second;
    ++itr;
    for(;itr!=freq.end();itr++){
        int value = itr->second;
        if(value >= residue){
            answer += residue;
            residue = value;
        }
        else{
            answer += value;
        }
    }
    cout<<answer;
    return 0;
}


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

#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define pii pair<int,int>
#define fr first
#define sc second
#define rep(i,a,b) for(int i=a;i<b;i++)
// #define endl '\n'

const int MOD = 998244353;
const int N = 2e6 + 7;
ll a[N];
void solve(){
	int n; cin >> n;
	rep(i, 0, n) cin >> a[i];
	sort(a, a + n);
	int fir = a[0];
	int cnt = 0;
	rep(i, 0 , n){
		if(a[i] == fir){
			cnt++;
		}else break;
	}
	cout<<n-cnt<<endl;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


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

#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define pii pair<int,int>
#define fr first
#define sc second
#define rep(i,a,b) for(int i=a;i<b;i++)
// #define endl '\n'

const int MOD = 998244353;
const int N = 2e6 + 7;
ll a[N];
void solve(){
	int n; cin >> n;
	int oc = 0;
	map<int, int> a;
	rep(i, 0, n){
		int m; cin >> m; a[m]++;
		oc = max(oc, a[m]);
	}
	cout<<n-oc<<endl;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


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

#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define pii pair<int,int>
#define fr first
#define sc second
#define rep(i,a,b) for(int i=a;i<b;i++)
// #define endl '\n'

const int MOD = 998244353;
const int N = 2e6 + 7;
ll a[N];
void solve(){
	int n; cin >> n;
	rep(i, 0, n) cin >> a[i];
	sort(a, a + n);
	int fir = a[0];
	int cnt = 0, oc = 0;
	rep(i, 0 , n){
		if(a[i] == fir){
			cnt++;
		}else{
			oc = max(cnt, oc);
			cnt = 0;
		}
	}
	oc = max(oc, cnt);
	cout<<n-oc<<endl;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll a,c,i;
    cin>>a;
    ll b[a];
    for(i=0;i<a;i++)
    {
        cin>>b[i];
    }
    sort(b,b+a);
    for(i=0;i<a;i++)
    {
        if(b[i+1]>b[i])
        {
            c=i+1;
            break;
        }
    }
    cout<<a-c;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll a,c=0,i,s=0;
    cin>>a;
    ll b[a];
    for(i=0;i<a;i++)
    {
        cin>>b[i];
    }
    sort(b,b+a);
    for(i=0;i<a;i++)
    {
        if(b[i]>b[i-1])
        {
            c=i;
        }
        if(c>0)
        {
            s++;
            c=c-s;

        }

    }
    cout<<s;
}


#define ll long long int
using namespace std;
int main()
{
    ll a,c=0,i,s=0;
    cin>>a;
    ll b[a];
    for(i=0;i<a;i++)
    {
        cin>>b[i];
    }
    sort(b,b+a);
    for(i=0;i<a;i++)
    {
        if(b[i]>b[i-1])
        {
            c=i;
        }
        if(c>0)
        {
            s++;
            c=c-s;

        }

    }
    cout<<s;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll a,j=0,i,s=0;
    cin>>a;
    ll b[a];
    for(i=0;i<a;i++)
    {
        cin>>b[i];
    }
    sort(b,b+a);
    for(i=0;i<a;i++)
    {
        if(b[i]>b[j])
        {
            j++;
        }

    }
    cout<<j;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef map<ll,ll> mll;

typedef vector < pii > vpii;
typedef vector < pll > vpll;

typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;
typedef vector< vpii > vvpii;
typedef vector < vpll > vvpll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ms;

#define mem(a, b) memset(a, (b), sizeof(a))
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define po pop_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}}
#define endl "\n"
#define SZ(x) ((int)x.size())

//debug template
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second);
cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""),
                          __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

template <typename T>
void ckmin(T& a, const T b) {a = min(a, b);}

template <typename T>
void ckmax(T& a, const T b) {a = max(a, b);}

template <typename T>
void print(vector <T> v) {
    for (T i : v) cout << i << " ";
    cout << '\n';
}

template <typename T>
void print(vector <vector <T>>& v) {
    for (vector <T>& vv : v) {
        for (T& i : vv)
            cout << i << " ";
        cout << '\n';
    }
}

template <typename T>
void read(vector <T>& v) {for (T& i : v) cin >> i;}

template <typename T>
void read(T&& t) {cin >> t;}

template <typename T, typename... Args>
void read(T&& t, Args&&... args)
{
    cin >> t;
    read(forward<Args>(args)...);
}

template <typename T>
void print(T&& t) {cout << t << '\n';}

template <typename T, typename... Args>
void print(T&& t, Args&&... args)
{
    cout << t << " ";
    print(forward<Args>(args)...);
}

void FAST_IO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(name.size())
    {
        freopen((name+".in").c_str(), "r", stdin);
        freopen((name+".txt").c_str(), "w", stdout);
    }
}
ll  binpow(ll a,  ll b, ll mod) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}
bool isprime(int n){
    if(n<=1){return false;}
    if(n<=3){return true;}
    if(n%2==0||n%3==0){return false;}
    for(int i=5;i*i<=n;i+=6){
        if(n%i==0||n%(i+2)==0){return false;}
    }
    return true;
}
void seive(ll n){
	vector<int> is_prime(n+1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i * i <= n; i++) {
	    if (is_prime[i]) {
	        for (int j = i * i; j <= n; j += i)
	            is_prime[j] = false;
	    }
	}
	// for(int i=2;i<=n;i++){
	// 	if(is_prime[i])
	// 		ans.pb(i);
	// }
}
map<ll,ll> primeFactors(ll n)
{
	map<ll,ll> m;
    while (n % 2 == 0)
    {
        m[2]++;
        n = n/2;
    }
    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            m[i]++;
            n = n/i;
        }
    }
    if (n > 2)
        m[n]++;
    return m;
}

multiset<ll> get_factors(ll n){
	multiset<ll> m;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0){
			if(n/i == i){
				m.insert(i);
			}
			else{
				m.insert(i);
				m.insert(n/i);
			}
		}
	}
	return m;
}

const ld pie = 3.14159265358979323846;
//const ll mod = 1000000007;
 const ll mod = 998244353;
const ll inf = 1e15;

bool check_palin(ll n){
	ll h = n;
	ll rev =0;
	while(n){
		rev = (rev*10)+ n%10;
		n/=10;
	}
	return (h==rev);
}
// n<=10 -- O(N!) O(N^7) O(N^6)
// n<20 -- O(2^N) o(N^5)
// n<=80 -- O(N^4)
// n<=400 -- O(N^3)
// n<=7500 -- O(N^2)
// n<=10^4 -- O(Nsqrt(N))
// n<=10^5 -- O(NlogN)
// n<=10^6 -- O(N)
// n<=10^18 -- O(logN) O(1)
ll  C(int n,int m)
{
	ll z=1;
	for(int i=0;i<m;i++){
		z = z* (n-i)/(i+1);
		print(z);
	}
	return z;
}

void solve(){
	ll n;
	cin>>n;
	vll v(n);
	for(auto &i : v)
		cin>>i;
	sort(all(v));
	ll j =1;
	ll flag =0;
	ll ans =0;
	for(ll i=0;i<n;i++){
		while(v[j]<=v[i]){
			j++;
		}
		//print(j);
		ans++;
		if(v[j]>v[i])
			j++;
		if(j==n)
			break;
	}
	print(ans);
}

int32_t main()
{
    FAST_IO();
    ll t;
   	t=1;
    while(t--) solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef map<ll,ll> mll;

typedef vector < pii > vpii;
typedef vector < pll > vpll;

typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;
typedef vector< vpii > vvpii;
typedef vector < vpll > vvpll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ms;

#define mem(a, b) memset(a, (b), sizeof(a))
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define po pop_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}}
#define endl "\n"
#define SZ(x) ((int)x.size())

//debug template
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second);
cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""),
                          __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

template <typename T>
void ckmin(T& a, const T b) {a = min(a, b);}

template <typename T>
void ckmax(T& a, const T b) {a = max(a, b);}

template <typename T>
void print(vector <T> v) {
    for (T i : v) cout << i << " ";
    cout << '\n';
}

template <typename T>
void print(vector <vector <T>>& v) {
    for (vector <T>& vv : v) {
        for (T& i : vv)
            cout << i << " ";
        cout << '\n';
    }
}

template <typename T>
void read(vector <T>& v) {for (T& i : v) cin >> i;}

template <typename T>
void read(T&& t) {cin >> t;}

template <typename T, typename... Args>
void read(T&& t, Args&&... args)
{
    cin >> t;
    read(forward<Args>(args)...);
}

template <typename T>
void print(T&& t) {cout << t << '\n';}

template <typename T, typename... Args>
void print(T&& t, Args&&... args)
{
    cout << t << " ";
    print(forward<Args>(args)...);
}

void FAST_IO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(name.size())
    {
        freopen((name+".in").c_str(), "r", stdin);
        freopen((name+".txt").c_str(), "w", stdout);
    }
}
ll  binpow(ll a,  ll b, ll mod) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}
bool isprime(int n){
    if(n<=1){return false;}
    if(n<=3){return true;}
    if(n%2==0||n%3==0){return false;}
    for(int i=5;i*i<=n;i+=6){
        if(n%i==0||n%(i+2)==0){return false;}
    }
    return true;
}
void seive(ll n){
	vector<int> is_prime(n+1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i * i <= n; i++) {
	    if (is_prime[i]) {
	        for (int j = i * i; j <= n; j += i)
	            is_prime[j] = false;
	    }
	}
	// for(int i=2;i<=n;i++){
	// 	if(is_prime[i])
	// 		ans.pb(i);
	// }
}
map<ll,ll> primeFactors(ll n)
{
	map<ll,ll> m;
    while (n % 2 == 0)
    {
        m[2]++;
        n = n/2;
    }
    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            m[i]++;
            n = n/i;
        }
    }
    if (n > 2)
        m[n]++;
    return m;
}

multiset<ll> get_factors(ll n){
	multiset<ll> m;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0){
			if(n/i == i){
				m.insert(i);
			}
			else{
				m.insert(i);
				m.insert(n/i);
			}
		}
	}
	return m;
}

const ld pie = 3.14159265358979323846;
//const ll mod = 1000000007;
 const ll mod = 998244353;
const ll inf = 1e15;

bool check_palin(ll n){
	ll h = n;
	ll rev =0;
	while(n){
		rev = (rev*10)+ n%10;
		n/=10;
	}
	return (h==rev);
}
// n<=10 -- O(N!) O(N^7) O(N^6)
// n<20 -- O(2^N) o(N^5)
// n<=80 -- O(N^4)
// n<=400 -- O(N^3)
// n<=7500 -- O(N^2)
// n<=10^4 -- O(Nsqrt(N))
// n<=10^5 -- O(NlogN)
// n<=10^6 -- O(N)
// n<=10^18 -- O(logN) O(1)
ll  C(int n,int m)
{
	ll z=1;
	for(int i=0;i<m;i++){
		z = z* (n-i)/(i+1);
		print(z);
	}
	return z;
}

void solve(){
	ll n;
	cin>>n;
	vll v(n);
	for(auto &i : v)
		cin>>i;
	ll i=0; ll j= n-1;
	ll ans =1;
	while(i<j){
		if(v[i]!=v[j]){
			ans++;
			i++;
			j--;
		}
		else{
			i++;
			j--;
		}
	}
	if(ans==1){
		print(0);
		return;
	}
	print(ans);
}

int32_t main()
{
    FAST_IO();
    ll t;
   	t=1;
    while(t--) solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef map<ll,ll> mll;

typedef vector < pii > vpii;
typedef vector < pll > vpll;

typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;
typedef vector< vpii > vvpii;
typedef vector < vpll > vvpll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ms;

#define mem(a, b) memset(a, (b), sizeof(a))
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define po pop_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}}
#define endl "\n"
#define SZ(x) ((int)x.size())

//debug template
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second);
cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""),
                          __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

template <typename T>
void ckmin(T& a, const T b) {a = min(a, b);}

template <typename T>
void ckmax(T& a, const T b) {a = max(a, b);}

template <typename T>
void print(vector <T> v) {
    for (T i : v) cout << i << " ";
    cout << '\n';
}

template <typename T>
void print(vector <vector <T>>& v) {
    for (vector <T>& vv : v) {
        for (T& i : vv)
            cout << i << " ";
        cout << '\n';
    }
}

template <typename T>
void read(vector <T>& v) {for (T& i : v) cin >> i;}

template <typename T>
void read(T&& t) {cin >> t;}

template <typename T, typename... Args>
void read(T&& t, Args&&... args)
{
    cin >> t;
    read(forward<Args>(args)...);
}

template <typename T>
void print(T&& t) {cout << t << '\n';}

template <typename T, typename... Args>
void print(T&& t, Args&&... args)
{
    cout << t << " ";
    print(forward<Args>(args)...);
}

void FAST_IO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(name.size())
    {
        freopen((name+".in").c_str(), "r", stdin);
        freopen((name+".txt").c_str(), "w", stdout);
    }
}
ll  binpow(ll a,  ll b, ll mod) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}
bool isprime(int n){
    if(n<=1){return false;}
    if(n<=3){return true;}
    if(n%2==0||n%3==0){return false;}
    for(int i=5;i*i<=n;i+=6){
        if(n%i==0||n%(i+2)==0){return false;}
    }
    return true;
}
void seive(ll n){
	vector<int> is_prime(n+1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i * i <= n; i++) {
	    if (is_prime[i]) {
	        for (int j = i * i; j <= n; j += i)
	            is_prime[j] = false;
	    }
	}
	// for(int i=2;i<=n;i++){
	// 	if(is_prime[i])
	// 		ans.pb(i);
	// }
}
map<ll,ll> primeFactors(ll n)
{
	map<ll,ll> m;
    while (n % 2 == 0)
    {
        m[2]++;
        n = n/2;
    }
    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            m[i]++;
            n = n/i;
        }
    }
    if (n > 2)
        m[n]++;
    return m;
}

multiset<ll> get_factors(ll n){
	multiset<ll> m;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0){
			if(n/i == i){
				m.insert(i);
			}
			else{
				m.insert(i);
				m.insert(n/i);
			}
		}
	}
	return m;
}

const ld pie = 3.14159265358979323846;
//const ll mod = 1000000007;
 const ll mod = 998244353;
const ll inf = 1e15;

bool check_palin(ll n){
	ll h = n;
	ll rev =0;
	while(n){
		rev = (rev*10)+ n%10;
		n/=10;
	}
	return (h==rev);
}
// n<=10 -- O(N!) O(N^7) O(N^6)
// n<20 -- O(2^N) o(N^5)
// n<=80 -- O(N^4)
// n<=400 -- O(N^3)
// n<=7500 -- O(N^2)
// n<=10^4 -- O(Nsqrt(N))
// n<=10^5 -- O(NlogN)
// n<=10^6 -- O(N)
// n<=10^18 -- O(logN) O(1)
ll  C(int n,int m)
{
	ll z=1;
	for(int i=0;i<m;i++){
		z = z* (n-i)/(i+1);
		print(z);
	}
	return z;
}

void solve(){
	ll n;
	cin>>n;
	vll v(n);
	for(auto &i : v)
		cin>>i;
	sort(all(v));
	ll j =1;
	ll flag =0;
	ll ans =0;
	for(ll i=0;i<n;i++){
		while(v[j]<=v[i] and j<n){
			j++;
		}
		//print(j);
		if(j==n)
			break;
		ans++;
		if(v[j]>v[i])
			j++;
	}
	print(ans);
}

int32_t main()
{
    FAST_IO();
    ll t;
   	t=1;
    while(t--) solve();
}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define forn(i,x,y) for(int i=x;i<y;++i)
#define fornr(i,x,y) for(long long i=x;i>=y;--i)
#define ever (;;)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e5; // 10^5;

int main()
{
	ios_base :: sync_with_stdio(false);
  	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	vector<int> v(n);
	map<int,int> m;
	forn(i,0,n){
		cin >> v[i];
		m[v[i]]++;
	}
	sort(all(v));
	int c = 0;
	for(int i = v.size()-1; i >= 0; i--){
		if(i - m[v[i]] >= 0 && v[i] > v[i - m[v[i]]] ){
			c++;
		}
	}
	cout << c;

  	return 0;
}

 	/*
		10 1 1 1 5  5 3
		1  1 1 3 5  5 10
		1  5 5 3 10 1 1
 	*/


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define forn(i,x,y) for(int i=x;i<y;++i)
#define fornr(i,x,y) for(long long i=x;i>=y;--i)
#define ever (;;)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e5; // 10^5;

int main()
{
	ios_base :: sync_with_stdio(false);
  	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	vector<ll> v(n);
	map<ll,ll> m;
	forn(i,0,n){
		cin >> v[i];
		m[v[i]]++;
	}
	vector<bool> us(n,0);
	sort(all(v));
	ll c = 0;
	for(int i = v.size()-1; i >= 0; i--){
		if(i - m[v[i]] >= 0 && !us[v[i-m[v[i]]]] && v[i] > v[i - m[v[i]]] ){
			c++;
			us[v[i-m[v[i]]]] = 1;
		}
	}
	cout << c;

  	return 0;
}

 	/*

	 	1 3 5 5 5

	*/


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define forn(i,x,y) for(int i=x;i<y;++i)
#define fornr(i,x,y) for(long long i=x;i>=y;--i)
#define ever (;;)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e5; // 10^5;

int main()
{
	ios_base :: sync_with_stdio(false);
  	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	vector<ll> v(n);
	map<ll,ll> m;
	forn(i,0,n){
		cin >> v[i];
		m[v[i]]++;
	}
	sort(all(v));
	ll c = 0;
	for(int i = v.size()-1; i >= 0; i--){
		if(i - m[v[i]] >= 0 && v[i] > v[i - m[v[i]]] ){
			c++;
		}
	}
	cout << c;

  	return 0;
}

 	/*
		10 1 1 1 5  5 3
		1  1 1 3 5  5 10
		1  5 5 3 10 1 1
 	*/


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define forn(i,x,y) for(int i=x;i<y;++i)
#define fornr(i,x,y) for(long long i=x;i>=y;--i)
#define ever (;;)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e5; // 10^5;

int main()
{
	ios_base :: sync_with_stdio(false);
  	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	vector<ll> v(n);
	map<ll,ll> m;
	map<ll,ll> m1;

	forn(i,0,n){
		cin >> v[i];
		m[v[i]]++;
		m1[v[i]]++;
	}
	vector<bool> us(n,0);
	sort(all(v));
	ll c = 0;
	for(int i = v.size()-1; i >= 0; i--){
		if(i - m[v[i]] >= 0 && m1[v[i-m[v[i]]]] && v[i] > v[i - m[v[i]]] ){
			c++;
			m1[v[i-m[v[i]]]]--;
		}
	}
	cout << c;

  	return 0;
}

 	/*

		1 1 1 3 5 5 10

	    4

	 	1 3 5 5 5

	*/


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define forn(i,x,y) for(int i=x;i<y;++i)
#define fornr(i,x,y) for(long long i=x;i>=y;--i)
#define ever (;;)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e5; // 10^5;

int main()
{
	ios_base :: sync_with_stdio(false);
  	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	vector<ll> v(n);
	vector<ll> u(n,0);
	map<ll,ll> m;
	map<ll,ll> m1;

	forn(i,0,n){
		cin >> v[i];
		m[v[i]]++;
		m1[v[i]]++;
	}
	vector<bool> us(n,0);
	sort(all(v));
	ll c = 0;
	for(int i = v.size()-1; i >= 0; i--){
	//	for(int k : v)
	//		cout << k << " ";
	//	cout << '\n';

		if(i - m[v[i]] >= 0 && u[i - m[v[i]]] != -1 && v[i] > v[i - m[v[i]]] ){
			c++;
			u[i - m[v[i]]] = -1;
		}else if(i - m[v[i]] >= 0 && u[i - m[v[i]]] == -1 ){
			int j = i-m[v[i]]-1;
			while(j >= 0 && u[j] == -1){
				--j;
			}
			if(j >= 0 && u[j] != -1){
				u[j] = -1;
				c++;
			}
		}

	}
	cout << c;

  	return 0;
}

 	/*

		3 5 2 2 5 2 4

       	2 2 2 3 4 5 5

        4

	*/


/*
"اله السماء يعطينا النجاح ونحن عبيده نقوم ونبني"
"تكفيك نعمتي لان قوتي في الضعف تكمل"
created by: shady rafet
my face book : https://www.facebook.com/profile.php?id=100019579575853
never give up
one day i will do it ':(

*****   ******        *           *       *********   ************     ********          *       **********
    *   *            * *         *               *   *            *    *      *         * *      *
    *   *           *   *       *               *   *              *   * ** * *        *   *     *
 ****   ******     *     *     *               *    *              *   **             *     *    **********
    *   *         *       *   *               *     *              *   *  *          * ***** *            *
    *   *        *         * *               *       *            *    *    *       *         *           *
*****   ******  *           *               *         ************     *       *   *           *  *********
*/
#include <bits/stdc++.h>
using namespace std;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define el '\n'
#define point complex<double>
#define rep(i,x,n) for (int i = x; i < n; i++)
#define lep(n) for (int i = n - 1; i > -1; i--)
#define all(v) v.begin(),v.end()
#define read(a,n) for(int i = 0 ;i<n;i++) cin>>a[i];
#define pb push_back
#define mms(i,v) memset(v,i,sizeof v)
typedef long long ll;
typedef long  double ld;
typedef unsigned long long ul;
const ll oo = 1e18;
const ll N = 1e6;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
/*
ll fn(ll x,ll y)
{
    if(y==0)
        return 1;
    ll ans = fn(x,y/2);
    ans = (ans*ans)%mod;
    if(y%2)
    {
        ans = (ans*(x%mod))%mod;
    }
    return ans ;
}
void pre(ll n)
{
    fact[0] = 1;
    fact [0] = 1;
    for(int i = 1;i<n;i++)
    {
        fact [i] = (fact[i-1] * i)%mod;
        inv[i] = fn(fact[i],mod-2);
    }
}
ll ncr(int n,int r)
{
    return (((fact[n]*inv[n-r])%mod)*inv[r])%mod;
}
*/
int main()
{
   int n;
   cin>>n;
   int a[n];
   rep(i,0,n)
   cin>>a[i];
   sort(a,a+n);
   int ans = 0,re = 0,cnt = 0;
   bool f ;
   for(int i = 1;i<n;i++)
   {
       if(a[i]>a[i-1])
       {
           ans++;
           int ad = min(re,cnt);
           ans+=ad;
           re-=ad;
           cnt-=ad;
           re = max(re,cnt);
           cnt = 0;
           f = 1;

       }
       else
       {
           if(!f)
            re++;
            else cnt++;

       }
   }
   cout<<ans;

return 0;
}


/*
"اله السماء يعطينا النجاح ونحن عبيده نقوم ونبني"
"تكفيك نعمتي لان قوتي في الضعف تكمل"
created by: shady rafet
my face book : https://www.facebook.com/profile.php?id=100019579575853
never give up
one day i will do it ':(

*****   ******        *           *       *********   ************     ********          *       **********
    *   *            * *         *               *   *            *    *      *         * *      *
    *   *           *   *       *               *   *              *   * ** * *        *   *     *
 ****   ******     *     *     *               *    *              *   **             *     *    **********
    *   *         *       *   *               *     *              *   *  *          * ***** *            *
    *   *        *         * *               *       *            *    *    *       *         *           *
*****   ******  *           *               *         ************     *       *   *           *  *********
*/
#include <bits/stdc++.h>
using namespace std;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define el '\n'
#define point complex<double>
#define rep(i,x,n) for (int i = x; i < n; i++)
#define lep(n) for (int i = n - 1; i > -1; i--)
#define all(v) v.begin(),v.end()
#define read(a,n) for(int i = 0 ;i<n;i++) cin>>a[i];
#define pb push_back
#define mms(i,v) memset(v,i,sizeof v)
typedef long long ll;
typedef long  double ld;
typedef unsigned long long ul;
const ll oo = 1e18;
const ll N = 1e6;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
/*
ll fn(ll x,ll y)
{
    if(y==0)
        return 1;
    ll ans = fn(x,y/2);
    ans = (ans*ans)%mod;
    if(y%2)
    {
        ans = (ans*(x%mod))%mod;
    }
    return ans ;
}
void pre(ll n)
{
    fact[0] = 1;
    fact [0] = 1;
    for(int i = 1;i<n;i++)
    {
        fact [i] = (fact[i-1] * i)%mod;
        inv[i] = fn(fact[i],mod-2);
    }
}
ll ncr(int n,int r)
{
    return (((fact[n]*inv[n-r])%mod)*inv[r])%mod;
}
*/
int main()
{
   int n;
   cin>>n;
   int a[n],mx = 0;
   map<int,int> mp;
  for(int i = 0;i<n;i++)
  {
      cin>>a[i];
      mp[a[i]]++;
      mx = max(mx,mp[a[i]]);
  }
  cout<<n-mx;

return 0;
}


/*
"اله السماء يعطينا النجاح ونحن عبيده نقوم ونبني"
"تكفيك نعمتي لان قوتي في الضعف تكمل"
created by: shady rafet
my face book : https://www.facebook.com/profile.php?id=100019579575853
never give up
one day i will do it ':(

*****   ******        *           *       *********   ************     ********          *       **********
    *   *            * *         *               *   *            *    *      *         * *      *
    *   *           *   *       *               *   *              *   * ** * *        *   *     *
 ****   ******     *     *     *               *    *              *   **             *     *    **********
    *   *         *       *   *               *     *              *   *  *          * ***** *            *
    *   *        *         * *               *       *            *    *    *       *         *           *
*****   ******  *           *               *         ************     *       *   *           *  *********
*/
#include <bits/stdc++.h>
using namespace std;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define el '\n'
#define point complex<double>
#define rep(i,x,n) for (int i = x; i < n; i++)
#define lep(n) for (int i = n - 1; i > -1; i--)
#define all(v) v.begin(),v.end()
#define read(a,n) for(int i = 0 ;i<n;i++) cin>>a[i];
#define pb push_back
#define mms(i,v) memset(v,i,sizeof v)
typedef long long ll;
typedef long  double ld;
typedef unsigned long long ul;
const ll oo = 1e18;
const ll N = 1e6;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
/*
ll fn(ll x,ll y)
{
    if(y==0)
        return 1;
    ll ans = fn(x,y/2);
    ans = (ans*ans)%mod;
    if(y%2)
    {
        ans = (ans*(x%mod))%mod;
    }
    return ans ;
}
void pre(ll n)
{
    fact[0] = 1;
    fact [0] = 1;
    for(int i = 1;i<n;i++)
    {
        fact [i] = (fact[i-1] * i)%mod;
        inv[i] = fn(fact[i],mod-2);
    }
}
ll ncr(int n,int r)
{
    return (((fact[n]*inv[n-r])%mod)*inv[r])%mod;
}
*/
int main()
{
   int n;
   cin>>n;
   int a[n];
   rep(i,0,n)
   cin>>a[i];
   sort(a,a+n);
   int ans = 0,m = (n)/2;
   m--;
   int mm = +1;
   for(int i = 0;i<=m;i++)
   {
       if(mm==n)
        break ;
       if(a[mm]>a[i])
       {
           ans++;
       }
       else i--;
       mm++;

   }

   if(mm<n&&a[mm]>a[mm-1])
    ans++;
   cout<<ans;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,k,l,m,p,i,j;
    cin>>n;
    //map<int,int> arr;
    vector<int> arr;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        //arr[t]++;
        arr.push_back(t);
    }

    sort(arr.begin(),arr.end());
    k=0;
    /*map<int,int>::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        int i=it->first;
        if(p+l==n)
            l-=arr[i];
        p+=arr[i];

        if(it==arr.begin())
        {
            if(arr[i]<l)
                k+=arr[i];
            else
            {
                k+=l;
                break;
            }
            continue;
        }

        int j=(--it)->first;
        it++;

        if(arr[i]<arr[j])
            l-=(arr[j]-arr[i]);

        if(l>0)
        {
            if(arr[i]>=l)
            {
                k+=l;
                break;
            }
            else
                k+=arr[i];
        }
        else
            break;
    }
*/
    i=j=0;
    while(j++<n)
    {
        if(arr[i]<arr[j])
        {
            k++;
            i++;
        }
    }
    cout<<k<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,k,l,m;
    cin>>n;
    map<int,int> arr;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        arr[t]++;
    }

    k=0;
    l=n;
    map<int,int>::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        int i=it->first;
        l-=arr[i];

        if(it==arr.begin())
        {
            if(arr[i]<l)
                k+=arr[i];
            else
            {
                k+=l;
                break;
            }
            continue;
        }

        int j=(--it)->first;
        it++;

        if(arr[i]<arr[j])
            m=l-(arr[j]-arr[i]);
        else
            m=l;

        if(m>0)
        {
            if(arr[i]>=m)
            {
                k+=m;
                break;
            }
            else
                k+=arr[i];
        }
        else
            break;
    }

    cout<<k<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,k,l,m,p,i,j;
    cin>>n;
    //map<int,int> arr;
    vector<int> arr;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        //arr[t]++;
        arr.push_back(t);
    }

    sort(arr.begin(),arr.end());
    k=0;
    /*map<int,int>::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        int i=it->first;
        if(p+l==n)
            l-=arr[i];
        p+=arr[i];

        if(it==arr.begin())
        {
            if(arr[i]<l)
                k+=arr[i];
            else
            {
                k+=l;
                break;
            }
            continue;
        }

        int j=(--it)->first;
        it++;

        if(arr[i]<arr[j])
            l-=(arr[j]-arr[i]);

        if(l>0)
        {
            if(arr[i]>=l)
            {
                k+=l;
                break;
            }
            else
                k+=arr[i];
        }
        else
            break;
    }
*/
    i=j=0;
    while(j<n)
    {
        if(arr[i]<arr[j])
        {
            k++;
            i++;
        }
        j++;
    }
    cout<<k<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,k,l,m,p;
    cin>>n;
    map<int,int> arr;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        arr[t]++;
    }

    k=0;
    l=n;
    p=0;
    map<int,int>::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        int i=it->first;
        if(p+l==n)
            l-=arr[i];
        p+=arr[i];

        if(it==arr.begin())
        {
            if(arr[i]<l)
                k+=arr[i];
            else
            {
                k+=l;
                break;
            }
            continue;
        }

        int j=(--it)->first;
        it++;

        if(arr[i]<arr[j])
            l-=(arr[j]-arr[i]);

        if(l>0)
        {
            if(arr[i]>=l)
            {
                k+=l;
                break;
            }
            else
                k+=arr[i];
        }
        else
            break;
    }

    cout<<k<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,k,l,m;
    cin>>n;
    map<int,int> arr;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        arr[t]++;
    }

    k=0;
    l=n;
    map<int,int>::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        int i=it->first;
        l-=arr[i];

        if(it==arr.begin())
        {
            if(arr[i]<l)
                k+=arr[i];
            else
            {
                k+=l;
                break;
            }
            continue;
        }

        int j=(--it)->first;
        it++;

        if(arr[i]<arr[j])
            l-=(arr[j]-arr[i]);

        if(l>0)
        {
            if(arr[i]>=l)
            {
                k+=l;
                break;
            }
            else
                k+=arr[i];
        }
        else
            break;
    }

    cout<<k<<endl;
}


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

int main()
{
    int n,coun=0,k=1;
    cin >> n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for(int i1=0;i1<n-1;i1++){
        for(int i2=k;i2<n;i2++){
            if(arr[i2]>arr[i1]){
            coun++;
            k=i2+1;
            break;

            }
           }
            if(k==(n+1)){
                break;
            }
        }

    cout << coun << endl;
    return 0;
}


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

int main()
{
    int n,coun=0,k=1;
    cin >> n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for(int i1=0;i1<n-1;i1++){
        for(int i2=k;i2<n;i2++){
            if(arr[i2]>arr[i1]){
            coun++;
            k=i2+1;
            break;

            }
        }
    }

    cout << coun << endl;
    return 0;
}


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

int main()
{
    int n,coun=0;
    cin >> n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for(int i1=0;i1<n-1;i1++){
        for(int i2=i1+1;i2<n;i2++){
            if(arr[i2]>arr[i1]){
            coun++;
            arr[i2]=0;

            }
        }
    }

    cout << coun << endl;
    return 0;
}


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

int main()
{
    int n,coun=0,k=1;
    cin >> n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    sort(arr,arr+n);
    for(int i1=0;i1<n-1;i1++){
        for(int i2=k;i2<n;i2++){
            if(arr[i2]>arr[i1]){
            coun++;
            k=i2+1;
            break;

            }
            if(k==(n+1)){
                break;
            }

        }
    }

    cout << coun << endl;
    return 0;
}


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

int main()
{
    int n,coun=0,k=1;
    cin >> n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    sort(arr,arr+n);
    int i1=0;
        for(int i2=k;i2<n;i2++){
            if(arr[i2]>arr[i1]){
            coun++;
            k=i2+1;
            i1++;

            }
           }

    cout << coun << endl;
    return 0;
}


#include <bits/stdc++.h>
#define STREAM_IO ios_base::sync_with_stdio(false)
#define STREAM_IO_BUFFERED cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define LL (ll)
#define ULL (ull)
#define UINT (uint)
#define mp make_pair
#define pb push_back
#define all(U) begin(U), end(U)
#define rall(U) (U).rbegin(), (U).rend()
#define SORT(U) sort(all(U))
#define REV(U) reverse(all(U))
#define LB(U, CMP) lower_bound(all(U), CMP)
#define UB(U, CMP) upper_bound(all(U), CMP)
#define imin(U) begin(U)
#define imax(U) (U).rbegin()
#define gmin(U) (*imin(U))
#define gmax(U) (*imax(U))
#define CPY(FROM, TO) memcpy(TO, FROM, min(sizeof FROM, sizeof TO))
#define CLR(U) memset(U, 0, sizeof U)
#define SZ(U) int(U.size())
#define bitscnt(U) __builtin_popcountll(ULL (U))
#define vdbg(U) cerr << __LINE__ << ' ' << #U << ": " << U << endl;
template <typename T1, typename T2>
inline istream& operator>>(istream& in, pair<T1, T2>& x) { return in >> x.first >> x.second; }
template <typename T>
inline istream& operator>>(istream& in, vector<T>& x) { for (T& y : x) in >> y; return in; }

inline void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	cin >> a;
	SORT(a);
	int cnt(0), cur(0), ans(0), pre(-1);
	for (int x : a) {
		if (x != pre) {
			cnt += cur;
			cur = 0;
		}
		ans += ++cur <= cnt;
		pre = x;
	}
	cout << ans;
}

int main() {
	STREAM_IO;
	STREAM_IO_BUFFERED;
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define STREAM_IO ios_base::sync_with_stdio(false)
#define STREAM_IO_BUFFERED cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define LL (ll)
#define ULL (ull)
#define UINT (uint)
#define mp make_pair
#define pb push_back
#define all(U) begin(U), end(U)
#define rall(U) (U).rbegin(), (U).rend()
#define SORT(U) sort(all(U))
#define REV(U) reverse(all(U))
#define LB(U, CMP) lower_bound(all(U), CMP)
#define UB(U, CMP) upper_bound(all(U), CMP)
#define imin(U) begin(U)
#define imax(U) (U).rbegin()
#define gmin(U) (*imin(U))
#define gmax(U) (*imax(U))
#define CPY(FROM, TO) memcpy(TO, FROM, min(sizeof FROM, sizeof TO))
#define CLR(U) memset(U, 0, sizeof U)
#define SZ(U) int(U.size())
#define bitscnt(U) __builtin_popcountll(ULL (U))
#define vdbg(U) cerr << __LINE__ << ' ' << #U << ": " << U << endl;
template <typename T1, typename T2>
inline istream& operator>>(istream& in, pair<T1, T2>& x) { return in >> x.first >> x.second; }
template <typename T>
inline istream& operator>>(istream& in, vector<T>& x) { for (T& y : x) in >> y; return in; }

inline void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	cin >> a;
	SORT(a);
	int cnt(0), cur(0), ans(0), pre(-1);
	for (int x : a) {
		if (x != pre) {
			cnt += cur;
			cur = 1;
		}
		ans += cur <= cnt;
		pre = x;
	}
	cout << ans;
}

int main() {
	STREAM_IO;
	STREAM_IO_BUFFERED;
	solve();
	return 0;
}