#P2034. Maximum Sum with Limited Consecutive Selections

    ID: 15316 Type: Default 1000ms 256MiB

Maximum Sum with Limited Consecutive Selections

Maximum Sum with Limited Consecutive Selections

You are given a sequence of n non-negative integers \(a_1, a_2, \ldots, a_n\). In addition, you are provided with an integer \(k\). Your task is to choose some of the numbers such that no more than \(k\) consecutive numbers are chosen, and the sum of the selected numbers is maximized.

Notes:

  • You may choose any subset of the given numbers provided that you do not select more than \(k\) consecutive numbers.
  • The array elements and \(k\) are non-negative integers.
  • It is allowed not to select any number at all.

The answer is the maximum sum achievable under these constraints.

inputFormat

The input consists of two lines:

  1. The first line contains two integers \(n\) and \(k\) separated by a space, where \(n\) is the number of elements in the sequence.
  2. The second line contains \(n\) non-negative integers \(a_1, a_2, \ldots, a_n\) separated by spaces.

outputFormat

Output a single integer, which is the maximum sum obtainable by selecting numbers under the given constraint.

sample

5 2
3 2 5 10 7
22