#P5093. Shortest Missing Subsequence

    ID: 18331 Type: Default 1000ms 256MiB

Shortest Missing Subsequence

Shortest Missing Subsequence

John has N cows (with 1 ≤ N ≤ 100000) standing in a row. Each cow carries a tag indicating her breed, which is an integer in the range \(1 \ldots K\) (1 ≤ K ≤ 10000). While observing the sequence of cow breeds, John noticed that some sequences of numbers (not necessarily contiguous) appear and some do not. For example, consider the sequence:

1, 5, 3, 2, 5, 3, 4, 4, 2, 5, 1, 2, 3

John now wishes to construct a sequence using integers between 1 and K of minimum possible length such that this sequence does not appear as a subsequence of the cow lineup. Note that a subsequence is a sequence that can be derived from the original sequence by deleting some or no elements without changing the order of the remaining elements.

Determine the length of the shortest sequence (constructed from numbers 1 to K) that is missing as a subsequence in the given cow sequence.

The answer is guaranteed to exist.

inputFormat

The first line contains two integers N and K separated by a space.

The second line contains N integers representing the cow sequence.

outputFormat

Output a single integer, the length of the shortest sequence that does not appear as a subsequence in the cow lineup.

sample

13 5
1 5 3 2 5 3 4 4 2 5 1 2 3
4