<p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>

<p>Divide the marbles into the <code>k</code> bags according to the following rules:</p>

<ul>
	<li>No bag is empty.</li>
	<li>If the <code>i<sup>th</sup></code> marble and <code>j<sup>th</sup></code> marble are in a bag, then all marbles with an index between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> indices should also be in that same bag.</li>
	<li>If a bag consists of all the marbles with an index from <code>i</code> to <code>j</code> inclusively, then the cost of the bag is <code>weights[i] + weights[j]</code>.</li>
</ul>

<p>The <strong>score</strong> after distributing the marbles is the sum of the costs of all the <code>k</code> bags.</p>

<p>Return <em>the <strong>difference</strong> between the <strong>maximum</strong> and <strong>minimum</strong> scores among marble distributions</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> weights = [1,3,5,1], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. 
The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. 
Thus, we return their difference 10 - 6 = 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> weights = [1, 3], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> The only distribution possible is [1],[3]. 
Since both the maximal and minimal score are the same, we return 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= weights.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= weights[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def putMarbles(self, weights: List[int], k: int) -> int:

        # THIS is not a DP solution, it would be nk and TLE
        # Greedy # editorial
        # O(nlogn) O(n)
        # We collect and sort the value of all n - 1 pairs.
        n = len(weights)
        pair_weights = []
        for i in range(n - 1):
            pair_weights.append(weights[i] + weights[i + 1])
        pair_weights.sort() # nlogn
        #print(pair_weights) #[4,6,8]
        ############total score = first + last + sum of pairs, ###### so we can remove 1, 1
        # Get the difference between the largest k - 1 values and the smallest k - 1 values.
        res = 0
        #max_score = min_score = weights[0] + weights[n-1] # as one distribution

        """
        for i in range(k - 1): #k-1 splits
            max_score += pair_weights[len(pair_weights)-1 - i] # score = first + last + larger k
            min_score += pair_weights[i] #score = first + last + smaller k
            #res += pair_weights[n - 2 - i] - pair_weights[i] #8-4, 6-6 = 4
        """
        #max_score = sum(pair_weights[-k+1:]) # this is not working for weights = [25,74,16,51,12,48,15,5], k = 1 => 462 but 0
        max_score = sum(pair_weights[len(pair_weights)-1-(k-2):]) # think about example for the split , 8
        min_score = sum(pair_weights[:k-1]) #k = 2, so pair_weights[:1] = 4
        print(max_score, min_score)
        return max_score - min_score    
        #return res        
        
537