#C6080. Minimum Subarray Length for Query Sums

    ID: 49801 Type: Default 1000ms 256MiB

Minimum Subarray Length for Query Sums

Minimum Subarray Length for Query Sums

You are given an array of integers \(A\) of length \(n\) and a list of \(q\) queries. For each query, you need to find the length of the smallest contiguous subarray whose sum is at least \(s\). If no subarray meets the condition, print -1.

Formally, for each query \(s\), find the minimum positive integer \(k\) such that there exists an index \(i\) where

[ \sum_{j=i}^{i+k-1} A_j \ge s, ]

and if there is no such \(k\), output -1.

This problem can be efficiently solved using the sliding window technique.

inputFormat

The first line of input contains two integers \(n\) and \(q\): the number of elements in the array and the number of queries respectively.

The second line contains \(n\) space-separated integers representing the array \(A\).

The third line contains \(q\) space-separated integers representing the query values.

outputFormat

Output \(q\) space-separated integers where the \(i\)-th integer is the length of the smallest contiguous subarray of \(A\) whose sum is greater than or equal to the \(i\)-th query. If no such subarray exists, output -1 for that query.

## sample
8 3
1 2 3 4 5 6 7 8
15 20 5
2 3 1