#K86772. Longest Contiguous Subsequence with at Most One Skip
Longest Contiguous Subsequence with at Most One Skip
Longest Contiguous Subsequence with at Most One Skip
You are given an array of N integers and an integer S. Your task is to find the maximum length of a contiguous subsequence (or subarray) such that the sum of its elements is at most S. Additionally, you are allowed to remove (i.e., skip) at most one element from the array to potentially achieve a longer valid subsequence.
In other words, let the original sequence be \(a_1, a_2, \dots, a_N\). You may remove at most one element (or none) from the sequence. Then, consider all contiguous segments of the resulting sequence. Find the length of the longest segment for which the sum of its elements is \(\le S\).
Note: The removal of an element is performed once for the whole sequence, not separately for each contiguous block. The goal is to maximize the length of some contiguous segment after at most one removal.
inputFormat
The first line of the input contains two integers \(N\) and \(S\) separated by a space, where \(N\) is the number of elements in the sequence and \(S\) is the maximum allowed sum of a subsequence.
The second line contains \(N\) integers separated by spaces representing the elements of the sequence.
You should read from standard input (stdin).
outputFormat
Output a single integer representing the length of the longest contiguous subsequence with sum \(\le S\) that can be achieved by optionally skipping at most one element.
The output should be written to standard output (stdout).
## sample6 10
1 2 3 4 5 6
4