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.
''' # FASTIO BEGINS ------------------------>
import atexit,io,os,sys
ss=io.BytesIO()
_write=ss.write
ss.write=lambda sett:_write(sett.encode())
atexit.register(lambda:os.write(1,ss.getvalue()))
y_in=open(0).read().split("\n")
def y_inf():
    for y_id in range(len(y_in)):
        yield y_id
y_ino=y_inf()
input=lambda:y_in[next(y_ino)]
# print=ss.write
# FASTIO ENDS <---------------------------- '''

''' NOTES BEGIN ------------------------------>

sys.setrecursionlimit(1000)

def binarySearch(arr, key):   # search lower bound(key)
    beg, end = 0, len(arr) - 1
    found = False
    ind = -1
    while beg <= end:
        mid = beg + (end - beg) // 2    # to avoid mid overflow
        if key == arr[mid]:             # condition
            found = True
            ind = mid
            # end = mid - 1     # lower bound
            break                  # traditional BS
        elif key < arr[mid]:
            end = mid - 1
        else:
            beg = mid + 1
    if not found:     # insertion point = beg (beg < end == True)
        return -1 
    else:
        return ind

def checkprime(n):
    if(n > 1):
        for j in range(2, int(math.sqrt(n)) + 1):
            if (n % j == 0):
                return 0
        return 1
    else: return 0

def primesieve(n):
    array=[1]*n
    for j in range(4,n,2): array[j]=0
    for j in range(2,n):
        for j in range(j*j,n,j):
            array[j]=0
    return array

def arrayXOR(n):    # XOR of range [1, n]
    if n % 4 == 0:
        return n
    elif n % 4 == 1:
        return 1
    elif n % 4 == 2:
        return n + 1
    else:
        return 0

def prod(a, mod=10 ** 9 + 7):
    ans = 1
    for each in a:
        ans = (ans * each) % mod
    return ans


def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


def lcm(a, b):
    return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

class Multiset:
    def __init__(self):
        self.items = []
    def add(self, val):
        #adds one occurrence of val from the multiset, if any
        return self.items.append(val)
        pass
    def remove(self, val):
        # removes one occurrence of val from the multiset, if any
        if self.items.count(val) != 0:
            return self.items.remove(val)
        pass
    def __contains__(self, val):
        # returns True when val is in the multiset, else returns False
        return val in self.items
    def __len__(self):
        # returns the number of elements in the multiset
        return len(self.items)

bisect.bisect_left(arr, num, lo=0, hi=len(arr)) == std::lower_bound(start_ptr, end_ptr, num)
bisect.bisect_right(arr, num, lo=0, hi=len(arr)) == std::upper_bound(start_ptr, end_ptr, num)

'''
#       <--------------------------------------

'''     // -- Template by A_*_A -- //       '''

import sys, collections
inum = lambda: int(sys.stdin.readline())
imap = lambda: map(int, sys.stdin.readline().split())
itup = lambda: tuple(map(int, sys.stdin.readline().split()))
ilis = lambda: [int(zz) for zz in sys.stdin.readline().split()]

tcs = 1 if not 0 else int(input())   # False - 1, True - n
for tc in range(1, tcs + 1):
    # print("Hello World")
    s = input()
    nb, ns, nc = list(map(int, input().split()))
    pb, ps, pc = list(map(int, input().split()))
    r = int(input())
    def check(b, s, c):
        cost = 0
        # remaining items
        rb = max(0, b - nb)
        rs = max(0, s - ns)
        rc = max(0, c - nc)
        # extra cost of remaining iterms
        cost += rb * pb
        cost += rs * ps
        cost += rc * pc
        return cost <= r
    minb, mins, minc = s.count('B'), s.count('S'), s.count('C')
    def binSearch(minb, mins, minc):
        beg, end = 0, int(1e18)
        ans = 0
        while beg <= end:
            mid = beg + (end - beg) // 2
            # ingredients required for mid number of Hamburgers
            curb, curs, curc = mid * minb, mid * mins, mid * minc
            if check(curb, curs, curc):
                ans = mid
                beg = mid + 1
            else:
                end = mid - 1
        return ans
    res = binSearch(minb, mins, minc)
    print(res)

'''     // -- Template by A_*_A -- //       '''
