Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.

Polycarpus has *n*<sub class="lower-index">*b*</sub> pieces of bread, *n*<sub class="lower-index">*s*</sub> pieces of sausage and *n*<sub class="lower-index">*c*</sub> pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are *p*<sub class="lower-index">*b*</sub> rubles for a piece of bread, *p*<sub class="lower-index">*s*</sub> for a piece of sausage and *p*<sub class="lower-index">*c*</sub> for a piece of cheese.

Polycarpus has *r* rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).

The second line contains three integers *n*<sub class="lower-index">*b*</sub>, *n*<sub class="lower-index">*s*</sub>, *n*<sub class="lower-index">*c*</sub> (1 ≤ *n*<sub class="lower-index">*b*</sub>, *n*<sub class="lower-index">*s*</sub>, *n*<sub class="lower-index">*c*</sub> ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers *p*<sub class="lower-index">*b*</sub>, *p*<sub class="lower-index">*s*</sub>, *p*<sub class="lower-index">*c*</sub> (1 ≤ *p*<sub class="lower-index">*b*</sub>, *p*<sub class="lower-index">*s*</sub>, *p*<sub class="lower-index">*c*</sub> ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer *r* (1 ≤ *r* ≤ 10<sup class="upper-index">12</sup>) — the number of rubles Polycarpus has.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
from collections import Counter
from math import inf
recipe = Counter(input())   #demand
state = list(map(int,input().split(' ')))   #B S C
prices = list(map(int,input().split(' ')))
rubles = int(input())
max_result,min_val = 0,inf
if recipe['B'] != 0:
    min_val = min(state[0] // recipe['B'],min_val)
if recipe['S'] != 0:
    min_val = min(state[1] // recipe['S'],min_val)
if recipe['C'] != 0:
    min_val = min(state[2] // recipe['C'],min_val)
state[0] -= min_val * recipe['B']   
state[1] -= min_val * recipe['S']
state[2] -= min_val * recipe['C']
flag,max_result = True,min_val
while rubles > 0:  
    flag = True
    for key in recipe:
        if key == 'B' and state[0] != 0: flag = False
        elif key == 'S' and state[1] != 0: flag = False
        elif key == 'C' and state[2] != 0: flag = False
    if flag == True: 
        break
    if state[0] < recipe['B'] and rubles >= prices[0] and 'B' in recipe:
        rubles -= prices[0] * (recipe['B'] - state[0]) 
        state[0] += (recipe['B'] - state[0])
    if state[1] < recipe['S'] and rubles >= prices[1] and 'S' in recipe:
        rubles -= prices[1] * (recipe['S'] - state[1])
        state[1] += (recipe['S'] - state[1])
    if state[2] < recipe['C'] and rubles >= prices[2] and 'C' in recipe:
        rubles -= prices[2] * (recipe['C'] - state[2])
        state[2] += (recipe['C'] - state[2])
    state[0] -= recipe['B']; state[1] -= recipe['S']; state[2] -= recipe['C']
    max_result += 1

full_h = rubles // ((recipe['B'] * prices[0]) + (recipe['S'] * prices[1]) + (recipe['C'] * prices[2]))
rubles -= ((recipe['B'] * prices[0]) + (recipe['S'] * prices[1]) + (recipe['C'] * prices[2])) * full_h
max_result += full_h
print(max_result)


'''
3
0 2 3
1 2 3 -> 0 1 2 : 4
1 1 2 -> 0 0 2 : 5
1 1 2 -> 0 0 1 : 6
1 1 1 -> 0 0 0 : 7

czyli najpierw opłaca się przejść i dokupić te do których nam zostały
a potem dopiero jak skończą się składniki to kupujemy pełne przepisy.
Dlaczego tak się dzieje: traktuj to jakbyś za niepełne ceny (tylko dokupując składniki do tego co masz)
mógł kupić pełne hamburgery. Więc chcemy ich w ten sposób kupić jak najwięcej.
Jak kupisz od razu pełne przepisy to stracisz te możliwości.
BSC
3 5 6
7 3 9
100
ans: 10

Najpierw zrob z tego co masz a potem dokup,
1.Current_recipe - [bread,cheese,sausage]
2.State - {bread:x , cheese:y, sasuage:z}
Przejdź po obecnym stanie i sprawdź ile hamburgerów możesz zrobić z tego co masz,
będzie to var = min(składnik[i] // zapotrzebowanie w Current_recipe[skladnik])
(możesz zrobić tyle hamburgerów na ile pozwoli ci składnik którego masz najmniej)
Po ustaleniu zmiennej, przechodzisz po skladnikach i odejmujesz var * zapotrzebowanie[skladnik]
od kazdego skladnika. Teraz musisz dokupic.
3.Greedy - póki możesz coś kupić 
iterujesz po tych skladnikach ktore masz i po kolei sprawdzasz czy jakiegoś brakuje
do zrobienia jednego hamburgera, zawsze dokupujesz do jednego! Jeżeli nie masz
pieniędzy koniec algorytmu.

O(1) rozwiązanie:
ze wszystkich rubli ktore posiadasz, kup wszystkie pelne gotowe przepisy ktore
bys mógł (Sum(recipe)). + mozliwe ten jeden ktory mozesz dopelnic.

'''
