No.158 A Strange Errand
Problem Statement
A-kun has $A_{1000}$ 1000-yen bills, $A_{100}$ 100-yen coins, and $A_{1}$ 1-yen coins.
If he pays B-san exactly $D_b$ yen, using any types of bills and coins, (he cannot pay more than $D_b$ yen)
he receives $B_{1000}$ 1000-yen bills, $B_{100}$ 100-yen coins, and $B_{1}$ 1-yen coins.
The amount he receives here is always less than the amount he paid.
If he pays C-san exactly $D_c$ yen, using any types of bills and coins, (he cannot pay more than $D_c$ yen)
he receives $C_{1000}$ 1000-yen bills, $C_{100}$ 100-yen coins, and $C_{1}$ 1-yen coins.
The amount he receives here is also always less than the amount he paid.
One act of paying B-san or C-san and receiving money counts as one errand.
A-kun gets praised if he runs many errands.
A-kun wants to run as many errands as possible by carefully choosing the order in which he visits B-san and C-san and how he pays.
What is the maximum number of errands A-kun can run?
Input
$A_{1000}$ $A_{100}$ $A_{1}$
$D_b$
$B_{1000}$ $B_{100}$ $B_{1}$
$D_c$
$C_{1000}$ $C_{100}$ $C_{1}$
A-kun's initial money is at most $10000$ yen.
Therefore, $0 \le A_{1000}\times 1000+A_{100}\times 100+A_{1} \le 10000$. $A_{1000}$,$A_{100}$,$A_{1}$ are all integers greater than or equal to $0$.
$1 \le D_b,D_c \le 10000$. $D_b$,$D_c$ are positive integers.
$B_{1000}\times 1000+B_{100}\times 100+B_{1} < D_b$. $B_{1000}$,$B_{100}$,$B_{1}$ are all integers greater than or equal to $0$.
$C_{1000}\times 1000+C_{100}\times 100+C_{1} < D_c$. $C_{1000}$,$C_{100}$,$C_{1}$ are all integers greater than or equal to $0$.
Output
Output the maximum number of errands A-kun can run on one line.
Do not forget the newline at the end of your answer.
Sample
Sample 1
Input
0 1 12 110 0 1 2 6 0 0 1
Output
2
A-kun initially has 0 1000-yen bills, 1 100-yen coin, and 12 1-yen coins.
For example, if he goes to B-san, B-san exchanges 110 yen for 1 100-yen coin and 2 1-yen coins.
After going to B-san, he is left with 1 100-yen coin and 4 1-yen coins.
He cannot run any more errands.
For example, if he goes to C-san, C-san exchanges 6 yen for 1 1-yen coin.
He can run errands to C-san 2 times.
After going to C-san 2 times, he is left with 1 100-yen coin and 2 1-yen coins.
He cannot run any more errands.
From the above, by going to C-san 2 times, he can run at most 2 errands.
Sample 2
Input
10 0 0 900 0 5 300 1001 1 0 0
Output
0
A-kun initially has 10 1000-yen bills, 0 100-yen coins, and 0 1-yen coins.
If he goes to B-san, B-san exchanges 900 yen for 5 100-yen coins and 300 1-yen coins.
If he goes to C-san, C-san exchanges 1001 yen for 1 1000-yen bill.
However, since A-kun has only 1000-yen bills, he cannot pay exactly 900 yen or exactly 1001 yen.
Therefore, he cannot run even one errand.
Sample 3
Input
0 1 0 100 0 0 12 3 0 0 1
Output
6
A-kun initially has 0 1000-yen bills, 1 100-yen coin, and 0 1-yen coins.
First, he goes to B-san and exchanges 100 yen for 12 1-yen coins.
Next, he runs the errand of exchanging 3 yen for a 1-yen coin at C-san's place 5 times.
In total, he can run 6 errands.
Sample 4
Input
2 30 2572 5088 3 11 403 8447 1 1 3378
Output
5
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
nmnmnmnmnmnmnm