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

struct Project
{
    Project(): index(0), profit(0), capital(0) 
    {

    }

    Project(int index, int profit, int capital):
    index(index), profit(profit), capital(capital)
    {

    }

    int index;
    int profit;
    int capital;
};

// Construct max heap based on profit
struct MaxHeap_Profit
{
public:

    MaxHeap_Profit():
    array(vector<Project>(0)),
    heapsize(0)
    {

    }

    void insert(const Project& project)
    {
        ++heapsize;
        if (heapsize > array.size())
        {
            array.push_back(Project(-1, numeric_limits<int>::min(), 0));
        }
        else
        {
            array[heapsize - 1] = Project(-1, numeric_limits<int>::min(), 0);
        }
        increase_key(heapsize, project);
    }

    int size()
    {
        return heapsize;
    }

    Project maximum()
    {
        return array[0];
    }

    Project extract_maximum()
    {
        Project maximum = array[0];
        array[0] = array[heapsize - 1];
        --heapsize;
        max_heapify(1);
        return maximum;
    }

private:

    void increase_key(int index, const Project& project)
    {
        array[index - 1] = project;
        int parent = index / 2;
        if (index > 1 && array[parent - 1].profit < array[index - 1].profit)
        {
            array[index - 1] = array[parent - 1];
            increase_key(parent, project);
        }
    }
    
    void max_heapify(int index)
    {
        int maximum_index = index;
        int left_child = 2 * index;
        int right_child = 2 * index + 1;
        if (left_child <= heapsize && array[left_child - 1].profit > array[maximum_index - 1].profit)
        {
            maximum_index = left_child;
        }
        if (right_child <= heapsize && array[right_child - 1].profit > array[maximum_index - 1].profit)
        {
            maximum_index = right_child;
        }
        if (maximum_index != index)
        {
            Project array_index = array[index - 1];
            array[index - 1] = array[maximum_index - 1];
            array[maximum_index - 1] = array_index;
            max_heapify(maximum_index);
        }
    }

    vector<Project> array;
    int heapsize;
};

// Construct min heap based on capital
struct DescendingSortedList_Capital
{
public:

    DescendingSortedList_Capital():
    array(vector<Project>(0))
    {

    }

    void insert(const Project& project)
    {
        array.push_back(Project(-1, -1, numeric_limits<int>::max()));
        decrease_key(array.size(), project);
    }

    int size()
    {
        return array.size();
    }

    void sort()
    {
        int heapsize = array.size();
        for (int index = array.size(); index >= 2; --index)
        {
            Project array_min = array[0];
            array[0] = array[index - 1];
            array[index - 1] = array_min;
            --heapsize;
            min_heapify(1, heapsize);
        }
    }

    Project minimum()
    {
        return array.back();
    }

    Project extract_minimum()
    {
        Project minimum = array.back();
        array.pop_back();
        return minimum;
    }

private:

    void decrease_key(int index, const Project& project)
    {
        array[index - 1] = project;
        int parent = index/2;
        if (index > 1 && array[parent - 1].capital > array[index - 1].capital)
        {
            array[index - 1] = array[parent - 1];
            decrease_key(parent, project);
        }
    }

    void min_heapify(int index, int heapsize)
    {
        int minimum_index = index;
        int left_child = 2 * index;
        int right_child = 2 * index + 1;
        if (left_child <= heapsize && array[left_child - 1].capital < array[minimum_index - 1].capital)
        {
            minimum_index = left_child;
        }
        if (right_child <= heapsize && array[right_child - 1].capital < array[minimum_index - 1].capital)
        {
            minimum_index = right_child;
        }
        if (minimum_index != index)
        {
            Project array_index = array[index - 1];
            array[index - 1] = array[minimum_index - 1];
            array[minimum_index - 1] = array_index;
            min_heapify(minimum_index, heapsize);
        }
    }

    vector<Project> array;

};

class Solution 
{
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) 
    {
        // Prepare minimum heap based on capital required to complete a project
        DescendingSortedList_Capital sorted_list_capital;
        for (int i = 0; i < profits.size(); ++i)
        {
            Project project(i, profits[i], capital[i]);
            sorted_list_capital.insert(project);
        }
        sorted_list_capital.sort();

        // A maximum heap is used to represent all the projects that can 
        // be completed with the current amount of capital.
        MaxHeap_Profit profit_heap;

        while (k > 0)
        {
            // Retrieve list of projects that can be completed with current capital.
            // cout << "a: " << sorted_list_capital.size() << " " << profit_heap.size() << " " << sorted_list_capital.minimum().capital << " " << w << "\n";
            while (sorted_list_capital.size() && sorted_list_capital.minimum().capital <= w)
            {
                profit_heap.insert(sorted_list_capital.extract_minimum());
            }

            // cout << "b: " << sorted_list_capital.size() << " " << profit_heap.size() << "\n";

            // Retrieve project to complete that gives the greatest profit
            if (profit_heap.size() == 0)
            {
                break;
            }
            else
            {
                w += profit_heap.extract_maximum().profit;
                --k;
            }
        }

        return w;
    }
};
93612