#C10545. Maximum Happiness Gain
Maximum Happiness Gain
Maximum Happiness Gain
You are given n attractions with associated happiness values. A visitor wants to visit l consecutive attractions such that the total happiness gained is maximized.
Formally, you are given an array h of size n and an integer l. You need to compute:
$$\max_{0 \le i \le n - l} \left(\sum_{j=i}^{i+l-1} h_j\right)$$
It is guaranteed that n is at least l.
inputFormat
The input is provided through standard input (stdin
) and contains two parts:
- The first line contains two integers, n and l, where n is the number of attractions and l is the number of consecutive attractions to select.
- The second line contains n space-separated integers representing the happiness values for each attraction.
outputFormat
Output a single integer to standard output (stdout
), which is the maximum possible happiness gain over any contiguous subarray of length l.
5 3
1 2 3 4 5
12