#P2627. Maximum Efficiency Without Overcrowding
Maximum Efficiency Without Overcrowding
Maximum Efficiency Without Overcrowding
Farmer John once won the Best Lawn Competition in his town. However, after a year of resting, his lawn has become a complete mess. With the new competition approaching and wanting to reclaim his title, he needs help. Farmer John has N cows lined up in a row, numbered from 1 to N. Each cow i has an efficiency Ei.
When selecting cows to work on the lawn, there is a restriction: if more than K consecutive cows are chosen, they will go on strike to throw a party. In other words, you may choose at most K consecutive cows.
Your task is to help Farmer John compute the maximum total efficiency he can achieve by selecting a set of cows such that no more than K successive cows are chosen. Formally, if you denote the efficiencies as \(E_1, E_2, \dots, E_N\), choose a subset of indices such that in any block of consecutive indices that are selected, its length is \(\le K\), and maximize the sum of the chosen efficiencies.
inputFormat
The input consists of two lines. The first line contains two integers, N and K (1 ≤ N ≤ 105 and 1 ≤ K ≤ N). The second line contains N integers: E1, E2, ..., EN (0 ≤ Ei ≤ 109), representing the efficiencies of the cows.
outputFormat
Output a single integer, the maximum total efficiency Farmer John can achieve without selecting more than K consecutive cows.
sample
5 2
1 2 3 4 5
12
</p>