#C4657. Maximum Consecutive Sum
Maximum Consecutive Sum
Maximum Consecutive Sum
You are given a list of n integers and an integer C. Your task is to find the maximum sum of any C consecutive elements from the list. If the list contains fewer than C elements, output -1.
The problem can be formally stated as follows:
Given an array A of size n and an integer C, if n < C then output -1. Otherwise, find the maximum value of the sum:
\( S = \sum_{i=j}^{j+C-1} A_i \)
where \( 0 \leq j \leq n-C \). Use the sliding window technique to solve this problem efficiently.
inputFormat
The input is given via standard input (stdin) as follows:
- The first line contains two space-separated integers:
n
(the number of elements in the list) andC
(the number of consecutive elements to consider). - The second line contains
n
space-separated integers representing the list.
outputFormat
Output a single integer, which is the maximum sum of C
consecutive integers in the list. If the list has fewer than C
elements, output -1
.
10 4
1 2 3 4 5 6 7 8 9 10
34