#abc290c. C - Max MEX

C - Max MEX

Score : 300300 points

问题描述

你将得到一个长度为 NN 的非负整数序列。

BB 是通过从 AA 中选择 KK 个元素并按原顺序拼接而成的序列时,找出 MEX(B)MEX(B) 可能的最大值。

这里,对于一个序列 XX,我们定义 MEX(X)MEX(X) 为满足以下条件的唯一非负整数 mm

  • 对于所有满足 0i<m0 \le i < m 的整数 ii,都包含在 XX 中。
  • mm 不包含在 XX 中。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a length-NN sequence of non-negative integers.
When BB is a sequence obtained by choosing KK elements from AA and concatenating them without changing the order, find the maximum possible value of MEX(B)MEX(B).

Here, for a sequence XX, we define MEX(X)MEX(X) as the unique non-negative integer mm that satisfies the following conditions:

  • Every integer ii such that 0i<m0 \le i < m is contained in XX.
  • mm is not contained in XX.

Constraints

  • All values in the input are integers.
  • 1KN3×1051 \le K \le N \le 3 \times 10^5
  • 0Ai1090 \le A_i \le 10^9

Input

The input is given from Standard Input in the following format:

NN KK

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer.

Sample Input 1

7 3
2 0 2 3 2 1 9

Sample Output 1

3

In this input, A=(2,0,2,3,2,1,9)A=(2,0,2,3,2,1,9), and you obtain the sequence BB by picking K=3K=3 elements. For example,

  • If the 11-st, 22-nd, and 33-rd elements are chosen, MEX(B)=MEX(2,0,2)=1MEX(B)=MEX(2,0,2)=1.
  • If the 33-rd, 44-th, and 66-th elements are chosen, MEX(B)=MEX(2,3,1)=0MEX(B)=MEX(2,3,1)=0.
  • If the 22-nd, 66-th, and 77-th elements are chosen, MEX(B)=MEX(0,1,9)=2MEX(B)=MEX(0,1,9)=2.
  • If the 22-nd, 33-rd, and 66-th elements are chosen, MEX(B)=MEX(0,2,1)=3MEX(B)=MEX(0,2,1)=3.

The maximum possible MEXMEX is 33.

update @ 2024/3/10 12:06:48