Maximum and Minimum Sums of at Most Size K Subsequences
Sort the values; each pair of positions (i, j) as min and max contributes a[i] + a[j] times the count of valid middle choices, summed with prefix sums of binomial counts.
Sort the values; each pair of positions (i, j) as min and max contributes a[i] + a[j] times the count of valid middle choices, summed with prefix sums of binomial counts.