#include #include #include #include using namespace std; int best(int from, int to, int* bestchc, set* spreads, int n, int* ar); int main() { ios::sync_with_stdio(false); int n; while (cin >> n) { set* spreads = (set*)malloc(10000 * sizeof(set)); for (int i = 0; i < 10000; i++) { spreads[i] = set(); } int* bestchc = (int*) malloc(n * n * sizeof(int)); memset(bestchc, 0, n * n * sizeof(int)); int* ar = (int*)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { int spec; cin >> spec; spec--; ar[i] = spec; spreads[spec].insert(i); } cout << best(0, n - 1, bestchc, spreads, n, ar) << endl; } return 0; } int best(int from, int to, int* bestchc, set* spreads, int n, int* ar) { //cout << "best" << from << " " << to << endl; 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, bestchc, spreads, n, ar) + 1; if (curbest > max) max = curbest; } bestchc[from * n + to] = max + 1; return max; }