#K46772. Maximum Sum Subsequence of Fixed Length
Maximum Sum Subsequence of Fixed Length
Maximum Sum Subsequence of Fixed Length
Your task is to find the maximum sum of any contiguous subsequence of a specified length \( k \) from a given sequence of integers. If no contiguous subsequence of length \( k \) exists (i.e., if \( k > n \)), output 0.
Given \( n \) (the length of the sequence), \( k \) (the required length of the contiguous subsequence), and the sequence of integers, compute the maximum sum possible from any contiguous subsequence of length \( k \). You are encouraged to use a sliding window approach to solve this problem efficiently.
inputFormat
The input is provided via standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers \( n \) and \( k \), where \( n \) is the length of the sequence and \( k \) is the desired contiguous subsequence length.
- The second line contains \( n \) space-separated integers representing the sequence.
outputFormat
Output a single integer representing the maximum sum of any contiguous subsequence of length \( k \). If there is no such subsequence (i.e., when \( k > n \)), output 0.
## sample6 3
1 2 3 -2 5 -1
6