Easy DP Approach ,SC- O(k), TC- O(n*k) ,Optimised Code.
IntuitionApproachComplexity
Time complexity:O(n*k)

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

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

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

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

# Code
```cpp []
class Solution {
public:
    #define pii pair<ll,ll>
    #define ll long long
    int mod = 1e9+7;
    pii solve(vector<int> &nums,int pos,int taken,int k,vector<vector<vector<pii>>> & dp){
        
        if(taken<0){
            return {0,1};
        }
        if(pos == (nums.size()-1)){
            return {(nums[pos]*(taken+1)),1};
        }
        if(dp[taken][k][pos].first != -1){
            return dp[taken][k][pos];
        }
        pii curr = {0,0};
        pii curr2 = {2*nums[pos],1};
        pii v1 = solve(nums,pos+1,taken,k,dp);
        pii v2 = {0,0};
        if(k>=taken) v2 = solve(nums,pos+1,taken-1,k-1,dp);
        pii v3 = ((taken!=1) && (k!=0)) ? solve(nums,pos+1,taken,k-1,dp) : curr;
        pii v4 = (taken==1)?curr2:curr;
        
        v2.first += (nums[pos]*v2.second);
        
        
        return dp[taken][k][pos]={(v1.first+v2.first+v3.first+v4.first)%mod,(v1.second+v2.second+v3.second+v4.second)%mod};
    }
    
    
   int minMaxSums(vector<int>& nums, int k1) {
        sort(nums.begin(), nums.end());
        int n = nums.size();

        
        vector<vector<pii>> nxt(3, vector<pii>(k1+1, {0, 0}));
        vector<vector<pii>> curr(3, vector<pii>(k1+1, {0, 0}));
        
        for (int pos = n - 1; pos >= 0; pos--) {

            for (int taken = 0; taken < 3; taken++) {
                for (int k = 0; k <= k1; k++) {
                    if (taken == 0) {
                        curr[taken][k] = {0, 1};
                        continue;
                    }
                    if (pos == n - 1) {
                        curr[taken][k] = {(nums[pos] * taken), 1};
                        continue;
                    }

                    pii curr2 = {2 * nums[pos], 1};
                    pii v1 = nxt[taken][k];
                    pii v2 = {0, 0};
                    if (k >= (taken)) v2 = nxt[taken - 1][k - 1];
                    pii v3 = ((taken != 2) && (k > 1)) ? nxt[taken][k - 1] : pii{0, 0};
                    pii v4 = (taken == 2) ? curr2 : pii{0, 0};

                    v2.first += (nums[pos] * v2.second);

                   
                    curr[taken][k].first = (v1.first + v2.first + v3.first + v4.first) % mod;
                    curr[taken][k].second = (v1.second + v2.second + v3.second + v4.second) % mod;
                   
                }
            }
            nxt = curr;
        }

        return nxt[2][k1].first; 
    }
};
```
['C++']