#K64072. Maximum Contiguous Subarray Size Under Storage Limit

    ID: 31894 Type: Default 1000ms 256MiB

Maximum Contiguous Subarray Size Under Storage Limit

Maximum Contiguous Subarray Size Under Storage Limit

You are given n files with their sizes and a storage limit L (in MB). Your task is to find the maximum sum of a contiguous subarray of file sizes that does not exceed L.

In other words, let the array of file sizes be A with indices from 1 to n. You need to determine the maximum possible sum of some subarray A[i..j] (with 1 ≤ i ≤ j ≤ n) such that

\(\sum_{k=i}^{j} A[k] \le L\)

If no contiguous subarray has a sum ≤ L, then the answer is 0.

The challenge is to design an efficient algorithm to handle the scenario where n can be reasonably large.

inputFormat

The input is read from stdin and consists of two lines.

  • The first line contains two space-separated integers: n (the number of files) and L (the storage limit in MB).
  • The second line contains n space-separated integers representing the file sizes in MB.

outputFormat

Output to stdout a single integer which is the maximum total size (sum) of a contiguous subarray of file sizes that does not exceed the storage limit L. If no such subarray exists, output 0.

## sample
5 10
1 2 3 4 5
10

</p>