#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
int main() {
ll n,k;
cin>>n>>k;
ll arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
vector<vector<ll>> dp(n,vector<ll> (k+1,1));
//intialised to 1 keeping in viewing that seq has len==1 now
map<pair<ll,ll>,ll> m;
set<pair<ll,ll>> seta[k+1];
for(ll i=0;i<n;i++)
{
for(ll j=0;j<=k;j++)
{
// for(int p=0;p<i;p++)
// {
// ll a=-1,b=-1; if(arr[i]==arr[p]) a=dp[p][j]+1; else if(j>=1) b=dp[p][j-1]+1;
// dp[i][j]=max({dp[i][j],a,b});
// }
ll a=-1,b=-1;
a=m[{arr[i],j}]+1;
ll val=-2;
//seta[j]
if(j>=1 && !seta[j-1].empty())
{
auto e1=prev(seta[j-1].end());
if((*e1).s==arr[i])
{
if(e1!=seta[j-1].begin()) {e1=prev(e1);val=(*e1).f;}
}
else val=(*e1).f;
}
if(j>=1) b=val+1;
dp[i][j]=max({dp[i][j],a,b});
if(seta[j].find({m[{arr[i],j}],arr[i]})!=seta[j].end())
{
seta[j].erase({m[{arr[i],j}],arr[i]});
}
m[{arr[i],j}]=max(m[{arr[i],j}],dp[i][j]);
seta[j].insert({m[{arr[i],j}],arr[i]});
}
}
cout<<dp[n-1][k]<<endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHMgc2Vjb25kCgppbnQgbWFpbigpIHsKCWxsIG4sazsKCWNpbj4+bj4+azsKCglsbCBhcnJbbl07Cglmb3IoaW50IGk9MDtpPG47aSsrKSBjaW4+PmFycltpXTsKCgl2ZWN0b3I8dmVjdG9yPGxsPj4gZHAobix2ZWN0b3I8bGw+IChrKzEsMSkpOwoJLy9pbnRpYWxpc2VkIHRvIDEga2VlcGluZyBpbiB2aWV3aW5nIHRoYXQgc2VxIG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