# include <stdio.h> # include <string.h> # define INF 0x3f3f3f3f int min(int a, int b){return a<b?a:b;} int a[501], dp[501][501]; int main() { int n, i, j, k, len; while(~scanf("%d",&n)) { for(i=0; i<=500; ++i) for(j=0; j<=500; ++j) dp[i][j] = INF; for(i=0; i<n; ++i) { scanf("%d",&a[i]); dp[i][i] = 1;//第一次初始化 } for(i=0; i<n-1; ++i)//第二次初始化 if(a[i]==a[i+1]) dp[i][i+1] = 1; else dp[i][i+1] = 2; for(len=2; len<n; ++len) { for(i=0; i+len<n; ++i) { if(a[i]==a[i+len]) dp[i][i+len] = dp[i+1][i+len-1]; for(k=i; k<i+len; ++k) dp[i][i+len] = min(dp[i][k]+dp[k+1][i+len], dp[i][i+len]); } } printf("%d\n",dp[0][n-1]); } return 0; }