<p>You have an <code>inventory</code> of different colored balls, and there is a customer that wants <code>orders</code> balls of <strong>any</strong> color.</p>

<p>The customer weirdly values the colored balls. Each colored ball&#39;s value is the number of balls <strong>of that color&nbsp;</strong>you currently have in your <code>inventory</code>. For example, if you own <code>6</code> yellow balls, the customer would pay <code>6</code> for the first yellow ball. After the transaction, there are only <code>5</code> yellow balls left, so the next yellow ball is then valued at <code>5</code> (i.e., the value of the balls decreases as you sell more to the customer).</p>

<p>You are given an integer array, <code>inventory</code>, where <code>inventory[i]</code> represents the number of balls of the <code>i<sup>th</sup></code> color that you initially own. You are also given an integer <code>orders</code>, which represents the total number of balls that the customer wants. You can sell the balls <strong>in any order</strong>.</p>

<p>Return <em>the <strong>maximum</strong> total value that you can attain after selling </em><code>orders</code><em> colored balls</em>. As the answer may be too large, return it <strong>modulo </strong><code>10<sup>9 </sup>+ 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/jj.gif" style="width: 480px; height: 270px;" />
<pre>
<strong>Input:</strong> inventory = [2,5], orders = 4
<strong>Output:</strong> 14
<strong>Explanation:</strong> Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).
The maximum total value is 2 + 5 + 4 + 3 = 14.
</pre>

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

<pre>
<strong>Input:</strong> inventory = [3,5], orders = 6
<strong>Output:</strong> 19
<strong>Explanation: </strong>Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).
The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.
</pre>

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

<ul>
	<li><code>1 &lt;= inventory.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= inventory[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= orders &lt;= min(sum(inventory[i]), 10<sup>9</sup>)</code></li>
</ul>

class Solution {
public:
    int func(long long n, long long m, int cnt){
        int mod=1e9+7;
        long long x=(n*(n+1)/2);
        long long y=(m*(m+1)/2);
        //cout<<n<<" "<<m<<" "<<cnt<<" "<<x-y<<endl;
        return ((x-y)*cnt)%mod;
    }
    
    int maxProfit(vector<int>& nums, int orders) {
        int n=nums.size();
        long long cost=0;

        priority_queue<pair<long long,long long>> pq;
        sort(nums.begin(),nums.end());
        int mod=1e9+7;

        long long prev=nums[0],cnt=1;

        for(int i=1;i<n;i++){
            if(prev==nums[i]){
                cnt++;
            }else{
                pq.push({prev,cnt});
                prev=nums[i];
                cnt=1;
            }
        }
        
        pq.push({prev,cnt});
        pq.push({0,0});
        
        while(pq.size()>1){
            pair<long,long> top=pq.top();
            pq.pop();
            pair<long,long> second_top=pq.top();
            pq.pop();
            
            long long cnt=(long long)(top.first-second_top.first)*top.second;
            if(cnt>=orders){
                long long x=orders/top.second,y=orders%top.second;
                if(x!=0){
                    //cout<<cost<<endl;
                    cost+=func(top.first,top.first-x,top.second)%mod;
                    //cout<<cost<<endl;
                    if(y!=0)
                        cost+=((top.first-x)*y)%mod;
                }else{
                    cost+=(top.first*y)%mod;
                }
                return cost%mod;
            }else{
                cost+=func(top.first,second_top.first,top.second)%mod;
                orders-=cnt;
                pq.push({second_top.first,top.second+second_top.second});
            }
        }
        
        return cost%mod;
    }
};
81700