You are given an integer array and two positive integers and .
A counter starts at . Rearrange the array arbitrarily, then process every element exactly once in the chosen order. When processing a value :
Find the maximum possible final counter. The counter may become negative.
Process the values in the order . The counter changes as , giving a final value of .
The only value is . Since the counter starts at , processing it decreases the counter by , so the final value is .
Process the values in the order . The counter changes as , so all three steps increase it.