Mowing the Lawn
InterviewTime limit1sMemory limit128 MB
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 cows (), who stand in a single row numbered through from left to right. Some cows mow more efficiently than others: cow has efficiency ().
FJ has noticed that cows standing close together in the row are good friends: if he picks more than () 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 consecutive cows.
Input
- Line 1: Two space-separated integers, and .
- Lines 2 through : Line contains the single integer .
Output
- Line 1: A single integer, the best total efficiency FJ can obtain.
Hint
Suppose there are cows with efficiencies in that order, and no more than consecutive cows may be chosen. Choosing every cow except the third gives a total efficiency of , which is the maximum.