1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
typedef long long LL; const int inf = 0x3f3f3f3f; const int maxn = 250; int a[maxn], dp[maxn][maxn][maxn];
int DP(int i, int j, int k) { int& x = dp[i][j][k]; if (x != -1) { return x; } if (i > j) { return x = 0; } int p = j; while (p >= i && a[p] == a[j]) { --p; } x = DP(i, p, 0) + (j - p + k) * (j - p + k); for (int q = p - 1; q >= i; --q) { if (a[q] == a[j] && a[q] != a[q + 1]) { x = max(x, DP(q + 1, p, 0) + DP(i, q, j - p + k)); } } return x; }
int main() { int t, tt = 0; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } memset(dp, -1, sizeof dp); printf("Case %d: %d\n", ++tt, DP(1, n, 0)); } return 0; }
|