#K8871. Maximum Sum of K Consecutive Books' Pages
Maximum Sum of K Consecutive Books' Pages
Maximum Sum of K Consecutive Books' Pages
You are given a collection of books, each with a certain number of pages. Your task is to answer multiple queries where for each query you are provided an integer k. For each query, you must compute the maximum possible sum of pages for any k consecutive books in the list.
The solution should use an efficient algorithm such as the sliding window technique to handle large inputs. Note that the maximum sum for k consecutive books can be formally defined as:
\[ \max_{0 \leq i \leq n-k} \sum_{j=i}^{i+k-1} pages[j] \]
You are required to implement the solution to read from standard input (stdin) and output the result to standard output (stdout).
inputFormat
The first line of input contains two integers n
and q
separated by a space, where n
is the number of books and q
is the number of queries.
The second line contains n
space-separated integers representing the number of pages in each book.
The third line contains q
space-separated integers, each representing a query value k
.
outputFormat
For each query, output the maximum sum of pages for any k
consecutive books. Each result should be printed on a separate line.
5 3
1 2 3 4 5
1 3 5
5
12
15
</p>