1692E.md (2053B)
1 Source: https://codeforces.com/problemset/problem/1692/E 2 3 E. Binary Deque 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Slavic has an array of length $n$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. 22 23 What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $s$ after performing all the operations? In case the sum $s$ can't be obtained after any amount of operations, you should output -1. 24 25 Input 26 27 The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. 28 29 The first line of each test case contains two integers $n$ and $s$ ($1 \leq n, s \leq 2 \cdot 10^5$) — the length of the array and the needed sum of elements. 30 31 The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 1$) — the elements of the array. 32 33 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. 34 35 Output 36 37 For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $s$, or -1 if obtaining an array with sum $s$ isn't possible. 38 39 Example 40 41 Input 42 43 ``` 44 7 45 46 3 1 47 48 1 0 0 49 50 3 1 51 52 1 1 0 53 54 9 3 55 56 0 1 0 1 1 1 0 0 1 57 58 6 4 59 60 1 1 1 1 1 1 61 62 5 1 63 64 0 0 1 1 0 65 66 16 2 67 68 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 69 70 6 3 71 72 1 0 1 0 0 0 73 ``` 74 75 Output 76 77 ``` 78 0 79 1 80 3 81 2 82 2 83 7 84 -1 85 ``` 86 87 Note 88 89 In the first test case, the sum of the whole array is $1$ from the beginning, so we don't have to make any operations. 90 91 In the second test case, the sum of the array is $2$ and we want it to be equal to $1$, so we should remove the first element. The array turns into $[1, 0]$, which has a sum equal to $1$. 92 93 In the third test case, the sum of the array is $5$ and we need it to be $3$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $[0, 1, 1, 1, 0, 0]$, which has a sum equal to $3$.