#P1194. 买礼物
买礼物
Description
It is Mingming’s birthday again. Mingming wants to buy kinds of items, and coincidentally, each of these items is priced at yuan.
However, the shop owner says there is a promotion:
If you have bought item , then when you buy item , you only need to pay yuan. Moreover, happens to equal .
Now Mingming wants to know the minimum amount of money he has to spend.
Input Format
The first line contains two integers, .
Then there are lines, each containing numbers. The -th line and -th number is .
We guarantee and .
In particular, if , it means there is no discount between these two items.
Note that may be greater than .
Output Format
Output a single integer, the minimum total amount of money required.
1 1
0
1
3 3
0 2 4
2 0 2
4 2 0
7
Hint
Explanation for Sample .
Buy item first, spending 3 yuan. Then, due to the promotion, buying items and each costs 2 yuan, for a total of 7 yuan.
(When multiple “discounts” apply at the same time, clever Mingming will of course not choose to pay 4 yuan for the remaining one, but choose to pay 2 yuan.)
Constraints
For of the testdata, .
For of the testdata, .
Added one more set of testdata on 2018.7.25.
Translated by ChatGPT 5
京公网安备 11011102002149号