int f[N][N]; vector<int> g[N]; int a[N], cnt, n, ans[N]; intmain() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); f[i][i] = a[i]; g[i].push_back(i); } for (int len = 2; len <= n; len++) for (int i = 1; i + len - 1 <= n; i++) { int j = i + len - 1; for (int t = i; t < j; t++) if (f[i][t] == f[t + 1][j] && f[i][t] != 0) f[i][j] = f[i][t] + 1, g[j].push_back(i); }
int st = 1; for (int i = 1; i <= n; i++) { ans[i] = ans[i - 1] + 1; for (int j = 0; j < g[i].size(); j++) ans[i] = min(ans[i], ans[g[i][j] - 1] + 1); } printf("%d\n", ans[n]); }