Knapsack-like problem oriented on contiguous selection
You can use dynamic programming.
Define $T^{0}(k_0,m_0,i)$- the optimal subset $S$, that uses $k_0$ elements from the set  $\{a_i,a_{i+1},\ldots,a_n\}$, includes $a_i$  uses less than (or equal to)  $m_0$ subgroups. We define similarly $T^{1}(k_0,m_0,i)$ but it does no includes the element $a_i$. We define $W(S)=\sum_{s\in S} Weight(s)$ for every subset $S$.

For every $k,m\geq 2$ there exists $j&gt;i$ such that begins with the subgroup $[a_{i},a_{i+1},\ldots, a_{j-1}]$. Thus, if we denote $T_j=\{[a_{i},a_{i+1},\ldots, a_{j-1}]\}\bigcup T^1(k_0-(j-i),m_0-1,j)$ then $T^{0}(k_0,m_0,i)$ is the subset with maximum weight among the $T_j$'s, i.e.,

$$T^{0}(k_0,m_0,i)=\arg\max_{T_j| \min(n,k_0+i)\geq j&gt;i} W(T_j)$$

In addition, if the first element of $T^{1}(k_0,m_0,i)$ is $a_j$, then  $T^{1}(k_0,m_0,i)$ equals to $T^{0}(k_0,m_0,j)$. Therefore,
 $$T^{1}(k_0,m_0,i)=\arg\max_{T=T^{0}(k_0,m_0,j)|n\geq j&gt;i} W(T)$$

Note that the initial conditions are  $T^{0}(1,m_0,i)=\{[a_i] \}$, $T^{0}(k_0,1,i)=\{[a_i,a_{i+1},\ldots,a_{i+k_0-1} ]\}$.

The optimal solution for the problem is the subset with the largest weight among the subsets $\{ T^{0}(k,m,1), T^{1}(k,m,1)\}$

We can compute each entry of $T^{0}(k_0,m_0,i_0)$ and $T^{1}(k_0,m_0,i_0)$ in $O(n)$ time. Since there are $O(k\cdot m\cdot n)$ entries of $T^{1},T^{2}$ the time complexity is $O(k\cdot m\cdot n^2)$.