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.
import sys, math
input = sys.stdin.readline

def getInts():
    return [int(s) for s in input().split()]

def getInt():
    return int(input())

def getStrs():
    return [s for s in input().split()]

def getStr():
    return input().strip()

def listStr():
    return list(input().strip())

import collections as col
import math

def solve2(ingreds,full_burgers,one_cost,R):
    #the case where they're all zero
    if ingreds[2][0] == 0:
        #cost to reach next integer is
        I0, I1, I2 = ingreds
        R -= (I0[2]-I0[1])*I0[3] + (I1[2]-I1[1])*I1[3] + (I2[2]-I2[1])*I2[3]
        if R < 0:
            return full_burgers
        full_burgers += R//one_cost
        return full_burgers
    
    #the case where the first two are 0
    if ingreds[1][0] == 0:
        I0, I1, I2 = ingreds[0], ingreds[1], ingreds[2]
        R -= (I0[2]-I0[1])*I0[3] + (I1[2]-I1[1])*I1[3]
        if R < 0:
            return full_burgers
        full_burgers += 1
        ingreds[0], ingreds[1], ingreds[2] = [0,0,I0[2],I0[3]], [0,0,I1[2],I1[3]], [I2[0]-1,I2[1]-I2[2],I2[2],I2[3]]
        I0, I1, I2 = ingreds[0], ingreds[1], ingreds[2]
        #target is first to reach ingreds[2][0] burgers
        cost_R = I2[0]*(I1[2]*I1[3]+I0[2]*I0[3])
        if R >= cost_R:
            R -= cost_R
            full_burgers += I2[0]
        else:
            full_burgers += R//(I1[2]*I1[3]+I0[2]*I0[3])
            return full_burgers
        ingreds = [[0,0,I0[2],I0[3]], [0,0,I1[2],I1[3]], [0,I2[1]%I2[2],I2[2],I2[3]]]
        return solve2(ingreds,full_burgers,one_cost,R)
    
    #the case where ingreds[1][0] > 0
    I0, I1, I2 = ingreds[0], ingreds[1], ingreds[2]
    cost = (I0[2]-I0[1])*I0[3]
    if cost > R:
        return full_burgers
    R -= cost
    full_burgers += 1
    ingreds[0], ingreds[1], ingreds[2] = [0,0,I0[2],I0[3]], [I1[0]-1,I1[1]-I1[2],I1[2],I1[3]], [I2[0]-1,I2[1]-I2[2],I2[2],I2[3]]
    print(full_burgers,ingreds,R)
    I0, I1, I2 = ingreds[0], ingreds[1], ingreds[2]
    cost = I1[0]*I0[2]*I0[3]
    if R >= cost:
        R -= cost
        full_burgers += I1[0]
    else:
        full_burgers += R//(I0[2]*I0[3])
        return full_burgers
    ingreds[0], ingreds[1], ingreds[2] = [0,0,I0[2],I0[3]], [0,I1[1]%I1[2],I1[2],I1[3]], [I2[0]-I1[0],I2[1]-I2[2]*I1[0],I2[2],I2[3]]
    return solve2(ingreds,full_burgers,one_cost,R)

def solve3(ingreds,full_burgers,R):
    I0, I1 = ingreds
    #I0[0] = 0 and I1[0] = 0
    if I1[0] == 0:
        cost = (I0[2]-I0[1])*I0[3]+(I1[2]-I1[1])*I1[3]
        if cost > R:
            return full_burgers
        full_burgers +=1
        R -= cost
        ingreds = [[0,0,I0[2],I0[3]],[0,0,I1[2],I1[3]]]
        I0, I1 = ingreds
        cost = I0[2]*I0[3]+I1[2]*I1[3]
        return full_burgers + R//cost
    #I0[0] = 0 and I1[0] > 0
    cost = (I0[2]-I0[1])*I0[3]
    if cost > R:
        return full_burgers
    full_burgers += 1
    R -= cost
    ingreds = [[0,0,I0[2],I0[3]],[I1[0]-1,I1[1]-I1[2],I1[2],I1[3]]]
    cost = I0[2]*I0[3]
    if cost*I1[0] > R:
        return full_burgers + R//cost
    full_burgers += I1[0]
    R -= cost*I1[0]
    cost = I0[2]*I0[3] + (I1[2]-I1[1])*I1[3]
    if cost > R:
        return full_burgers
    full_burgers += 1
    R -= cost
    ingreds = [[0,0,I0[2],I0[3]],[0,0,I1[2],I1[3]]]
    return solve3(ingreds,full_burgers,R)

def solve():
    burger = listStr()
    NB, NS, NC = getInts()
    PB, PS, PC = getInts()
    R = getInt()
    B, S, C = burger.count('B'), burger.count('S'), burger.count('C')
    ingreds = [[NB//B if B > 0 else 0,NB,B,PB],[NS//S if S > 0 else 0,NS,S,PS],[NC//C if C > 0 else 0,NC,C,PC]]
    ingreds.sort(key = lambda x: x[2])
    if ingreds[1][2] == 0:
        NB, B, PB = ingreds[2][1:]
        full_burgers = NB//B
        NB %= B
        cost = (B-NB)*PB
        if cost > R:
            return full_burgers
        R -= cost
        return full_burgers + R//(NB*PB)
    if ingreds[0][2] == 0:
        ingreds = ingreds[1:]
        I0, I1 = ingreds
        full_burgers = I0[0]
        ingreds = [[0,I0[1]%I0[2],I0[2],I0[3]],[I1[0]-I0[0],I1[1]-I0[0]*I1[2],I1[2],I1[3]]]
        return solve3(ingreds,full_burgers,R)
    ingreds.sort()
    print(burger,B,S,C)
    assert(B+S+C==len(burger))
    full_burgers = ingreds[0][0]
    NB -= B*full_burgers
    NS -= S*full_burgers
    NC -= C*full_burgers
    one_cost = B*PB + S*PS + C*PC
    #now we have 0 of the minimum ingredient, and some non-negative amount of the other two ingredients

    #if they're all zero, get them all up to 1, then as far as possible
    #if two of them are zero, get those up to 1, then as far as possible towards the third integer, then (if still going) one more, then as far as possible
    #if just one is zero, get that as close to the value of the second, if we reach it, then one more, then as close to the value of the third, then one more,
    #then as far as possible
    print(full_burgers,ingreds,R,one_cost)
    return solve2(ingreds,full_burgers,one_cost,R)
    
    
    
            
        
#for _ in range(getInt()):
print(solve())
    

