Buying Potions
InterviewTime limit3sMemory limit1024 MB
Given N potion prices and the discounts each purchase triggers on other potions, find the minimum total cost to buy all of them in some order.
- Level
Medium4 of 10
- Topics
- Bit manipulation, Dynamic programming
- Solved
- No attempts yet
Problem
Jun-gyeom is an adventurer. Before setting out on an adventure, he has to prepare thoroughly.
Before leaving, Jun-gyeom wants to buy all kinds of potions. At the potion shop, he finds that each potion is numbered from 1 to . The shop is running a special event today: buying a certain potion gives discounts on some other potions.
The original price of the -th potion is coins. When you buy the -th potion, the prices of other potions go down.
Discounts stack. For example, suppose buying potion 1 takes 1 coin off potion 3, and buying potion 2 takes 2 coins off potion 3. If you buy both, then buying potion 3 afterward gives a discount of 3 coins. A price never drops below 0. For example, if a potion originally costs 5 coins and is discounted by more than 4 coins, its price becomes 1 coin.
Jun-gyeom was excited to start buying, but then he realized that the order of purchases matters. Find the minimum total cost of buying all the potions.
Input
The first line contains , the number of potion kinds.
The second line contains the prices separated by spaces ().
The next lines give the discount information for each potion, for from 1 to ().
Each block starts with . The next lines each contain a potion number and a discount amount . This means that after you buy potion , potion is discounted by coins.
Output
Print the minimum number of coins needed to buy all the potions, on the first line.
Constraints
- For each , all are distinct and