The brute force approach is straightforward. We simply evaluate the sum of all possible subarrays and keep track of the maximum sum encountered during the evaluations. The outer loop will determine ...
We need to find the sum of the values of the $k$ subarrays that have the largest values (that is, difference between its maximum and minimum elements): $\text{val}(S ...