<p>Suppose LeetCode will start its <strong>IPO</strong> soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the <strong>IPO</strong>. Since it has limited resources, it can only finish at most <code>k</code> distinct projects before the <strong>IPO</strong>. Help LeetCode design the best way to maximize its total capital after finishing at most <code>k</code> distinct projects.</p>

<p>You are given <code>n</code> projects where the <code>i<sup>th</sup></code> project has a pure profit <code>profits[i]</code> and a minimum capital of <code>capital[i]</code> is needed to start it.</p>

<p>Initially, you have <code>w</code> capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.</p>

<p>Pick a list of <strong>at most</strong> <code>k</code> distinct projects from given projects to <strong>maximize your final capital</strong>, and return <em>the final maximized capital</em>.</p>

<p>The answer is guaranteed to fit in a 32-bit signed integer.</p>

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

<pre>
<strong>Input:</strong> k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Since your initial capital is 0, you can only start the project indexed 0.
After finishing it you will obtain profit 1 and your capital becomes 1.
With capital 1, you can either start the project indexed 1 or the project indexed 2.
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
</pre>

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

<pre>
<strong>Input:</strong> k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
<strong>Output:</strong> 6
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= w &lt;= 10<sup>9</sup></code></li>
	<li><code>n == profits.length</code></li>
	<li><code>n == capital.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= profits[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= capital[i] &lt;= 10<sup>9</sup></code></li>
</ul>

#define ll long long

class Solution {
public:

    vector< pair<long long, long long> > vv;

    pair<ll,int> tree[400005];

    void update(int node, int b, int e, int id, ll x) {
        if(id<b || id>e) return;
        if(id == b && id == e) {
            tree[node].first = x;
            tree[node].second = id;
            return;
        }

        int left = 2*node;
        int right = left+1;
        int mid = (b+e)/2;

        update(left, b, mid, id, x);
        update(right, mid+1, e, id, x);

        tree[node].first = max(tree[left].first, tree[right].first);
        if(tree[left].first > tree[right].first) {
            tree[node].second = tree[left].second;
        } else tree[node].second = tree[right].second;

    }

    pair<ll, int> query(int node, int b, int e, int l, int r) {
        if(l > e || b > r) return make_pair(-1,-1);
        if(b>=l && e<=r) return tree[node];

        int mid = (b+e)/2;
        int left = 2*node;
        int right = left+1;

        pair<ll, int> p = query(left, b, mid, l, r);
        pair<ll, int> q = query(right, mid+1, e, l, r);

        if(p.first > q.first) return p;
        else return q;
    } 

    int bin(ll x) {
        int ret = -1;
        int l = 0, r = vv.size() - 1;

        while(l<=r) {
            int mid = (l+r)/2;
            if(vv[mid].first <= x) {
                ret = mid;
                l = mid+1;
            }else {
                r = mid-1;
            }
        }

        return ret;
    }

    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        for(int i = 0; i<profits.size(); i++) {
            long long x = capital[i];
            long long y = profits[i];

            vv.push_back(make_pair(x, y));
        }

        sort(vv.begin(), vv.end());

        for(int i=0; i<vv.size(); i++) {
            update(1, 0, vv.size()-1, i, vv[i].second);
        }

        ll ret = w;

        for(int i=0; i<k; i++) {
            int midd = bin(ret);
            if(midd == -1) break;

            pair<ll, int> x = query(1, 0, vv.size()-1, 0, midd);
            if(x.first == -1) {
                break;
            }
            ret+=x.first;
            update(1, 0, vv.size() - 1, x.second, 0);
        }

        return ret;
    }
};
144362