# Can somebody help me to optimize my top down chefwed code basically i use the variation of pailindromic string but it gives me tle in 4 cases

`````` #include<bits/stdc++.h>
using namespace std;
int dp[10000];
int enf[1005][1005];
int countenf(int i,int j, vector<int> &l)
{
int enf=0;
unordered_map<int,int> m;
for(int p=i;p<=j;p++)
{
int temp=l[p];
if(m[temp])
{
m[temp]++;
}
else
m[temp]=1;
}
for(auto it=m.begin();it!=m.end();it++)
{
if(it->second>1)
enf+=it->second;
}
return enf;
}
bool check(vector<int>& a,int m,int k)
{
int i=m;
int j=k;
while(i<=k)
{
if(a[i]!=a[k])
return false;

i++;
k--;
}
return true;
}
int find(vector<int>& a,int i,int cos)
{  if(i<0)
return 0;
if(dp[i]!=-1)
return dp[i];
int ans=INT_MAX;
if(check(a,0,i))
return cos;
for(int k=i;k>=0;k--)
{

ans=min(ans,find(a,k-1,cos)+cos+enf[k][i]);

}
/*if(ans!=INT_MAX)
ans++;*/
dp[i]=ans;
return ans;

}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp,-1,sizeof(dp));
int n;
int k;
cin>>n>>k;
//unordered_map<int,int> m;
//int enf=0;
int ans=0;
vector<int> s;
int x;
for(int i=0;i<n;i++)
{
cin>>x;
s.push_back(x);
}
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
enf[i][j]=countenf(i,j,s);
cout<<find(s,s.size()-1,k)<<endl;
}
}``````