C++#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2505;
int n,m,a[N],mid,dp[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x==2)
{
x=-1;
}
a[i]=a[i-1]+x;
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
int sum=a[i]-a[j-1];
sum=abs(sum);
if(sum<=m || sum==i-j+1)
{
dp[i]=min(dp[i],dp[j-1]+1);
}
}
}
cout<<dp[n];
return 0;
}
/*
5 1
2
2
1
2
2
*/