Apple Tree(动态规划)

Apple Tree

Time Limit: 10 Second      Memory Limit: 32768 KB

Description

There are so many problems called “Apple Tree”, including the one you are reading now.
After Leilei goes back home, he finds that all his grandpa’s apple trees go ripe. He is so happy and wants to pick some apples back to his school and shares them with all his friends, including the ones in the ACM group. Assume all the trees are in a line. To stop Leilei from picking all the apples, his grandpa asks him not to pick from two trees in K consecutive trees. So Leilei asks you, a genius programmer, to help him find out how many apples could he pick at most.
 

Input

There are multiple test cases.
In the first line of each test case there is only one integer N, indicating the number of grandpa’s apple trees. Here 0<N<=100000. A line with N=0 indicating the end of the input file.
In the second line of each test case there is only one positive integer K, which is described above.
In the next N lines there is one integer Ai per line, which is the number of apples in the ith tree. Here 0<Ai<=100.

 

Output

For each case, output one line with only one number which is the maximum apples Leilei can pick. 

Sample Input

5

2

1

2

3

4

5

3

3

10

10

10

0

Sample Output

9

10

 

 

 

 

题目大意:

在一序列数中,每连续K个数最多取一个数,使取到的数的和最大,输出和。

思路:

a[i]保存序列的数。

b[i]保存前i个数能取到的最大的和。

b[i]=max(a[i],b[i-1])=max(a[0],a[1],…a[i]).   i<k

b[i]=max(a[i]+b[i-k],b[i-1]).                  i>=k

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值