You are given a string s. You can perform an operation any number of times: choose any palindromic subsequence from the string and remove it.
For each removal, you gain a score equal to the square of the length of the subsequence you removed. After a subsequence is removed, the remaining characters in the string shift to form a new, shorter string.
Your task is to find the maximum total score you can achieve by removing all characters from the string.
The first line of input contains a single string s.
Print a single integer representing the maximum achievable score.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. It is not necessarily contiguous.
For example, in the string abacaba, aaaa is a palindromic subsequence.
The optimal strategy might involve removing multiple smaller palindromic subsequences rather than one large one.
abacaba
49
The entire string "abacaba" is itself a palindrome of length 7. By removing it in one operation, we get a score of 7 * 7 = 49. This is the optimal score.
abccba
26
The string "abccba" is not a palindrome. An optimal strategy is to first remove the palindromic subsequence "abcba" (length 5), which leaves the character "c".
- Score from removing "abcba": 5 * 5 = 25.
- Remaining string: "c".
- Remove the palindromic subsequence "c" (length1).Score: 1 * 1 = 1.
- Total Score: 25 + 1 = 26.
The expected time complexity is O(N^3), where N is the length of the string.
1 <= N <= 100
`s` consists of lowercase English letters.
Time limit: 1 sec
Time complexity is O(N^3), where N is the length of the string.