Microwavable Subsequence

시간 제한1초메모리 제한2048 MB

요약
x < y인 모든 값 쌍에 대해 x와 y만 쓰고 인접한 원소가 서로 다른 가장 긴 부분수열의 길이를 구해 모두 더한다.
난이도

어려움10점 중 8점

유형
배열, 동적 계획법, 조합론, 수학
정답자
아직 제출이 없습니다

문제

You are given an array of NN integers: \[A_1,A_2,…,A_N]\[A\_1, A\_2, \dots , A\_N ].

A subsequence can be derived from an array by removing zero or more elements without changing the order of the remaining elements. For example, \[2,1,2]\[2, 1, 2], \[3,3]\[3, 3], \[1]\[1], and \[3,2,1,3,2]\[3, 2, 1, 3, 2] are subsequences of array \[3,2,1,3,2]\[3, 2, 1, 3, 2], while \[1,2,3]\[1, 2, 3] is not a subsequence of array \[3,2,1,3,2]\[3, 2, 1, 3, 2].

A subsequence is microwavable if the subsequence consists of at most two distinct values and each element differs from its adjacent elements. For example, \[2,1,2]\[2, 1, 2], \[3,2,3,2]\[3, 2, 3, 2], and \[1]\[1] are microwavable, while \[3,3]\[3, 3] and \[3,2,1,3,2]\[3, 2, 1, 3, 2] are not microwavable.

Denote a function f(x,y)f(x, y) as the length of the longest microwavable subsequence of array AA such that each element within the subsequence is either xx or yy. Find the sum of f(x,y)f(x, y) for all 1≤x<y≤M1 ≤ x < y ≤ M.

입력

The first line consists of two integers NN MM (1≤N,M≤300,0001 ≤ N, M ≤ 300\\, 000).

The second line consists of NN integers A_iA\_i (1≤A_i≤M1 ≤ A\_i ≤ M).

출력

Output a single integer representing the sum of f(x,y)f(x, y) for all 1≤x<y≤M1 ≤ x < y ≤ M.

예제2

  1. 예제 1

    입력
    5 4
    3 2 1 3 2
    
    예상 출력
    13
    
  2. 예제 2

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