#P9797. Minimizing Class Schedule Span

    ID: 22943 Type: Default 1000ms 256MiB

Minimizing Class Schedule Span

Minimizing Class Schedule Span

You are a visiting student at a well-known university and wish to attend \(k\) classes. Unfortunately, you are only available during specific weeks. For each week \(i\), you are available if \(a_i = 1\) and unavailable if \(a_i = 0\). Your task is to choose \(k\) weeks (from the available weeks) such that the time span between your first attended class and your last attended class is minimized.

Note: You can choose the order in which you attend the classes. The time span is defined as the difference between the week numbers of the last and first attended classes. If it is impossible to attend \(k\) classes, output \(-1\).

Formally, if the indices of the weeks in which you are available are \(w_1, w_2, \dots, w_m\) (with \(w_i < w_{i+1}\)), you must choose a contiguous subsequence of length \(k\) to minimize \(w_{i+k-1} - w_i\). All parameters and numbers are in LaTeX format where applicable.

inputFormat

The input consists of two lines: The first line contains two integers (n) and (k) ((1 \le k \le n \le 10^5)) representing the number of weeks and the number of classes you wish to attend. The second line contains (n) integers (a_1, a_2, \dots, a_n), where each (a_i) is either 0 or 1. If (a_i = 1), you are available in week (i); otherwise, you are not available.

outputFormat

Output a single integer representing the minimum span (the difference between the week number of the last and the first attended class) you can achieve by choosing (k) available weeks. If it is impossible to attend (k) classes, output (-1).

sample

5 1
0 1 0 0 1
0

</p>