Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
How can you determine the minimum cost if you know the maximum value in the array once all values are made equal?
If <code>cost2 > cost1 * 2</code>, we should just use <code>cost1</code> to change all the values to the maximum one.
Otherwise, it's optimal to choose the smallest two values and use <code>cost2</code> to increase both of them.
Since the maximum value is known, calculate the required increases to equalize all values, instead of naively simulating the operations.
There are not a lot of candidates for the maximum; we can try all of them and choose which uses the minimum number of operations.