Editorial for COCI '12 Contest 2 #4 Popust
                Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
        Submitting an official solution before solving the problem yourself is a bannable offence.
What does an optimal solution for a given  look like? There are two possibilities:
meals with the lowest
price and one meal with the lowest
price (out of the remaining meals)
meals with the lowest
price, where one of them is chosen as first (subtracting its
price and replacing it with the
price – obviously, the one with the lowest
)
Let us sort the meals by ascending  prices and solve the problem incrementally for 
. We will keep the current sum of 
 prices for the first 
 meals (sorted by 
). Also, using a structure (such as a C++ STL set) we will keep the remaining, unselected, meals sorted by 
 (to be able to find the price of the first case), and another structure will keep the selected meals, sorted by 
 (to be able to find the price of the second case). The total complexity is 
 if a structure query takes 
 time.
Comments