Buy exactly items at their first-price or second-price values so that the total cost is minimum.
You are given items. For each item , there are two possible prices: a normal price and a discounted price . You must buy exactly items at the normal price and all remaining items at the discounted price.
Your task is to choose which items to buy at the normal price so that the total amount paid is as small as possible.
Example 1
Input
3 1 10 20 30 5 10 25
Output
45
Explanation
If item 1 is bought at the normal price, the total is . Choosing item 2 or 3 instead gives a larger total.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.