Under construction

Still being built. Things may change

AAQQZ

Time limit2sMemory limit512 MB

Summary
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 S=(S1,S2,...,SN)S = (S_1, S_2, ..., S_N). The sequence (Si,Si+1,...,Sj)(S_i, S_{i+1}, ..., S_j) taken from the i-th to the j-th element (1≤i≤j≤N1 ≤ i ≤ j ≤ N) is called segment (i,j)(i, j). Segment (i,j)(i, j) is a palindrome when it equals its reversal, that is, when (Si,Si+1,...,Sj)=(Sj,Sj−1,...,Si)(S_i, S_{i+1}, ..., S_j) = (S_j, S_{j-1}, ..., S_i) holds.

JOI builds a palindrome with the following operation.

  1. First, choose one segment of S. Call the chosen segment T.
  2. Let T' be T sorted in ascending order.
  3. Replace segment T in sequence S with T'. Call the result S'. In other words, if JOI chooses segment (i,j)(i, j), sort Si,...,SjS_i, ..., S_j into Ti′≤Ti+1′≤...≤Tj′T'_i ≤ T'_{i+1} ≤ ... ≤ T'_j, and set S′=(S1,...,Si−1,Ti′,...,Tj′,Sj+1,...,SN)S' = (S_1, ..., S_{i-1}, T'_i, ..., T'_j, S_{j+1}, ..., S_N).
  4. 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 SiS_i, the i-th element of S.

Output

Print one integer on one line: the maximum length of a palindrome JOI can build.

Constraints

  • 1≤N≤30001 ≤ N ≤ 3000.
  • 1≤C≤30001 ≤ C ≤ 3000.
  • 1≤Si≤C1 ≤ S_i ≤ C (1≤i≤N1 ≤ i ≤ N).

Examples2

  1. Example 1

    Input
    12 26
    26
    17
    17
    17
    1
    26
    1
    17
    19
    20
    1
    14
    
    Expected output
    8
    
  2. Example 2

    Input
    4 3
    1
    2
    3
    2
    
    Expected output
    3