原题链接


B. Zuma



time limit per test



memory limit per test



input



output


n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.


Input



n (1 ≤ n ≤ 500) — the number of gemstones.

n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.


Output



Print a single integer — the minimum number of seconds needed to destroy the entire line.


Examples



input



3 1 2 1



output



1



input



3 1 2 3



output



3



input



7 1 4 4 2 3 2 1



output



2


Note



In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

4 4 first and then destroy palindrome 1 2 3 2 1.



用dp[l][r]表示[l, r]这个区间内最少需要几步destroy all the gemstones, d[l, r] = min(dp[l, j] + dp[j+1, r]) l <= j < r, 而且dp[l, r] = min(dp[l, r], dp[l+1, r-1])


#include <bits/stdc++.h>
#define INF 1e9
#define maxn 505
using namespace std;
typedef long long ll;

int dp[maxn][maxn], num[maxn];
int main(){
	
	//freopen("in.txt", "r", stdin);
	int n;
	
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	 scanf("%d", num+i);
	for(int i = 0; i < n; i++)
	 for(int j = 0; j < n; j++)
	  dp[i][j] = INF;
	for(int i = 0; i < n; i++){
		dp[i][i] = 1;
	} 
	for(int i = 1; i < n; i++)
	 for(int j = 0; j + i < n; j++){
	 	int l = j, r = i + j;
		 for(int h = l; h < r; h++){
		 	dp[l][r] = min(dp[l][r], dp[l][h] + dp[h+1][r]);
		 }
		 if(num[l] == num[r]){
		 	if(i == 1)
		 	 dp[l][r] = 1;
		 	else
		 	 dp[l][r] = min(dp[l][r], dp[l+1][r-1]);
		 }
	 }
	 printf("%d\n", dp[0][n-1]);
	 
	 return 0;
}