Editorial for COCI '09 Contest 1 #4 Mali
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's try to solve the following subproblem: Given two sequences and , what is the optimal pairing for these two sequences? Let be the largest number in sequence (if more than one exists, choose any one), and be the smallest number in sequence . Choose some pairing which contains the pair . Is this optimal? There are two cases we need to address:
is the largest sum in the chosen pairing. If this is the case, it is quite obvious that no other number from can reduce this sum. The best you can do is select another instance of , if more than one is present, and achieve the exact same sum. So, if is the largest sum in the chosen pairing, there is no way to improve that.
is not the largest sum in the chosen pairing. Let denote the pair with the largest sum. Can we improve this by breaking the pair? We could try creating pairs and . It is clear that now the sum is equal or smaller than . However, is now larger or equal than because is larger than or equal to . This has now created a new largest sum, larger than or equal to the previous one.
So we conclude that it is not possible to improve the solution by not using . This gives us a good starting point for a greedy approach.
Comments