You are given N marbles in different colors. You have to remove marbles till there are no marbles left. Each time you can choose continuous marbles with the same color, remove them and get k*k points. Find the maximum points you can get.
1 ≤ N ≤ 100 0 ≤ Ai ≤ 100
INPUT 6 4 3 1 1 4 2 OUTPUT 10
Remove 1, then 3, then 4 and 2, we get 2*2+1*1+2*2+1*1 = 10
Please provide link to similar Problems.
Also I would like to know your dp states and Transitions
link the problem ?
Here is a similar problem just different language LINK
Keep
dp[i][j]
as being the maximum score of the interval[i, j)
. For each of these intervals, look at the first elementa[i]
. You can either just do1 + dp[i+1][j]
or combine multiple occurrences ofa[i]
in this interval. Say these indices aret1, t2, t3, ...
. then for all prefixes of this sequence, you can updatedp[i][j]
withdp[t1+1][t2] + dp[t2+1][t3] + ... + dp[tk+1][j] + k*k
. So you can just iterate for all possible k and get the max of them. You can check out the code here: codeEDIT: If you want a similar-style problem, I remember a classic one from the AtCoder DP contest N — Slimes
Thanks
It gets wrong answer on this test case.
The prefixes need not be continuous for optimal score. Here, if we consider range [0,7], leaving the second 1 at index 3 is best for optimal score (26) compared to taking it (score : 24)
You can test solution here.
https://leetcode.com/problems/remove-boxes/description/
yep, you are right :) i think i didn't see that at the time. thanks so much for pointing it out, though!