Here’s a link to the problem.
The first key observation to make is that instead of using absolute difference when deleting and , we can instead choose either to increment score by then decrement it by , or vice versa. In any optimal configuration we will obviously increment by the larger of the two values and decrement by the other, which reduces to absolute value, but the added flexibility provided by this rephrasing allows for easier analysis.
In fact, using this model when is even allows us to choose any elements to increment by (while decrementing by the other ). How?
First, notice that all valid pairings correspond to an RBS. For example, if we choose to pair and in the array , this corresponds to the RBS (())
. Pairing , , and in the array would correspond to ()(())
. Now, in our rephrasing of the problem, we can choose to assign either or to a pair of brackets ()
. For instance, we can assign either ++--
, +-+-
, -+-+
, or --++
to (())
, but we couldn’t assign -++-
. From this, we see that it is necessary to have +
and -
. But is it sufficient?
Actually, yes! This will be left as an exercise to you, the reader, but the proof should be quite elegant.
This gives us a really simple solution for even : simply subtract the smallest values from the largest values. Odd is just slightly more complicated, since we have to enumerate the single element remaining in the end. This will also be left as an exercise to the reader.