#include <bits/stdc++.h>
#define pii pair < int , int >
#define ll long long
#define pipii pair < int , pair < int , int > >
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pri_que priority_queue
using namespace std;
const int maxn=3e5+14;
int n , k;
int a[maxn];
void sub12(){
if (k >= n - 1 ) {
cout << n;
return;
}
int ans = k + 1;
int cnt = 0;
for (int i = 1; i <= n; i++, cnt ++){
unordered_map < int , int > m;
int maxtanso = 1;
for (int j = i; j <= n; j ++, cnt++){
m[a[j]]++;
maxtanso = max(maxtanso, m[a[j]]);
if ( j - i + 1 - maxtanso > k) break;
ans = max(ans, j - i + 1);
}
}
cout << ans << endl ;
}
void sub3(){
unordered_map < int , vector < pii > > m ;
unordered_map < int , int > cnt ;
for (int i = 1; i <= n; i++){
if (cnt[a[i]] == 0){
m[a[i]].pb(mp(0, 0));
}
cnt[a[i]] ++;
m[a[i]].pb(mp(i, cnt[a[i]]));
}
int ans = k + 1;
if (k >= n - 1 ) {
cout << n;
return ;
}
unordered_map < int , int > dem ;
for (int i = 1; i <= n; i++){
dem[a[i]] ++;
int l = 1, r = m[a[i]].size();
int res = 1;
while (l <= r) {
int d = (l + r) >> 1;
if (m[a[i]][d].fi >= i) {
r = d - 1;
continue;
}
int maxtanso = dem[a[i]] - m[a[i]][d].se + 1;
if (i + 1 - maxtanso - k > m[a[i]][d].fi) l = d + 1;
else {
res = max(res, min(maxtanso + k , n));
r = d - 1;
}
}
ans = max(ans , res);
}
cout << ans ;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("SEQ.INP","r")){
freopen("SEQ.INP","r",stdin);
freopen("SEQ.OUT","w",stdout);
}
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
if (n <= 5000) sub12();
else sub3();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGlpIHBhaXIgPCBpbnQgLCBpbnQgPgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVm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