Bake a Cake
After a hard season of farming, your crops are ready to be harvested! To reward yourself for this hard work, you'd like to bake a cake — or rather — as many as you possibly can.
As you harvest each of your fields, you note down the number of items you've harvested from that field.
Given all of these harvested crops and a recipe for a cake, what is the maximum amount of cakes you can bake?
Input
The first line consists of a positive integer
, the number of fields you have to harvest.
The next lines consist of
and
— the name of the crop harvested in the field, and the quantity of that crop harvested in that field, respectively.
The next line consists of a positive integer
, the number of ingredients in the cake recipe.
The next lines consist of
and
— the name of an ingredient and the quantity needed for one recipe, respectively.
Output
Output a single integer, the maximum number of cakes you can make.
Clarifications
- You cannot make half-cakes or any fraction of a cake, only whole cakes.
and
will only consist of lowercase English characters.
- It may not always be possible to make any cakes.
- The recipe will have unique ingredients.
Example
Input 1
5
wheat 10
wheat 25
corn 5
sugar 15
tomato 100
2
wheat 1
sugar 1
Output 1
15
After harvesting all of your fields, you have a total of wheat,
corn,
sugar, and
tomato.
The recipe for a cake consists of wheat and
sugar, meaning you can make a maximum of
cakes. You are limited by your harvest of sugar.
Input 2
4
seed 30
grass 100
worm 10
seed 15
2
grass 20
seed 5
Output 2
5
After harvesting all of your fields, you have a total of seed,
grass and
worm.
The recipe for a cake consists of grass and
seed, meaning you can make a maximum of
cakes. You are limited by your harvest of grass.
Input 3
1
vanilla 6
1
chocolate 7
Output 3
0
Comments