Need help with the following problem

Revision en1, by Imthiyash786, 2024-10-26 16:25:43

Given an array A of lower case characters $$$a_1,a_2,....,a_n$$$ we need to perform the following operation until it becomes empty,

  • Select a segment $$$(l,r)$$$ such that $$$a_l=a_{l+1}=a_{l+2}=....=a_r$$$ and remove the segment from the array.

  • Add the square of length of segment to your score and rearrange the remaining elements.

Output the maximum score that can be obtained.(score is initialized to 0).

Constraints: n <= 100 (If I recall correctly)

input

2
6
a a b b a c
12
a b a b a b a b a b a b

output

14
42

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Imthiyash786 2024-10-27 00:39:22 27 Tiny change: 'nts: n <= 100 (If I recall correctly)\n\n\n**in' -> 'nts: n <= 60\n\n\n**in'
en1 English Imthiyash786 2024-10-26 16:25:43 612 Initial revision (published)