AAQQZ
Time limit2sMemory limit512 MB
Given a sequence of N integers, sort one contiguous segment in ascending order, then find the longest palindromic segment that can appear afterward.
- Level
Medium6 of 10
- Topics
- Sorting, Prefix sum, String, Implementation
- Solved
- No attempts yet
Problem
The 2015 International Olympiad in Informatics is held in Kazakhstan. The name "Kazakh" is sometimes spelled "QAZAQ" in the Latin alphabet, and QAZAQ is a palindrome. JOI learns this and starts to like palindromes, so he decides to build palindromes from strings he finds.
JOI finds a string of length N. Each character is assigned an integer from 1 to C, and the string written as integers is . The sequence taken from the i-th to the j-th element () is called segment . Segment is a palindrome when it equals its reversal, that is, when holds.
JOI builds a palindrome with the following operation.
- First, choose one segment of S. Call the chosen segment T.
- Let T' be T sorted in ascending order.
- Replace segment T in sequence S with T'. Call the result S'. In other words, if JOI chooses segment , sort into , and set .
- Then, find a segment of S' that is a palindrome.
JOI wants to build the longest possible palindrome with this operation.
Given the sequence S that represents the string JOI found, find the maximum length of a palindrome JOI can build.
Input
The first line contains two integers N and C, separated by a space. N is the length of the string JOI found, and C is the upper limit of the integers assigned to the characters.
The next N lines contain the sequence S. The i-th of these lines contains the integer , the i-th element of S.
Output
Print one integer on one line: the maximum length of a palindrome JOI can build.
Constraints
- .
- .
- ().