#include using namespace std; #define PB push_back #define CL(A, I) (memset(A, I, sizeof(A))) #define D(X) cout<<" "<<#X": "< vi; typedef pair ii; typedef vector vii; int dp[5123][5123], a[5123]; int n; int dxh(int l, int r) { if (l >= r) return 0; if (dp[l][r] != -1) return dp[l][r]; int bst = 0; if (a[l] == a[r]) bst = dxh(l+1, r-1) + 1; bst = max(dxh(l+1, r), bst); bst = max(dxh(l, r-1), bst); return dp[l][r] = bst; } int main() { while (scanf("%d", &n) == 1) { F(n) scanf("%d", &a[i]); F(n) FF(n) dp[i][j] = -1; printf("%d\n", dxh(0, n-1)); } }