<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>

<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>

<ul>
	<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
	<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
	<ul>
		<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
		<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
	</ul>
	</li>
	<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
	<li>A worker can only be chosen once.</li>
</ul>

<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>

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

<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>

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

<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>

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

<ul>
	<li><code>1 &lt;= costs.length &lt;= 10<sup>5 </sup></code></li>
	<li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k, candidates &lt;= costs.length</code></li>
</ul>

class MinHeap:
    def __init__(self, lst : list) -> None:
        self.size = len(lst)

    def get_size(self) -> int:
        return self.size

    def size_inc(self) -> int:
        self.size += 1

    def size_dec(self) -> int:
        if self.size > 0:
            self.size -= 1

    def heapify(self, lst : list) -> None:
        
        def rec(self, index):

            left = index * 2
            right = index * 2 + 1
            
            smallest = index

            if left <= self.get_size() and lst[left - 1][0] < lst[smallest - 1][0]:
                smallest = left

            if right <= self.get_size() and lst[right - 1][0] < lst[smallest - 1][0]:
                smallest = right

            if smallest != index:
                lst[index - 1], lst[smallest - 1] = lst[smallest - 1], lst[index - 1]
                rec(self, smallest)


        for index in range(self.get_size() // 2, 0, -1):
            rec(self, index)

    def pop(self, lst : list) -> int:

        if self.get_size() == 0:
            return None
        
        if self.get_size() == 1:
            self.size_dec()
            return lst[0]

        popped_item = lst[0]
        lst[0] = lst[self.get_size() - 1]
        self.size_dec()
        index = 1

        while True:
            left = index * 2
            right = index * 2 + 1

            smallest = index

            tt = self.get_size()

            if left <= tt:
                if lst[left - 1][0] < lst[smallest - 1][0] or (lst[left - 1][0] == lst[smallest - 1][0] and lst[left - 1][1] == 0 and lst[smallest - 1][1] == 1):
                    smallest = left

            if right <= tt:
                if lst[right - 1][0] < lst[smallest - 1][0] or (lst[right - 1][0] == lst[smallest - 1][0] and lst[right - 1][1] == 0 and lst[smallest - 1][1] == 1):
                    smallest = right

            if smallest != index:
                lst[index - 1], lst[smallest - 1] = lst[smallest - 1], lst[index - 1]
                index = smallest
            else:
                break

        return popped_item


    def push(self, lst : list, item : int) -> None:

        if self.get_size() < len(lst):
            lst[self.get_size()] = item
        else:
            lst.append(item)

        self.size_inc()

        current = self.get_size()

        while current > 1:
            parent = current // 2

            if (lst[current - 1][0] < lst[parent - 1][0]) or (lst[current - 1][0] == lst[parent - 1][0] and lst[current - 1][1] == 0 and lst[parent - 1][1] == 1):
                lst[current - 1], lst[parent - 1] = lst[parent - 1], lst[current - 1]
                current = parent
            else:
                break


    def print(self, lst : list, title : str) -> None:

        def rec(self, index = 1, indent = 0):
        
            if self.get_size() == 0:
                return

            print(" " * indent, lst[index - 1])
            
            left = index * 2
            right = index * 2 + 1
            
            if left <= self.get_size():
                rec(self, left, indent + 4)

            if right <= self.get_size():
                rec(self, right, indent + 4)

        print(f" -------- Prining {title} -----------")
        rec(self)
        print(f" -------- --------------- -----------")

    def peak(self, lst : list) -> int:
        if self.get_size() == 0:
            return None

        return lst[0][0]


from collections import deque

class Solution:
    def totalCost(self, costs: List[int], k: int, candidates: int) -> int:

        if candidates <= len(costs):
            p1 = candidates - 1
            p2 = max(p1 + 1, len(costs) - candidates)
        else:
            p1 = len(cost) - 1
            p2 = -1

        lst = [(x, 0) for x in costs[ : p1 + 1]] + [(x, 1) for x in costs[p2 : ]]
        mh = MinHeap(lst)
        mh.heapify(lst)

        queue = deque(costs[p1 + 1 : p2])

        total_cost = 0
        for ii in range(k):

            # mh.print(lst, "Heap")
            cur_val, cur_side = mh.pop(lst)
            # mh.print(lst, "Heap")

            if queue:
                if cur_side == 0:
                    q_item = queue.popleft()
                else:
                    q_item = queue.pop()

                
                
                mh.push(lst, (q_item, cur_side))

            total_cost += cur_val

            # print(" ************************************ ")

        return total_cost
30000