Maximum Total from Optimal Activation Order
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>

<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>

<ul>
	<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
	<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
	<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] &lt;= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>

<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">16</span></p>

<p><strong>Explanation:</strong></p>

<p>One optimal activation order is:</p>

<table>
	<thead>
		<tr>
			<th align="center" style="border: 1px solid black;">Step</th>
			<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
			<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
			<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
			<th align="center" style="border: 1px solid black;">Inactive Elements</th>
			<th align="center" style="border: 1px solid black;">Total</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">5</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
			<td align="center" style="border: 1px solid black;">[1]</td>
			<td align="center" style="border: 1px solid black;">5</td>
		</tr>
		<tr>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">3</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">-</td>
			<td align="center" style="border: 1px solid black;">[1]</td>
			<td align="center" style="border: 1px solid black;">8</td>
		</tr>
		<tr>
			<td align="center" style="border: 1px solid black;">3</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">8</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
			<td align="center" style="border: 1px solid black;">[0, 1]</td>
			<td align="center" style="border: 1px solid black;">16</td>
		</tr>
	</tbody>
</table>

<p>Thus, the maximum possible total is 16.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>

<p>One optimal activation order is:</p>

<table style="border: 1px solid black;">
	<thead>
		<tr>
			<th align="center" style="border: 1px solid black;">Step</th>
			<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
			<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
			<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
			<th align="center" style="border: 1px solid black;">Inactive Elements</th>
			<th align="center" style="border: 1px solid black;">Total</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">6</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
			<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
			<td align="center" style="border: 1px solid black;">6</td>
		</tr>
	</tbody>
</table>

<p>Thus, the maximum possible total is 6.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">12</span></p>

<p><strong>Explanation:</strong></p>

<p>One optimal activation order is:​​​​​​​<strong>​​​​​​​</strong></p>

<table style="border: 1px solid black;">
	<thead>
		<tr>
			<th align="center" style="border: 1px solid black;">Step</th>
			<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
			<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
			<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
			<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
			<th align="center" style="border: 1px solid black;">Inactive Elements</th>
			<th align="center" style="border: 1px solid black;">Total</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">5</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">-</td>
			<td align="center" style="border: 1px solid black;">[ ]</td>
			<td align="center" style="border: 1px solid black;">5</td>
		</tr>
		<tr>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">0</td>
			<td align="center" style="border: 1px solid black;">4</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
			<td align="center" style="border: 1px solid black;">[2]</td>
			<td align="center" style="border: 1px solid black;">9</td>
		</tr>
		<tr>
			<td align="center" style="border: 1px solid black;">3</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">1</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">-</td>
			<td align="center" style="border: 1px solid black;">[2]</td>
			<td align="center" style="border: 1px solid black;">10</td>
		</tr>
		<tr>
			<td align="center" style="border: 1px solid black;">4</td>
			<td align="center" style="border: 1px solid black;">3</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">2</td>
			<td align="center" style="border: 1px solid black;">3</td>
			<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
			<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
			<td align="center" style="border: 1px solid black;">12</td>
		</tr>
	</tbody>
</table>

<p>Thus, the maximum possible total is 12.</p>
</div>

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

<ul>
	<li><code>1 &lt;= n == value.length == limit.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= value[i] &lt;= 10<sup>5</sup></code>​​​​​​​</li>
	<li><code>1 &lt;= limit[i] &lt;= n</code></li>
</ul>
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
func maxTotal(value []int, limit []int) (ans int64) {
	g := make(map[int][]int)
	for i := range value {
		g[limit[i]] = append(g[limit[i]], value[i])
	}
	for lim, vs := range g {
		slices.SortFunc(vs, func(a, b int) int { return b - a })
		for i := 0; i < min(lim, len(vs)); i++ {
			ans += int64(vs[i])
		}
	}
	return
}
