P20975 [HackerRank] Billboards
提高
题目描述

ADZEN is a popular advertising firm in your city that owns all $n$ billboard locations on Main street. The city council passed a new zoning ordinance mandating that no more than $\boldsymbol{\mbox{k}}$ consecutive billboards may be up at any given time. For example, if there are $n=3$ billboards on Main street and $k=1$, ADZEN must remove either the middle billboard, the first two billboards, the last two billboards or the first and last billboard.

Being a for-profit company, ADZEN wants to lose as little advertising revenue as possible when removing the billboards. They want to comply with the new ordinance in such a way that the remaining billboards maximize their total revenues (i.e., the sum of revenues generated by the billboards left standing on Main street).

Given $n$, $\boldsymbol{\mbox{k}}$, and the revenue of each of the $n$ billboards, find and print the maximum profit that ADZEN can earn while complying with the zoning ordinance. Assume that Main street is a straight, contiguous block of $n$ billboards that can be removed but cannot be reordered in any way.

For example, if there are $n=7$ billboards, and $k=3$ is the maximum number of consecutive billboards that can be active, with $revenues=[5,6,4,2,10,8,4]$, then the maximum revenue that can be generated is $37$: $5+6+4+10+8+4=37$.

Function Description

Complete the billboards function in the editor below. It should return an integer that represents the maximum revenue that can be generated under the rules.

billboards has the following parameter(s):

k: an integer that represents the longest contiguous group of billboards allowed
revenue: an integer array where each element represents the revenue potential for a billboard at that index

Input Format

The first line contains two space-separated integers, $n$ (the number of billboards) and $\boldsymbol{\mbox{k}}$ (the maximum number of billboards that can stand together on any part of the road).

Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains an integer denoting the revenue value of billboard $\boldsymbol{i}$ (where $0\leq i<n$).

Constraints

$1\leq n\leq10^5$
$1\leq k\leq n$
$0\leq\:\text{revenue value of any billboard}\:\leq2\cdot10^9$

Output Format

Print a single integer denoting the maximum profit ADZEN can earn from Main street after complying with the city's ordinance.

Sample Input 0

6 2 
1
2
3
1
6
10 

Sample Output 0

21

Explanation 0

There are $n=6$ billboards, and we must remove some of them so that no more than $k=2$ billboards are immediately next to one another.

We remove the first and fourth billboards, which gives us the configuration _ 2 3 _ 6 10 and a profit of $2+3+6+10=21$. As no other configuration has a profit greater than $21$, we print $21$ as our answer.

Sample Input 1

5 4
1
2
3
4
5

Sample Output 1

14

Explanation 1

There are $n=5$ billboards, and we must remove some of them so that no more than $k=4$ billboards are immediately next to one another.

We remove the first billboard, which gives us the configuration _ 2 3 4 5 and a profit of $2+3+4+5=14$. As no other configuration has a profit greater than $\mathbf{14}$, we print $\mathbf{14}$ as our answer.

输入格式

见题面

输出格式

见题面

样例输入
6 2 1 2 3 1 6 10
样例输出
21