Vending Machine
Time limit1sMemory limit128 MB
Given snack prices, stocks, and a budget, choose purchases so that each buy also dispenses one free snack of every cheaper kind still in stock, maximizing total value received.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A snack vending machine sells kinds of snacks, numbered through . A snack of kind has price , and the machine currently holds snacks of that kind.
The machine is broken. When you buy a snack of kind (which is allowed only while at least one snack of kind is still in stock), the machine charges you and then dispenses that snack together with one snack of every kind that still has stock. Kinds among that are already sold out are simply skipped. Every dispensed snack (the one you paid for and each bonus one) reduces that kind's remaining stock by one.
Starting with units of money, you may keep buying snacks in any order, and you do not have to spend all of your money. Determine the maximum possible total value — the sum of the prices of all snacks you receive, both purchased and bonus — that you can collect without your spending exceeding .
Input
The first line contains two integers and — the number of snack kinds and the amount of money available.
The second line contains integers — the prices of the snack kinds.
The third line contains integers — how many snacks of each kind the machine currently holds.
Output
Print a single integer: the maximum total value of snacks obtainable while spending at most units of money.
Note
In the first example, buy a snack of kind (paying ): the machine also releases one snack of kinds and (kind is out of stock and is skipped). Then buy a snack of kind (paying ): it additionally releases one snack of kind . The two purchases cost and yield snacks worth in total.