Subtask 1: enumerate every subset of oranges bought by the first person. For each subset of size , directly compute the satisfaction and take the maximum. This takes time and space.
Full solution: if the second person bought everything, the satisfaction would be . Giving orange to the first person changes it by .
Let . We must choose exactly differences with maximum sum. Sort them in descending order and select the largest . If an unselected value were larger than a selected value, exchanging them would increase the sum. Thus this choice is optimal. Ties may be resolved arbitrarily.
Writing the sorted values as , the answer is
Exactly oranges must be selected, even when some selected differences are negative.
Each case takes time and space. The magnitude of the answer and intermediate sums is at most , and each difference has magnitude at most . Store sums in 64-bit integers.
Solution written by GPT