Yet another MEX problem MEXPROB

Read problem statements in Bengali, Mandarin Chinese, Russian, and Vietnamese as well.

The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

You are given an array $A$ of length $N$. You create a list $B$ consisting of the MEX-es of all subarrays of the array $A$. Formally, for all pairs $(l, r)$ such that $1 \leq l \leq r \leq N$, you calculate $MEX(A_l, A_{l + 1}, \dots, A_r)$ and append the value in the list $B$. Find the $K$-th smallest value in the list $B$.

Note: Since the size of the input and output is large, please use fast input-output methods.