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 random
from math import ceil,gcd,factorial,sqrt,log,log2,pi
import queue
import heapq as hq
# from functools import *
# product is used to generater truth tables or multiply list produc([0,1],repeat=n)
from itertools import permutations,combinations,accumulate,product
from collections import Counter,deque,defaultdict
from bisect import bisect, bisect_right,insort,bisect_left,insort_left
# from re import L
from sys import stdin,stdout,setrecursionlimit
# setrecursionlimit(10**6)
input = lambda :stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
ii = lambda :int(input())

# setrecursionlimit(10**7)
# bisect=upper_bound,bisect_left=lower_bound
def lcm(x,y):
    return (x//gcd(x,y))*y
def sieve(n):
    prime = [True]*(n+1)
    p = 2
    prime[0]=False
    prime[1]=False
    a=[]
    for p in range(2,int(n**0.5)+1):
        if (prime[p] == True):
            # a.append(p)
            for l1 in range(p * p, n+1, p):
                prime[l1] = False
    return prime
# def segementedsieve(low,high):
#     primes=sieve(int(high**.5))
#     prime = [True] * (high-low + 1)
#     for i in primes:
#         lower = (low//i)*i
#         if lower<low:
#             lower+=i
#         for j in range(lower,high+1,i):
#             if(j!=i):
#                 prime[j-low]=False
#     ans=[]
#     for i in range(low,high+1):
#         if prime[i-low]:
#             ans.extend(i)
#     return ans
# def isprime(n):
#     if(n==2):
#         return True
#     if(n%2==0 or n==1):
#         return False
#     for l1 in range(3,int(n**0.5)+1,2):
#         if(n%l1==0):
#             return False
#     return True
# def binpow(a,b,m):
#     r2=1
#     a=a%m
#     while(b>0):
#         if(b&1):
#             r2=(r2*a)%m
#         a=(a*a)%m
#         b>>=1
#     return r2
# def lcm(a,b):
#     return (a//gcd(a,b))*b
# #gcd(a,b)=ax+by and 
# def gcdExtended(a,b): 
#     if(b==0):  
#         return a,1,0
#     g,x1,y1=gcdExtended(b,a%b)  
#     x1,y1 = y1,x1-(a//b)*y1 
#     return g,x1,y1
# def binseach(a,l,r,key):
#     while(r-l>1):
#         m=l+(r-l)//2
#         if(a[m]<=key):
#             l=m
#         else:
#             r=m
#     if(a[l]==key):
#         return l
#     if(a[r]==key):
#         return r
#     return -1
# def find(n):
#     if(par[n]==n):
#         return n
#     par[n]=find(par[n])
#     return par[n]
# def union(a,b):
#     a1=find(a)
#     b1=find(b)
#     if(a1!=b1):
#         if(size[a1]>=size[b1]):
#             par[b1]=a1
#             size[a1]+=size[b1]
#         else:
#             size[b1]+=size[a1]
#             par[a1]=b1
        
# def dfs(s,p):
#     v[s]=1
#     path.extend(s)
#     for i in a[s]:
#         if(v[i]==0):
#             if(dfs(i)):
#                 return True
#         elif(i!=p):
#             path.extend(i)
#             return True
#     path.pop()
#     return False
# def bfs(n,x,dest):
#     v=[0]*(1+n)
#     # d=[0]*(1+n)
#     # d[x]=0
#     v[x]=1
#     q=deque([x])
#     p=[0]*(1+n)
#     p[x]=-1
#     while(len(q)):
#         z=q.popleft()
#         for s in gr[z]:
#             if(v[s]==0):
#                 v[s]=1
#                 # d[s]=d[z]+1
#                 q.extend(s)
#                 p[s]=z
#     path=[]
#     if(v[n]==0):
#         return -1
#     l1=dest#given
#     while(l1!=-1):
#        path.extend(l1)
#        l1=p[l1]
#     path.reverse()
#     #?print(d)
#     print(v)
#     return path


# def dijkstra(n, s):
#     r=[[0,s]]
#     hq.heapify()
#     d=[10**10]*(n+1)
#     d[s]=0
#     while(len(r)):
#         k=hq.heappop(r)
#         for i in ar[k[1]]:
#             if(k[0]+i[0]<d[i[1]]):
#                 d[i[1]]=k[0]+i[0]
#                 hq.heappush(r,[k[0]+i[0],i[1]])
#     return d
# # #LCA
# # class Solution:
# #     def lca(self,n,a, b):
# #         if(ar[b]==0 or ar[a]==0):
# #             return -1
# #         m=int(log2(n))
# #         lc=[[-1 for j in range(m+1)]for i in range(1+n)]
# #         lvl=HashMap(int)
# #         v=HashMap(int)
# #         def dfs(node,par,l):
# #             lc[node][0]=par
# #             lvl[node]=l
# #             v[node]=1
# #             for i in ar[node]:
# #                 if(v[i]==0):
# #                     dfs(i,node,1+l)
# #         dfs(0,-1,0)
# #         for i in range(1,m+1):
# #             for j in range(1,n+1):
# #                 if(lc[j][i-1]!=-1):
# #                     lc[j][i]=lc[lc[j][i-1]][i-1]
# #         if(lvl[a]>lvl[b]):
# #             a,b=b,a
# #         d=lvl[b]-lvl[a]
# #         while(d>0):
# #             i=int(log2(d))
# #             b=lc[b][i]
# #             d-=(1<<i)
# #         if(a==b):
# #             return a
# #         for i in range(m,-1,-1):
# #             if(lc[a][i]!=-1 and lc[a][i]!=lc[b][i]):
# #                 a=lc[a][i]
# #                 b=lc[b][i]
# #         return lc[a][0]
# # #LCA
# # class Solution:
# #     def lca(self,n,a, b):
# #         if(ar[b]==0 or ar[a]==0):
# #             return -1
# #         m=int(log2(n))
# #         lc=[[-1 for j in range(m+1)]for i in range(1+n)]
# #         lvl=HashMap(int)
# #         v=HashMap(int)
# #         def dfs(node,par,l):
# #             lc[node][0]=par
# #             lvl[node]=l
# #             v[node]=1
# #             for i in ar[node]:
# #                 if(v[i]==0):
# #                     dfs(i,node,1+l)
# #         dfs(0,-1,0)
# #         for i in range(1,m+1):
# #             for j in range(1,n+1):
# #                 if(lc[j][i-1]!=-1):
# #                     lc[j][i]=lc[lc[j][i-1]][i-1]
# #         if(lvl[a]>lvl[b]):
# #             a,b=b,a
# #         d=lvl[b]-lvl[a]
# #         while(d>0):
# #             i=int(log2(d))
# #             b=lc[b][i]
# #             d-=(1<<i)
# #         if(a==b):
# #             return a
# #         for i in range(m,-1,-1):
# #             if(lc[a][i]!=-1 and lc[a][i]!=lc[b][i]):
# #                 a=lc[a][i]
# #                 b=lc[b][i]
# #         return lc[a][0]

# #SEGMENT TREE
# class Node(object):
#     def __init__(self, start, end):
#         self.start = start
#         self.end = end
#         self.total = 0
#         self.left = None
#         self.right = None
# class NumArray(object):
#     def __init__(self, nums):
#         def createTree(nums, l, r):
#             if l > r:
#                 return None
#             if l == r:
#                 n = Node(l, r)
#                 n.total = nums[l]
#                 return n
#             mid = (l + r) // 2
#             root = Node(l, r)
#             root.left = createTree(nums, l, mid)
#             root.right = createTree(nums, mid + 1, r)
#             root.total = root.left.total + root.right.total
#             return root
#         self.root = createTree(nums, 0, len(nums) - 1)
#     def update(self, i, val):
#         def updateVal(root, i, val):
#             if root.start == root.end:
#                 root.total = val
#                 return val
#             mid = (root.start + root.end) // 2
#             if i <= mid:
#                 updateVal(root.left, i, val)
#             else:
#                 updateVal(root.right, i, val)
#             root.total = root.left.total + root.right.total
#             return root.total
#         return updateVal(self.root, i, val)
#     def sumRange(self, i, j):
#         def rangeSum(root, i, j):
#             if root.start == i and root.end == j:
#                 return root.total
#             mid = (root.start + root.end) // 2
#             if j <= mid:
#                 return rangeSum(root.left, i, j)
#             elif i >= mid + 1:
#                 return rangeSum(root.right, i, j)
#             else:
#                 return rangeSum(root.left, i, mid) + rangeSum(
#                     root.right, mid + 1, j
#                 )
#         return rangeSum(self.root, i, j)
#Sort 0th elmnt in decreasing and 1st elment in ascending
# a=[[1,2],[1,3],[2,4],[2,1]]
# a.sort(key=lambda k: (k[0], -k[1]), reverse=True)


class HashMap():
    def __init__(self, arg, strings=False):
        self.strings = strings
        self.magic = random.randrange(10**10)
        if callable(arg):
            # default factory
            self.dict = defaultdict(arg)
        else:
            # construct from list, Counter
            self.dict = defaultdict(int)
            for key in arg:
                self[key] += 1
 
    def __getitem__(self, key):
        return self.dict[key ^ self.magic]
 
    def __setitem__(self, key, val):
        self.dict[key ^ self.magic] = val
 
    def __contains__(self, key):
        return (key ^ self.magic) in self.dict
 
    def remove(self, key):
        del self.dict[key ^ self.magic]
 
    def items(self):
        for key, val in self.dict.items():
            yield (key ^ self.magic, val)
 
    def keys(self):
        for key in self.dict.keys():
            yield key ^ self.magic
 
    def values(self):
        return self.dict.values()
 
    def __iter__(self):
        for key in self.dict:
            yield key ^ self.magic
 
    def __len__(self):
        return len(self.dict)
 
    def __str__(self):
        s = '{'
        for key, val in self.items():
            s += f'{key}:{val}, '
        s += '}'
        return s
# for _ in range(int(input())):
s=input().strip()
c=Counter(s)
nb,ns,nc=mi()
pb,ps,pc=mi()
a=[[nb,pb,c["B"]],[ns,ps,c["S"]],[nc,pc,c["C"]]]
k=ii()
l=1
r=10**13
def calc(m,k):
    # print(m,k)
    for i in a:
        m1=m
        if(i[2]!=0):
            t=i[0]//i[2]
            rem=i[0]%i[2]
            m1-=t
            l=i[2]*i[1]*m1-rem*i[1]
            if(l>k):
                return False
            k-=l
    return True
while(l<=r):
    m=(l+r)//2
    if(calc(m,k)):
        l=m+1
    else:
        r=m-1
print(r)
