C++ | Combination | Sorting
Intuition
If I choose the nums[i] as the minimum value, then I should only consider how many elements I can get more to make nums[i] as the minimum.
It means th
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- If I choose the `nums[i]` as the minimum value, then I should only consider how many elements I can get more to make `nums[i]` as the minimum. 
- It means that I can choose `k-1` elements among elements that are larger than `nums[i]`. This can be easily calculated if I sort the array.
- I can get the number of subsequences by iterating from choosing 0 element to choosing `min(k-1, n-i-1)` elements. 

# Approach
<!-- Describe your approach to solving the problem. -->
- Pre-calculating the combination of `nCk`.
- Sort the array to make it easy to get the number of larger or smaller values than `nums[i]`.
- Calculate the whole cases from choosing 0 element to choosing `min(k-1, n-i-1)` elements. 

# Complexity
- Time complexity: O(N*K) 
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N*K)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int n = (int)nums.size();
        long long comb[n+1][k+1];
        int MOD = 1e9+7;
        memset(comb, 0LL, sizeof(comb));
        comb[0][0] = 1;
        for (int i = 1; i <= n; ++i) {
            comb[i][0] = 1LL;
            for (int j = 1; j <= min(k, i); ++j) {
                comb[i][j] = comb[i-1][j-1] + comb[i-1][j];
                comb[i][j] %= MOD;
            }
        }

        long long ans = 0LL;
        for (int i = 0; i < n; ++i) {
            long long minVal = nums[i];
            long long maxVal = nums[n-i-1];
            int rest = n-i-1;
            for (int j = 0; j <= min(rest, k-1); ++j) {
                long long cnt = comb[rest][j];
                ans += ((cnt * minVal) % MOD + (cnt * maxVal) % MOD) % MOD;
                ans %= MOD;
            }
        }
        return ans;
    }
};
```
['Math', 'Sorting', 'Combinatorics', 'C++']