아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Painting

시간 제한2초메모리 제한256 MB

요약
울타리 n개 구간의 목표 색이 주어질 때, m개 색 각각에 대해 한 번씩 구간을 칠하는 순서를 정해 총 칠한 길이의 최댓값을 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 구간
정답자
아직 제출이 없습니다

문제

You have been hired to paint a fence. The fence consists of nn sections, numbered 11 through nn. The fence is required to be eventually painted using mm colors, numbered 11 through mm. For each section ii, the desired color c_ic\_i of that section is known.

Your order also specifies how the painting process should look like. The painting should be conducted in exactly mm phases. In each phase, you can pick some color c∈1,…,mc\in\\{1,\ldots,m\\} and two indices a,ba,b, 1≤a≤b≤n1\leq a\leq b\leq n, and paint the segments a,a+1,…,ba,a+1,\ldots,b with color cc. It takes b−a+1b-a+1 hours to perform such a phase. If some of these segments has been painted before, the previous color is replaced with cc. Initially, all segments are unpainted.

Is is guaranteed that it is possible to paint the fence as required using the above process. However, you are free to decide how the individual phases will look like. Since you are paid hourly, you would like the painting to take as much time as possible.

Consider the following example. Let n=4n=4, m=3m=3 and suppose the subsequent segments have to be painted with colors (2,1,2,3)(2, 1, 2, 3). We could paint the fence so that the colors change as follows (here, 00 denotes unpainted): (0,0,0,0)→(0,0,0,3)→(2,2,2,3)→(2,1,2,3).(0, 0, 0, 0)\to (0, 0, 0, 3)\to (2, 2, 2, 3)\to (2, 1, 2, 3). Such a painting takes 1+3+1=51+3+1=5 hours. However, we could also spend 88 hours (ans thus earn more money) if we proceeded as follows: (0,0,0,0)→(3,3,3,3)→(2,2,2,3)→(2,1,2,3).(0, 0, 0, 0)\to (3, 3, 3, 3)\to (2, 2, 2, 3)\to (2, 1, 2, 3). 

Compute the maximum possible painting time.

입력

The first line of the input contains two integers nn, mm (1≤n≤1051\leq n\leq 10^5, 1≤m≤50001\leq m\leq 5000), denoting the number of the fence's segments and the number of used colors. The second line of the input contains nn integers c_1,…,c_nc\_1,\ldots,c\_n (1≤c_i≤m1\leq c\_i\leq m) that describe the desired colors of individual segments. It is guaranteed that each of the mm colors appears at least once in that sequence, i.e., c_1,…,c_n=1,…,m\\{c\_1,\ldots,c\_n\\}=\\{1,\ldots,m\\}.

출력

You should output the maximum possible painting time when painting according to the described rules.

예제1

  1. 예제 1

    입력
    4 3
    2 1 2 3
    
    예상 출력
    8