Max's Mex Med Max Problem
View as PDFMax has recently been studying various mathematic functions, and he found the Minimum Excluded Value (MEX) function to be quite fun. He also really likes the Median (MED) function, since it also starts with the letter "M". However, Max's absolute favourite math function is the Maximum (MAX) function, because it has the exact same name as him!
The of a set
is the smallest non-negative integer that is not in
. For example,
and
.
The of a set
is the median of the set. It is found by sorting
in non-decreasing order and taking the element at position
(numbered starting from
), where
denotes the size of the set. For example,
and
.
The of a set
is the maximum value in the set. For example,
and
.
Max also recently learned that a permutation of length is a sequence of
numbers that contains all the numbers from
exactly once. He has decided to come up with his own function on a permutation
.
Define as the number of pairs
that satisfy the following:
Let denote the set of all integers
such that
. Max is wondering what is the value of
. Please help him find it!
Constraints
It is guaranteed that is a permuation of the integers
.
Input Specification
The first line will contain two space-separated integers and
.
The second line will contain space-separated integers
.
Output Specification
If is not empty, output the value of
. Otherwise, output
.
Sample Input 1
5 2
0 1 3 2 4
Sample Output 1
3
Explanation for Sample 1
When ,
which is
. Below are the following pairs
:
:
:
Sample Input 2
4 5
0 3 1 2
Sample Output 2
-1
Comments
really accurate 12 point difficulty
Most testers agreed that the problem difficulty would be around 12p lol