Macaron Kama
Time limit1sMemory limit1024 MB
Pair each red shell with a blue shell from the same multiset so each macaron takes the larger size, and maximize the resulting digit string if it is a palindrome.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, String
- Solved
- No attempts yet
Problem
The land of fairies holds a macaron-making contest! A macaron is a dessert made by putting the filling of your choice between two baked shells. To prepare for the contest, Lulu first made the shells. Lulu made red shells and placed them on the floor. The size of the -th red shell is . Lulu also made blue shells, and the size of the -th blue shell is the same as the size of the -th red shell.
The contest has a special rule. The size of each macaron must be an integer from 1 to 9, and the contest must have exactly macarons in total. A participant's score is the number formed by writing the sizes of macarons 1 through in order. For example, when and , the score is 1423. The participant with the highest score wins the contest.
Lulu meant to place the -th blue shell on top of the -th red shell to finish the macarons, but decided to cheat to win. Lulu keeps the order of the red shells on the floor, but may place each blue shell on top of any red shell. After making all macarons, the larger shell of each pair is on top, so the fairies will think the larger shell's size is the size of that macaron.
Pixie, a fairy who was watching, allowed the trick on one condition: Lulu's score must be a palindrome. A palindrome reads the same forward and backward. For example, 12321 is a palindrome, but 3141 is not. Find the maximum score Lulu can get while meeting Pixie's request.
Input
The first line contains , the number of red shells Lulu made.
The second line contains a string of length giving the sizes of the red shells. The -th character is the size of the -th red shell, and is between 1 and 9.
Output
Print the maximum score Lulu can get on the first line.