C++ code
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

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

# Code
```cpp []
class Solution 
{
public:
   int mod = 1e9+7;
   void solve(vector<vector<int>>&dp,int n,int k)
   {
        for(int i=0;i<n;i++)
        {
            dp[i][0]=1;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=1;j<=min(i,k);j++)
            {
                dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%mod;
            }
        }
   }
int minMaxSums(vector<int>&v,int k) 
    {
          int n=v.size();
        vector<vector<int>>dp(n+1,vector<int>(k+1,0));
        solve(dp,n,k);
        sort(v.begin(),v.end());
        long long sum=0;
        for(int i=0;i<n-1;i++)
        {
            long long res=0;
            for(int j=0;j<k&&j<=n-i-1;j++)
            {
                res=dp[n-i-1][j];
                res*=v[i];
                res=res%mod;
                sum+=res;
                sum=sum%mod;
            }
        }
        sum+=v[n-1];
        sum=sum%mod;
        reverse(v.begin(),v.end());
        for(int i=0;i<n-1;i++)
        {
        long long res=0;
            for(int j=0;j<k && j<=n-i-1;j++)
            {
                res=dp[n-i-1][j];
                res*=v[i];
                res=res%mod;
                sum+=res;
                sum=sum%mod;
            }
        }
        sum+=v[n-1];
        sum=sum%mod;
       return sum;
    }
};

```
['Combinatorics', 'C++']