#C3318. Shortest Subarray with Sum at Least K

    ID: 46732 Type: Default 1000ms 256MiB

Shortest Subarray with Sum at Least K

Shortest Subarray with Sum at Least K

Given a sequence of integers (a_1, a_2, \dots, a_n) and an integer (k), your task is to find the length of the shortest contiguous subarray such that the sum of its elements is at least (k). More formally, you need to find the minimum length (L) for which there exists an index (i) such that (\sum_{j=i}^{i+L-1} a_j \ge k). If no such subarray exists, output -1.

The input is read from standard input (stdin) and the output is written to standard output (stdout).

inputFormat

The first line contains an integer (n), representing the number of elements in the array.
The second line contains (n) space-separated integers, the elements of the array.
The third line contains an integer (k), representing the target sum.

outputFormat

Print a single integer: the length of the shortest contiguous subarray whose sum is at least (k). If no such subarray exists, print -1.## sample

10
2 3 1 2 4 3 1 1 1 6
7
2