Apple Tree(动态规划)

来源:互联网 发布:怎样才能在淘宝上开店 编辑:程序博客网 时间:2024/05/22 03:26

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

 

 

 

原创粉丝点击