Algorithm to find maximum sum of elements in an array such that not more than k elements are adjacent
问题 I came across this question. Given an array containing only positive values, you want to maximize the sum of chosen elements under the constraint that no group of more than k chosen elements are adjacent. For example if input is 1 2 3 1 7 9 (n=6 and k =2). The output will be 21, which comes from picking the elements _ 2 3 _ 7 9. My simple DP solution is this #include<stdio.h> #include<limits.h> #include<malloc.h> long maxsum(int n,int k,long *sums){ long *maxsums; maxsums = malloc(sizeof(long