Last Updated: 26 Aug, 2025

Palindromic Subsequence Annihilation

Moderate

Problem statement

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.


Input Format:
The first line of input contains a single string s.


Output Format:
Print a single integer representing the maximum achievable score.


Note:
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.