This page is still under construction.

Parts of this page are still being built. What you see may change.

Mowing the Lawn

Interview

Time limit1sMemory limit128 MB

Summary
Given N cows in a row with efficiencies, pick a subset that never includes more than K adjacent cows and maximize the total efficiency.
Level

Medium6 of 10

Topics
Dynamic programming, Sliding window, Queue, Prefix sum
Solved
No attempts yet

Problem

A year ago Farmer John won the town's annual best-lawn competition, and he has been lazy ever since: he has not mowed his lawn once, so it has grown completely unruly. The competition is coming up again soon, and FJ wants to whip his lawn back into top shape so he can reclaim the title.

The lawn is so overgrown that FJ cannot handle it alone; he needs help from his NN cows (1≤N≤100,0001 \le N \le 100{,}000), who stand in a single row numbered 11 through NN from left to right. Some cows mow more efficiently than others: cow ii has efficiency EiE_i (0≤Ei≤1,000,000,0000 \le E_i \le 1{,}000{,}000{,}000).

FJ has noticed that cows standing close together in the row are good friends: if he picks more than KK (1≤K≤N1 \le K \le N) consecutive (adjacent) cows, they will ignore the lawn and throw a party instead. Determine the largest total efficiency FJ can obtain without ever choosing more than KK consecutive cows.

Input

  • Line 1: Two space-separated integers, NN and KK.
  • Lines 2 through N+1N+1: Line i+1i+1 contains the single integer EiE_i.

Output

  • Line 1: A single integer, the best total efficiency FJ can obtain.

Hint

Suppose there are 55 cows with efficiencies 1,2,3,4,51, 2, 3, 4, 5 in that order, and no more than 22 consecutive cows may be chosen. Choosing every cow except the third gives a total efficiency of 1+2+4+5=121 + 2 + 4 + 5 = 12, which is the maximum.

Examples1

  1. Example 1

    Input
    5 2
    1
    2
    3
    4
    5
    
    Expected output
    12