#P10169. Subarray MEX-MIN Challenge

    ID: 12159 Type: Default 1000ms 256MiB

Subarray MEX-MIN Challenge

Subarray MEX-MIN Challenge

You are given a sequence \(\{a_n\}\) and an integer \(k\). Your task is to count the number of subarrays \([l, r]\) (1-indexed) that satisfy the following inequality:

\(\operatorname{mex}\{a_l,a_{l+1},\dots,a_r\} + \min\{a_l,a_{l+1},\dots,a_r\} + k \geq \max\{a_l,a_{l+1},\dots,a_r\}\)

Here, the \(\operatorname{mex}\) of a set is defined as the smallest non-negative integer that is not present in the set.

inputFormat

The first line contains two integers \(n\) and \(k\), where \(n\) is the length of the sequence.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) denoting the sequence.

Note: 1-indexed input is assumed.

outputFormat

Output a single integer, representing the number of subarrays \([l, r]\) that satisfy the inequality.

sample

4 1
0 1 2 1
10