#P1913F. Palindromic Problem

    ID: 8960 Type: RemoteJudge 5000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>binary searchdata structureshashingstring suffix structuresstrings*2800

Palindromic Problem

No submission language available for this problem.

Description

You are given a string ss of length nn, consisting of lowercase Latin letters.

You are allowed to replace at most one character in the string with an arbitrary lowercase Latin letter.

Print the lexicographically minimal string that can be obtained from the original string and contains the maximum number of palindromes as substrings. Note that if a palindrome appears more than once as a substring, it is counted the same number of times it appears.

The string aa is lexicographically smaller than the string bb if and only if one of the following holds:

  • aa is a prefix of bb, but aba \ne b;
  • in the first position where aa and bb are different, the string aa contains a letter that appears earlier in the alphabet than the corresponding letter in bb.

The first line contains one integer nn (1n31051 \leq n \leq 3 \cdot 10^5) — the number of characters in the string.

The second line contains the string ss itself, consisting of exactly nn lowercase Latin letters.

In the first line, print one integer — the maximum number of palindromic substrings that can be obtained using the operation described in the statement at most once.

In the second line, print the string that can be obtained from ss and has the maximum possible number of palindromic substrings. If there are multiple answers, print the lexicographically smallest one.

Input

The first line contains one integer nn (1n31051 \leq n \leq 3 \cdot 10^5) — the number of characters in the string.

The second line contains the string ss itself, consisting of exactly nn lowercase Latin letters.

Output

In the first line, print one integer — the maximum number of palindromic substrings that can be obtained using the operation described in the statement at most once.

In the second line, print the string that can be obtained from ss and has the maximum possible number of palindromic substrings. If there are multiple answers, print the lexicographically smallest one.

Sample Input 1

5
aabaa

Sample Output 1

15
aaaaa

Sample Input 2

5
aaaaa

Sample Output 2

15
aaaaa

Sample Input 3

4
awoo

Sample Output 3

7
aooo