Point Card
Time limit2sMemory limit512 MB
Given M cards with A wins out of 2N cells, pay 1 yen per flipped stamp to make at least M-1 cards hold N or more wins; minimize total cost.
Problem
The JOI shopping district runs a point card service. Each point card has cells for stamps. Every time you buy something, you draw a lottery, and one empty cell receives either a "win" stamp or a "miss" stamp depending on the result. A cell can never be stamped more than once.
A point card on which at least of the cells carry a win stamp can be exchanged for one prize. You can also pay 1 yen to change any single stamp on a card into the other kind of stamp.
JOI has point cards, and all cells of each card are filled. Card has win stamps and miss stamps.
JOI wants to get at least prizes. Find the minimum cost needed to do this.
Input
The input consists of lines.
The first line contains two integers and (, ) separated by a space. Each point card has cells, and JOI has point cards.
The -th of the next lines () contains two integers and (, , ). Point card has win stamps and miss stamps.
Output
Print on one line the minimum cost, in yen, that JOI needs to get at least prizes.
Hint
In Example 1, changing 3 miss stamps on card 1 and 1 miss stamp on card 3 into win stamps costs 4 yen and makes cards exchangeable for prizes. This is the minimum cost.
In Example 2, cards can already be exchanged for prizes, so no stamp needs to change.