<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 {
private:
    long long sumSeq(const int &s, const int &e) {
        if (s == e) {
            return s;
        } else {
            return static_cast<long long>(s+e)*(e-s+1)/2;
        }
    }
public:
    int maxProfit(vector<int>& inventory, int orders) {
        int n = inventory.size();
        vector<int> sums(n);
        long long res = 0;

        sort(inventory.begin(), inventory.end(), greater<int>());
#if 1
        if (n == 1) {
            return sumSeq(inventory[0]-orders+1, inventory[0]) % 1000000007;
        }
        for (int i = 1; i < n; i++) {
            int diff = inventory[i-1] - inventory[i];
            if (diff) {
                if (diff * i <= orders) {
                    res = (res + i * sumSeq(inventory[i] + 1, inventory[i-1])) % 1000000007;
                    orders -= diff * i;
                } else {
                    long long value = inventory[i-1];
                    while (orders) {
                        if (i <= orders) {
                            res = (res + i * value) % 1000000007;
                            orders -= i;
                            value--;
                        } else {
                            res = (res + orders * value) % 1000000007;
                            orders = 0;
                        }
                    }
                    break;
                }
            }
        }
        long long value = inventory[n-1];
        while (orders) {
            if (n <= orders) {
                int h = orders/n;
                int m = orders%n;
                res = (res + n * sumSeq(value-h+1, value)) % 1000000007;
                orders -= h*n;
                value -= h;
                res = (res + m * value) % 1000000007;
                orders = 0;
            } else {
                res = (res + orders * value) % 1000000007;
                orders = 0;
            }
        }
#else
        for (int i = 0; i < n-1; i++) {
            int diff = inventory[i] - inventory[i+1];
            if (diff) {
                if (i == 0) {
                    sums[i] = diff;
                } else {
                    sums[i] = sums[i-1] + (diff * (i+1));
                }
            }
            cout << "i:" << i << " sum:" << sums[i] << endl;
        }
        if (n > 1) {
            sums[n-1] = sums[n-2] + inventory[n-1] * n;
            cout << "i:" << n-1 << " sum:" << sums[n-1] << endl;
        } else {
            sums[0] = inventory[0];
        }
        
        auto iter = upper_bound(sums.begin(), sums.end(), sums[n-1]-orders);
        int idx = iter - sums.begin();
        int left = inventory[idx];
        cout << "n:" << n << " idx:" << idx << " left:" << left << endl;

        if (idx > 0) {
            for (int i = 0; i < idx; i++) {
                res = (res + sumSeq(left+1, inventory[i])) % 1000000007;
            }
            orders -= sums[idx-1];
        }
        cout << "res:" << res << " orders:" << orders << " left:" << left << endl;
        idx++;
        while (orders > 0) {
            int h = left;
            while ()
            if (orders < idx) {
                res = (res + left * orders) % 1000000007;
                orders = 0;
            } else {
                res = (res + left * idx) % 1000000007;
                orders -= idx;
            }
            left--;
            if (idx < n && left <= inventory[idx]) {
                idx++;
            }
        cout << "res:" << res << " orders:" << orders << " left:" << left << endl;
        }
#endif
        return res;
    }
};
71600