<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>

class Solution {
public:

    void bubbleUp(vector<pair<int,int>>& values, int index) {
        while(index > 1)
        {
            if(values[index / 2].second < values[index].second)
            {
                swap(values[index / 2], values[index]);
            } else {
                break;
            }
            index = index / 2;
        }
    }

    void bubbleDown(vector<pair<int,int>>& values, int index, int n) {
        while(index < n)
        {
            int leftChild = index * 2, rightChild = index*2 + 1;
            if(leftChild < n && rightChild < n)
            {
                if(values[index].second >= values[leftChild].second && values[index].second >= values[rightChild].second)
                {
                    break;
                }
                if(values[leftChild].second >= values[rightChild].second)
                {
                    swap(values[index], values[leftChild]);
                    index = leftChild;
                } else {
                    swap(values[index], values[rightChild]);
                    index = rightChild;
                }
            } else if(leftChild < n && values[index].second < values[leftChild].second ) {
                swap(values[index], values[leftChild]);
                index = leftChild;
            } else {
                break;
            }
        }
    }

    vector<pair<int,int>> buildHeap(vector<pair<int,int>> values) {
        vector<pair<int,int>> answer;
        answer.push_back(make_pair(0,0));
        int i = 1;
        for(auto& elem : values) {
            answer.push_back(elem);
            bubbleUp(answer, i);
            i++;
        }
        return answer;
    }
    void displayVector(vector<pair<int,int>> vec) {
        for(int i = 0 ; i < vec.size() ; i++) {
            cout << vec[i].first << ' ';
        } 
    }
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        vector<pair<int,int>> projectsOrder;
        vector<pair<int,int>> projects;
        for(int i = 0 ; i < profits.size() ; i++)
        {
            projects.push_back(make_pair(profits[i], capital[i]));
        }
        sort(projects.begin(), projects.end(), [](pair<int,int>& a, pair<int,int>& b) {
            return a.second < b.second;
        });
        int oldValue = -1, projectIndex = -1;
        int next = 0;
        while(next < projects.size() && projects[next].second <= w)
        {
            projectsOrder.push_back(make_pair(next, projects[next].first) );
            next++;
        }     
        projectsOrder = buildHeap(projectsOrder);
        int sz = profits.size();
        k = min(k, sz);
        for(int i = 0 ; i < k && projectsOrder.size() > 1 ; i++)
        {
            pair<int,int> project = projectsOrder[1];
            if(project.second == -1) {
                break;
            }
            w = w + project.second;
            projectsOrder[1] = projectsOrder.back();
            projectsOrder.pop_back();
            bubbleDown(projectsOrder, 1, projectsOrder.size());
            while(next < projects.size() && projects[next].second <= w)
            {
                projectsOrder.push_back(make_pair(next, projects[next].first));
                bubbleUp(projectsOrder, projectsOrder.size() - 1);
                next++;
            }
        }
        return w;
    }
};
143487