#include #include #include #include using namespace std; int best(int from, int to); set* spreads; int n; int* bestchc; int* ar; int main() { spreads = (set*)malloc(10000 * sizeof(set)); while (scanf("%d", &n) != EOF) { for (int i = 0; i < 10000; i++) { spreads[i] = set(); } bestchc = (int*) malloc(n * n * sizeof(int)); memset(bestchc, 0, n * n * sizeof(int)); ar = (int*)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { int spec; scanf("%d", &spec); spec--; ar[i] = spec; spreads[spec].insert(i); } printf("%d\n", best(0, n - 1)); free(bestchc); free(ar); } return 0; } int best(int from, int to) { if (bestchc[from * n + to] != 0) { return bestchc[from * n + to] - 1; } if (to - from < 1) { return 0; } int max = 0; for (int i = from; i < to + 1; i++) { int spec = ar[i]; int first = *(spreads[spec].lower_bound(from)); set::iterator lastit = spreads[spec].upper_bound(to); lastit--; int last = *lastit; if (first == last) { continue; } int curbest = best(first + 1, last - 1) + 1; if (curbest > max) max = curbest; } bestchc[from * n + to] = max + 1; return max; }