<p>There are <code>3n</code> piles of coins of varying size, you and your friends will take piles of coins as follows:</p>

<ul>
	<li>In each step, you will choose <strong>any </strong><code>3</code> piles of coins (not necessarily consecutive).</li>
	<li>Of your choice, Alice will pick the pile with the maximum number of coins.</li>
	<li>You will pick the next pile with the maximum number of coins.</li>
	<li>Your friend Bob will pick the last pile.</li>
	<li>Repeat until there are no more piles of coins.</li>
</ul>

<p>Given an array of integers <code>piles</code> where <code>piles[i]</code> is the number of coins in the <code>i<sup>th</sup></code> pile.</p>

<p>Return the maximum number of coins that you can have.</p>

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

<pre>
<strong>Input:</strong> piles = [2,4,1,2,7,8]
<strong>Output:</strong> 9
<strong>Explanation: </strong>Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with <strong>7</strong> coins and Bob the last one.
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with <strong>2</strong> coins and Bob the last one.
The maximum number of coins which you can have are: 7 + 2 = 9.
On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2, <strong>4</strong>, 7) you only get 2 + 4 = 6 coins which is not optimal.
</pre>

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

<pre>
<strong>Input:</strong> piles = [2,4,5]
<strong>Output:</strong> 4
</pre>

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

<pre>
<strong>Input:</strong> piles = [9,8,7,6,5,1,2,3,4]
<strong>Output:</strong> 18
</pre>

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

<ul>
	<li><code>3 &lt;= piles.length &lt;= 10<sup>5</sup></code></li>
	<li><code>piles.length % 3 == 0</code></li>
	<li><code>1 &lt;= piles[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def maxCoins(self, piles: List[int]) -> int:
        # Sorting Algorithms (_ Sort)
        # piles = [9,8,7,6,5,1,2,3,4]       Output: 18
        # [1,2,3,4,5,6,7,8,9]
        # 9 8 1    8
        # [2,3,4,5,6,7]
        # 7 6 2    6
        # [3,4,5]
        # 5 4 3    4

        # Sort using any 1 sorting algoritm - Remove last 2 biggest numbers and 1 smallest number from arr - Add 2nd biggest number in 'ans' - At last return 'ans'
        # Heap Sort

        def heapify(piles, n, i):
            largest = i
            l = 2 * i + 1
            r = 2 * i + 2

            if l < n and piles[l] > piles[largest]:
                largest = l
            
            if r < n and piles[r] > piles[largest]:
                largest = r
            
            if largest != i:
                piles[i], piles[largest] = piles[largest], piles[i]
                heapify(piles, n, largest)
        

        def heap_sort(n):
            for i in range(n//2-1, -1, -1):
                heapify(piles, n, i)
            
            for i in range(n-1, 0, -1):
                piles[i], piles[0] = piles[0], piles[i]
                heapify(piles, i, 0)
        

        ans = 0
        n = len(piles)

        heap_sort(n)

        m = int(n / 3)

        for i in range(m):
            piles.pop()
            ans += piles.pop()
            piles.pop(0)
        

        return ans
26600