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_{b} pieces of bread, n_{s} pieces of sausage and n_{c} pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are p_{b} rubles for a piece of bread, p_{s} for a piece of sausage and p_{c} 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.


-----Input-----

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_{b}, n_{s}, n_{c} (1 ≤ n_{b}, n_{s}, n_{c} ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers p_{b}, p_{s}, p_{c} (1 ≤ p_{b}, p_{s}, p_{c} ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 10^12) — 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.


-----Output-----

Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.


-----Examples-----
Input
BBBSSC
6 4 1
1 2 3
4

Output
2

Input
BBC
1 10 1
1 10 1
21

Output
7

Input
BSC
1 1 1
1 1 3
1000000000000

Output
200000000001

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Complete search


Solutions:
from collections import Counter

def solve(s, n, p, money):

	def get_price(count):
		return sum((max(r[i] * count - n[i], 0) * p[i] for i in range(3)))
	c = Counter(s)
	r = [c['B'], c['S'], c['C']]
	x = (money + n[0] * p[0] + n[1] * p[1] + n[2] * p[2]) // (r[0] * p[0] + r[1] * p[1] + r[2] * p[2])
	(li, ro) = (0, 10 ** 12 + 100)
	feasible_count = 0
	while li <= ro:
		mi = (li + ro) // 2
		if get_price(mi) > money:
			ro = mi - 1
		else:
			feasible_count = mi
			li = mi + 1
	return feasible_count
s = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
r = int(input())
print(solve(s, n, p, r))


from sys import stdin, stdout, setrecursionlimit
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd, sqrt, factorial, log2, floor
from bisect import bisect_right as br, bisect_left as bl
import heapq

def money_for_x(na, nb, nc, pa, pb, pc, cs, cb, cc, i):
	return max(0, cb * i - na) * pa + max(0, cs * i - nb) * pb + max(0, cc * i - nc) * pc
s = list(input())
(a, b, c) = mp()
(pa, pb, pc) = mp()
p = it()
d = C(s)
i = 0
j = 10 ** 13
ind = 0
while i <= j:
	mid = (i + j) // 2
	tt = money_for_x(a, b, c, pa, pb, pc, d['S'], d['B'], d['C'], mid)
	if tt <= p:
		i = mid + 1
		ind = mid
	else:
		j = mid - 1
print(ind)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
c = C(data())
(nb, ns, nc) = sp()
(pb, ps, pc) = sp()
r = int(data())
answer = 0
(low, high) = (0, int(1e+18))
while low <= high:
	mid = low + high >> 1
	(bc, sc, cc) = (pb * max(0, c['B'] * mid - nb), ps * max(0, c['S'] * mid - ns), pc * max(0, c['C'] * mid - nc))
	if bc + sc + cc <= r:
		answer = max(answer, mid)
		low = mid + 1
	else:
		high = mid - 1
out(answer)


import time, math
from sys import stdin, stdout

def I():
	return stdin.readline()

def In():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def Si():
	return sorted(list(map(int, stdin.readline().split())))

def Si():
	return sorted(list(map(int, stdin.readline().split())), reverse=True)

def P(x):
	return stdout.write(str(x) + '\n')

def Pi(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def solve():
	(b, s, c) = (0, 0, 0)
	for i in input():
		if i == 'B':
			b += 1
		elif i == 'S':
			s += 1
		else:
			c += 1
	(nb, ns, nc) = In()
	(pb, ps, pc) = In()
	p = int(input())
	l = 0
	r = int(10000000000000.0)
	while r - l > 1:
		mid = (l + r) // 2
		x = max(0, (mid * b - nb) * pb) + max(0, (mid * s - ns) * ps) + max(0, (mid * c - nc) * pc)
		if x <= p:
			l = mid
		else:
			r = mid
	P(l)
solve()


from collections import Counter
recept = input().strip()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
t = int(input())
(l, r) = (0, 10 ** 21)
(rb, rs, rc) = (0, 0, 0)
for s in recept:
	if s == 'B':
		rb += 1
	elif s == 'S':
		rs += 1
	else:
		rc += 1

def check(tmp):
	return max(tmp * rb - nb, 0) * pb + max(tmp * rs - ns, 0) * ps + max(tmp * rc - nc, 0) * pc <= t
while l != r - 1:
	tmp = (l + r) // 2
	if check(tmp):
		l = tmp
	else:
		r = tmp
print(l)


k = [0 for x in range(3)]
for i in input():
	if i == 'B':
		k[0] += 1
	elif i == 'S':
		k[1] += 1
	else:
		k[2] += 1
n = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
N = int(input())
left = 0
right = 0
for i in range(3):
	if k[i] != 0:
		if (n[i] + N // p[i]) // k[i] > right:
			right = (n[i] + N // p[i]) // k[i]
right += 1

def is_correct(x):
	N1 = 0
	for i in range(3):
		f = k[i] * x - n[i]
		if f >= 0:
			N1 += (k[i] * x - n[i]) * p[i]
	return N1 <= N
while right - left > 1:
	mid = int((left + right) / 2)
	if is_correct(mid):
		left = mid
	else:
		right = mid
print(left)


import sys
lines = sys.stdin.readlines()
burger = lines[0].strip()
count = [0, 0, 0]
for l in burger:
	if l == 'B':
		count[0] += 1
	elif l == 'S':
		count[1] += 1
	else:
		count[2] += 1
have = list(map(int, lines[1].strip().split(' ')))
price = list(map(int, lines[2].strip().split(' ')))
money = int(lines[3].strip())
l = 0
r = money // min(price) + sum(have) + 2
while l < r - 1:
	mid = l + (r - l) // 2
	need = 0
	for i in range(3):
		need += max(0, mid * count[i] - have[i]) * price[i]
	if need > money:
		r = mid
	else:
		l = mid
print(l)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
n = int(input())
b = 0
ss = 0
c = 0
for i in range(len(s)):
	if s[i] == 'B':
		b += 1
	if s[i] == 'C':
		c += 1
	if s[i] == 'S':
		ss += 1

def check(m):
	B = m * b
	S = m * ss
	C = m * c
	nn = n
	if B > nb:
		nn -= (B - nb) * pb
	if S > ns:
		nn -= (S - ns) * ps
	if C > nc:
		nn -= (C - nc) * pc
	return nn >= 0
l = 0
r = 10 ** 12 + 1000
while r - l > 1:
	m = l + (r - l) // 2
	if check(m):
		l = m
	else:
		r = m
print(l)


ingrs = list(input())
able = list(map(int, input().split()))
prices = list(map(int, input().split()))
money = int(input())
B = ingrs.count('B')
S = ingrs.count('S')
C = ingrs.count('C')

def check(k):
	p = money
	p -= max(k * B - able[0], 0) * prices[0]
	p -= max(k * S - able[1], 0) * prices[1]
	p -= max(k * C - able[2], 0) * prices[2]
	return True if p >= 0 else False
l = 0
r = 10 ** 15
ans = 0
while r >= l:
	mid = (r + l) // 2
	if check(mid):
		l = mid + 1
		ans = mid
	else:
		r = mid - 1
print(ans)


def func(x):
	global d, nb, ns, nc, pb, ps, pc, r
	cal = max(0, d['B'] * x - nb) * pb + max(0, d['S'] * x - ns) * ps + max(0, d['C'] * x - nc) * pc
	if cal <= r:
		return True
	else:
		return False
s = input()
d = {}
d['B'] = 0
d['S'] = 0
d['C'] = 0
for i in s:
	d[i] += 1
(nb, ns, nc) = map(int, input().strip().split(' '))
(pb, ps, pc) = map(int, input().strip().split(' '))
r = int(input())
l = 0
u = max(nb, nc, ns) + r // (d['B'] * pb + d['S'] * ps + d['C'] * pc) + 25
while l < u:
	mid = (l + u) // 2
	x = func(mid)
	y = func(mid + 1)
	if x and y:
		l = mid + 1
	elif x and (not y):
		l = mid
		break
	else:
		u = mid
print(l)


def bin(a, b, c, n, m, k, pn, pm, pk, r):
	[L, R] = [0, (r + n + m + k) * 100]
	while R - L > 1:
		M = (R + L) // 2
		a2 = max(M * a - n, 0)
		b2 = max(M * b - m, 0)
		c2 = max(M * c - k, 0)
		if a2 * pn + b2 * pm + c2 * pk <= r:
			L = M
		else:
			R = M
	return L
str = input()
[a, b, c] = [0, 0, 0]
[n, m, k] = [int(i) for i in input().split()]
for i in str:
	if i == 'B':
		a += 1
	elif i == 'S':
		b += 1
	else:
		c += 1
[pn, pm, pk] = [int(i) for i in input().split()]
r = int(input())
print(bin(a, b, c, n, m, k, pn, pm, pk, r))


recipe = input()
rb = rs = rc = 0
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
r = int(input())
for ch in recipe:
	if ch == 'B':
		rb += 1
	elif ch == 'S':
		rs += 1
	else:
		rc += 1
a = 0
b = 10 ** 12 + 1000

def doable(n):
	return r >= pb * max(0, rb * n - nb) + ps * max(0, rs * n - ns) + pc * max(0, rc * n - nc)
res = 0
while a <= b:
	m = (a + b) // 2
	if doable(m):
		res = m
		a = m + 1
	else:
		b = m - 1
print(res)


a = input()
(b, s, c) = list(map(int, input().split()))
(rb, rs, rc) = list(map(int, input().split()))
total = int(input())
ns = 0
nb = 0
nc = 0
for i in a:
	if i == 'B':
		nb += 1
	if i == 'S':
		ns += 1
	if i == 'C':
		nc += 1

def get(x):
	f = max(0, nb * x - b) * rb + max(0, ns * x - s) * rs + max(0, nc * x - c) * rc
	return f
start = 0
end = 100000000000000
maximum = 0
while start <= end:
	mid = (start + end) // 2
	if get(mid) <= total:
		start = mid + 1
		maximum = max(mid, maximum)
	else:
		end = mid - 1
print(maximum)


import sys
import copy
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
t = 1
for test in range(t):
	S = input()
	b = S.count('B')
	s = S.count('S')
	c = S.count('C')
	(nb, ns, nc) = get_ints()
	(pb, ps, pc) = get_ints()
	r = int(input())

	def good(x):
		price = (x * b - min(x * b, nb)) * pb + (x * s - min(x * s, ns)) * ps + (x * c - min(x * c, nc)) * pc
		if price <= r:
			return True
		else:
			return False
	l = 0
	h = 2 * 10 ** 12
	while l < h:
		mid = (l + h) // 2 + 1
		if good(mid):
			l = mid
		else:
			h = mid - 1
	print(l)


import sys

def hamburguers2(ing, kit, prices, munny):
	ham_ing = {}
	pieces = {'B': kit[0], 'S': kit[1], 'C': kit[2]}
	prices = {'B': prices[0], 'S': prices[1], 'C': prices[2]}
	for char in ing:
		if char not in ham_ing:
			ham_ing[char] = 1
		else:
			ham_ing[char] += 1
	first_hams = []
	for item in ham_ing:
		if ham_ing[item] != 0:
			first_hams.append(pieces[item] / ham_ing[item])
	first = max(first_hams)
	ultimo = {}
	for item in ham_ing:
		if ham_ing[item] != 0:
			if pieces[item] / ham_ing[item] != first:
				ultimo[item] = 1
	if type(first) != int:
		first = int(first) + 1
	cost = sum((prices[item] * (first * ham_ing[item] - pieces[item]) for item in ham_ing))
	while munny - cost < 0:
		first -= 1
		cost = sum((prices[item] * (first * ham_ing[item] - pieces[item]) for item in ultimo))
	munny -= cost
	ham_price = sum((ham_ing[item] * prices[item] for item in ham_ing))
	something = int(munny / ham_price)
	return something + first
line = sys.stdin.readline().strip()
st = str(line)
(b, s, c) = map(int, sys.stdin.readline().strip().split())
ls = [b, s, c]
(d, e, f) = map(int, sys.stdin.readline().strip().split())
lw = [d, e, f]
munny = int(sys.stdin.readline().strip())
print(hamburguers2(st, ls, lw, munny))


def f(tar, l, p, r):
	req = 0
	for i in range(len(l)):
		if p[i][1] == 0:
			pass
		elif l[i] // p[i][1] < tar:
			req += p[i][1] * p[i][0] * tar - p[i][0] * l[i]
	if req <= r:
		return True
	else:
		return False
s = input()
(a, b, c) = map(int, input().split())
(x, y, z) = map(int, input().split())
r = int(input())
l = [a, b, c]
p = [[x, s.count('B')], [y, s.count('S')], [z, s.count('C')]]
(low, high, ans) = (0, 10 ** 18, 0)
while low <= high:
	mid = low + (high - low) // 2
	u = f(mid, l, p, r)
	if u:
		ans = max(ans, mid)
		low = mid + 1
	else:
		high = mid - 1
print(ans)


S = input()
k1 = S.count('B')
k2 = S.count('S')
k3 = S.count('C')
(a, b, c) = map(int, input().split())
(a1, b1, c1) = map(int, input().split())
lo = int(input())
l = 0
r = 10000000000000000
while l < r:
	m = (l + r) // 2
	v = 0
	v += max(0, (m * k1 - a) * a1)
	v += max(0, (m * k2 - b) * b1)
	v += max(0, (m * k3 - c) * c1)
	if v > lo:
		r = m
	else:
		l = m + 1
print(l - 1)


recipe = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
money = int(input())
need_b_for_one_cooking = recipe.count('B')
need_s_for_one_cooking = recipe.count('S')
need_c_for_one_cooking = recipe.count('C')

def CanMake(amount):
	need_buy_b = max(amount * need_b_for_one_cooking - nb, 0)
	need_buy_s = max(amount * need_s_for_one_cooking - ns, 0)
	need_buy_c = max(amount * need_c_for_one_cooking - nc, 0)
	need_money = need_buy_b * pb + need_buy_s * ps + need_buy_c * pc
	return money >= need_money

def FindAnswer():
	(l, r) = (0, int(1e+18))
	while l + 1 < r:
		m = (l + r) // 2
		if CanMake(m):
			l = m
		else:
			r = m
	return l
print(FindAnswer())


from math import *

def main():
	R = lambda : map(int, input().split())
	k = dict(B=0, S=0, C=0)
	for c in input():
		k[c] += 1
	k = [k['B'], k['S'], k['C']]
	(n, p) = (list(R()), list(R()))
	r = int(input())
	ans = 0
	for i in range(3):
		if k[i] == 0:
			n[i] = 0
	while n[0] or n[1] or n[2]:
		t = [k[i] - n[i] if k[i] - n[i] >= 0 else 0 for i in range(3)]
		s = sum([t[i] * p[i] for i in range(3)])
		if s > r:
			print(ans)
			return
		r -= s
		for i in range(3):
			n[i] -= k[i]
			if n[i] < 0:
				n[i] = 0
		ans += 1
	s = sum([k[i] * p[i] for i in range(3)])
	ans += r // s
	print(ans)
main()


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'
mod = 10 ** 9 + 7

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def ispali(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	s = I().strip()
	(nb, ns, nc) = mi()
	(pb, ps, pc) = mi()
	r = ii()
	bread = s.count('B')
	souce = s.count('S')
	chese = s.count('C')

	def possi(burger):
		need_bread = max(0, burger * bread - nb)
		need_souce = max(0, burger * souce - ns)
		need_chese = max(0, burger * chese - nc)
		need_money = need_bread * pb + need_souce * ps + need_chese * pc
		if need_money <= r:
			return True
		return False
	i = 0
	j = 4 * 10 ** 12
	ans = 0
	while i <= j:
		mid = i + (j - i) // 2
		if possi(mid):
			ans = mid
			i = mid + 1
		else:
			j = mid - 1
	print(ans)
main()


from sys import stdin, stdout
from collections import Counter
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, input().split()))
for i in range(1):
	d = Counter(input())
	(bi, si, ci) = lst()
	(cb, cs, cc) = lst()
	m = nmbr()
	l = 1
	r = 10 ** 15 + 1
	while l <= r:
		mid = l + r >> 1
		reqb = max(0, d['B'] * mid - bi)
		reqs = max(0, d['S'] * mid - si)
		reqc = max(0, d['C'] * mid - ci)
		costb = reqb * cb
		costs = reqs * cs
		costc = reqc * cc
		if costb + costs + costc <= m:
			l = mid + 1
		else:
			r = mid - 1
	print(r)


burger = input()
b = burger.count('B')
s = burger.count('S')
c = burger.count('C')
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
money = int(input())

def ok(target):
	costB = max(0, (target * b - nb) * pb)
	costS = max(0, (target * s - ns) * ps)
	costC = max(0, (target * c - nc) * pc)
	return costC + costB + costS <= money
low = 0
high = int(1e+18)
while low < high:
	mid = (high + low + 1) // 2
	if ok(mid):
		low = mid
	else:
		high = mid - 1
print(high)


ham = input()
(nb, ns, nc) = [int(x) for x in input().split()]
(pb, ps, pc) = [int(x) for x in input().split()]
rubles = int(input())
b = c = s = 0
for ch in ham:
	if ch == 'B':
		b += 1
	elif ch == 'S':
		s += 1
	else:
		c += 1

def binary(mid, rubles, pb, ps, pc, nb, nc, ns, b, c, s):
	if b > 0 and mid * b >= nb:
		rubles -= (mid * b - nb) * pb
	if c > 0 and mid * c >= nc:
		rubles -= (mid * c - nc) * pc
	if s > 0 and mid * s >= ns:
		rubles -= (mid * s - ns) * ps
	if rubles < 0:
		return False
	return True
ans = 0
(start, end) = (0, rubles + 202)
while start <= end:
	mid = start + (end - start) // 2
	if binary(mid, rubles, pb, ps, pc, nb, nc, ns, b, c, s) == True:
		ans = mid
		start = mid + 1
	else:
		end = mid - 1
print(ans)


def check(x):
	bread_cost = max(0, recipe[0] * x - has_b) * price_b
	sausage_cost = max(0, recipe[1] * x - has_s) * price_s
	cheese_cost = max(0, recipe[2] * x - has_c) * price_c
	cost = bread_cost + sausage_cost + cheese_cost
	return cost <= money
s_recipe = input()
(has_b, has_s, has_c) = map(int, input().split())
(price_b, price_s, price_c) = map(int, input().split())
money = int(input())
recipe = (s_recipe.count('B'), s_recipe.count('S'), s_recipe.count('C'))
upper = 2 ** 64
lower = 0
while lower < upper:
	guess = (lower + upper) // 2
	if not check(guess):
		upper = guess
	else:
		lower = guess + 1
print(lower - 1)


h = input()
h_c = [h.count('B'), h.count('S'), h.count('C')]
al = [*map(int, input().split())]
pr = [*map(int, input().split())]
su = int(input())
l = 0
r = 10 ** 16
while l < r:
	v = 0
	m = (l + r) // 2
	v += max(0, (m * h_c[0] - al[0]) * pr[0])
	v += max(0, (m * h_c[1] - al[1]) * pr[1])
	v += max(0, (m * h_c[2] - al[2]) * pr[2])
	if v > su:
		r = m
	else:
		l = m + 1
print(l - 1)


hamberger = input().strip()
recipe = {}
for c in hamberger:
	recipe[c] = recipe.get(c, 0) + 1
stock = list(map(int, input().strip().split()))
prices = list(map(int, input().strip().split()))
r = int(input().strip())
(lp, rp) = (0, r + sum(stock))

def countPrice(recipe, curStock, prices, num):
	price = 0
	if recipe.get('B') and recipe.get('B') * num > curStock[0]:
		price += (recipe.get('B') * num - curStock[0]) * prices[0]
	if recipe.get('S') and recipe.get('S') * num > curStock[1]:
		price += (recipe.get('S') * num - curStock[1]) * prices[1]
	if recipe.get('C') and recipe.get('C') * num > curStock[2]:
		price += (recipe.get('C') * num - curStock[2]) * prices[2]
	return price
while lp <= rp:
	mid = (lp + rp) // 2
	price = countPrice(recipe, stock, prices, mid)
	if r == price:
		break
	elif r > price:
		lp = mid + 1
	else:
		rp = mid - 1
print(lp - 1 if r != price else mid)


def ok(burgers):
	cost = 0
	(tb, ts, tc) = (max(0, cb * burgers - b), max(0, cs * burgers - s), max(0, cc * burgers - c))
	cost = tb * pb + ts * ps + tc * pc
	if cost <= price:
		return True
	return False
string = str(input())
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
price = int(input())
count = 0
(cb, cs, cc) = (0, 0, 0)
for i in string:
	if i == 'B':
		cb += 1
	elif i == 'S':
		cs += 1
	else:
		cc += 1
ans = 0
(low, high) = (0, price + b + s + c)
while low <= high:
	mid = low + (high - low) // 2
	if ok(mid) == True:
		ans = mid
		low = mid + 1
	else:
		high = mid - 1
print(ans)


def f(x):
	global nb, ns, nc, pb, pc, pb, ps, cb, cs, cc
	return max(0, x * cb - nb) * pb + max(0, x * cs - ns) * ps + max(0, x * cc - nc) * pc
s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
rub = int(input())
(cb, cs, cc) = (s.count('B'), s.count('S'), s.count('C'))
l = 0
r = 10 ** 100
while r - l > 1:
	m = (l + r) // 2
	if f(m) < rub:
		l = m
	elif f(m) > rub:
		r = m
	else:
		ans = m
		break
else:
	m = l
print(m)


recipe = input()
(rb, rs, rc) = (recipe.count('B'), recipe.count('S'), recipe.count('C'))
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
x = int(input())

def check(mi):
	(reqb, reqs, reqc) = (rb * mi, rs * mi, rc * mi)
	reqb -= nb
	reqc -= nc
	reqs -= ns
	return max(reqb, 0) * pb + max(reqs, 0) * ps + max(reqc, 0) * pc <= x
lo = 0
hi = 10 ** 20
while lo <= hi:
	mi = lo + hi >> 1
	if check(mi):
		ans = mi
		lo = mi + 1
	else:
		hi = mi - 1
print(ans)


recipe = input()
(n_b, n_s, n_c) = list(map(int, input().split()))
(p_b, p_s, p_c) = list(map(int, input().split()))
money = int(input())
(c_b, c_s, c_c) = (recipe.count('B'), recipe.count('S'), recipe.count('C'))

def calc_cost(count):
	return max(0, count * c_b - n_b) * p_b + max(0, count * c_s - n_s) * p_s + max(0, count * c_c - n_c) * p_c

def find_answer():
	(l, r) = (0, 10 ** 13)
	while l + 1 < r:
		mid = (l + r) // 2
		mid_val = calc_cost(mid)
		if mid_val > money:
			r = mid
		else:
			l = mid
	return l
print(find_answer())


t = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
r = int(input())
q = [t.count('B'), t.count('S')]
q.append(len(t) - q[0] - q[1])
n = [n[i] for i in range(3) if q[i]]
p = [p[i] for i in range(3) if q[i]]
q = [q[i] for i in range(3) if q[i]]
m = len(q)
a = min((n[i] // q[i] for i in range(m)))
b = max(((n[i] + r // p[i]) // q[i] for i in range(m))) + 1
while b - a > 1:
	c = (a + b) // 2
	if sum((max(0, (c * q[i] - n[i]) * p[i]) for i in range(m))) > r:
		b = c
	else:
		a = c
print(a)


R = lambda : map(int, input().split())
str = input()
(bs, ss, cs) = (str.count('B'), str.count('S'), str.count('C'))
(nb, ns, nc) = R()
(pb, ps, pc) = R()
money = int(input())
(l, r) = (0, money + max(nb, ns, nc))
while l < r:
	m = (l + r + 1) // 2
	(mb, ms, mc) = (max(0, bs * m - nb) * pb, max(0, ss * m - ns) * ps, max(0, cs * m - nc) * pc)
	if mb + ms + mc <= money:
		l = m
	else:
		r = m - 1
print(l)


import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))
from collections import Counter

def can_cook(n, recipe, n_b, n_s, n_c, p_b, p_s, p_c, r):
	(b, s, c) = (n * recipe.get('B', 0), n * recipe.get('S', 0), n * recipe.get('C', 0))
	(diff_b, diff_s, diff_c) = (n_b - b, n_s - s, n_c - c)
	if diff_b < 0:
		r -= -diff_b * p_b
	if diff_s < 0:
		r -= -diff_s * p_s
	if diff_c < 0:
		r -= -diff_c * p_c
	return r >= 0

def solve():
	recipe = dict(Counter(input().replace('\n', '')))
	(n_b, n_s, n_c) = read_tuple(int)
	(p_b, p_s, p_c) = read_tuple(int)
	r = int(input())
	(lb, ub) = (0, 10 ** 20)
	while ub - lb > 1:
		mid = (lb + ub) // 2
		if can_cook(mid, recipe, n_b, n_s, n_c, p_b, p_s, p_c, r):
			lb = mid
		else:
			ub = mid
	if can_cook(ub, recipe, n_b, n_s, n_c, p_b, p_s, p_c, r):
		print(ub)
	else:
		print(lb)
solve()


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
s = input()
cb = cs = cc = 0
for i in s:
	if i == 'B':
		cb += 1
	elif i == 'S':
		cs += 1
	else:
		cc += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
n = int(input())
f = 0
r = 2000000000000
while f < r:
	m = (f + r) // 2
	pr = pb * max(0, cb * m - nb) + ps * max(0, cs * m - ns) + pc * max(0, cc * m - nc)
	if n >= pr:
		f = m + 1
	else:
		r = m
print(f - 1)


def boleh(mid, recipe_B, recipe_C, recipe_S):
	need_B = max(recipe_B * mid - have_B, 0)
	need_S = max(recipe_S * mid - have_S, 0)
	need_C = max(recipe_C * mid - have_C, 0)
	total = need_B * price_B + need_S * price_S + need_C * price_C
	if rubles >= total:
		return True
	return False
recipe = input()
recipe_B = recipe.count('B')
recipe_S = recipe.count('S')
recipe_C = recipe.count('C')
(have_B, have_S, have_C) = map(int, input().split())
(price_B, price_S, price_C) = map(int, input().split())
rubles = int(input())
(left, right) = (0, 100000000000000)
while left < right:
	mid = left + (right - left + 1) // 2
	if boleh(mid, recipe_B, recipe_C, recipe_S):
		left = mid
	else:
		right = mid - 1
print(left)


def check(m):
	need = [h[0] * m, h[1] * m, h[2] * m]
	(need[0], need[1], need[2]) = (need[0] - N[0], need[1] - N[1], need[2] - N[2])
	calc = 0
	if need[0] > 0:
		calc += need[0] * P[0]
	if need[1] > 0:
		calc += need[1] * P[1]
	if need[2] > 0:
		calc += need[2] * P[2]
	if calc <= r:
		return 1
	return 0
B = input()
h = [0, 0, 0]
for el in B:
	if el == 'B':
		h[0] += 1
	elif el == 'S':
		h[1] += 1
	else:
		h[2] += 1
N = list(map(int, input().split()))
P = list(map(int, input().split()))
r = int(input())
R = 10 ** 20
L = 0
while R - L > 1:
	m = (R + L) // 2
	if check(m):
		L = m
	else:
		R = m
print(L)


s = input()
mp = [0] * 3
for i in s:
	if i == 'B':
		mp[0] += 1
	elif i == 'S':
		mp[1] += 1
	else:
		mp[2] += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
x = 0
y = 1000000000000000000
ans = 0
l = r
while x <= y:
	mid = (x + y) // 2
	if mid * mp[0] > nb:
		v = (mid * mp[0] - nb) * pb
		if r >= v:
			r -= v
		else:
			y = mid - 1
			continue
	if mid * mp[1] > ns:
		v = (mid * mp[1] - ns) * ps
		if r >= v:
			r -= v
		else:
			y = mid - 1
			r = l
			continue
	if mid * mp[2] > nc:
		v = (mid * mp[2] - nc) * pc
		if r >= v:
			r -= v
		else:
			y = mid - 1
			r = l
			continue
	ans = max(ans, mid)
	r = l
	x = mid + 1
print(ans)


recipe = input()
(nb, ns, nc) = map(int, input().split(' '))
(pb, ps, pc) = map(int, input().split(' '))
rubles = int(input())
(BREAD, SAUSAGE, CHEESE) = (recipe.count('B'), recipe.count('S'), recipe.count('C'))
(left, right) = (0, 10000000000000)
while left < right:
	mid = (left + right + 1) // 2
	bread = max(mid * BREAD - nb, 0)
	sausage = max(mid * SAUSAGE - ns, 0)
	cheese = max(mid * CHEESE - nc, 0)
	if bread * pb + sausage * ps + cheese * pc <= rubles:
		left = mid
	else:
		right = mid - 1
print(right)


F = str.count
I = input
R = lambda : map(int, I().split())
p = I()
B = F(p, 'B')
S = F(p, 'S')
C = F(p, 'C')
(b, s, c) = R()
(q, w, e) = R()
p = int(I())
l = -1
r = 10 ** 20
while l < r - 1:
	m = (l + r) // 2
	if q * max(0, B * m - b) + w * max(0, S * m - s) + e * max(0, C * m - c) <= p:
		l = m
	else:
		r = m
print(l)


recipe = input()
(cb, cs, cc) = [recipe.count(x) for x in 'BSC']
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
n_ready = min(nb // cb if cb != 0 else 100, ns // cs if cs != 0 else 100, nc // cc if cc != 0 else 100)
(nnb, nns, nnc) = (nb - n_ready * cb, ns - n_ready * cs, nc - n_ready * cc)

def get_remain_money(n):
	return r - ((pb * (n * cb - nnb) if n * cb - nnb > 0 else 0) + (ps * (n * cs - nns) if n * cs - nns > 0 else 0) + (pc * (n * cc - nnc) if n * cc - nnc > 0 else 0))
lo = 0
hi = max(nb // cb if cb != 0 else 0, ns // cs if cs != 0 else 0, nc // cc if cc != 0 else 0) - n_ready + r // (cb * pb + cs * ps + cc * pc) + 2
while lo < hi - 1:
	mid = (lo + hi) // 2
	if get_remain_money(mid) >= 0:
		lo = mid
	else:
		hi = mid
print(n_ready + lo)


res = input()
(nb, ns, nc) = map(int, input().split(' '))
(pb, ps, pc) = map(int, input().split(' '))
ru = int(input())
(cb, cs, cc) = (0, 0, 0)
for i in res:
	if i == 'B':
		cb += 1
	elif i == 'S':
		cs += 1
	elif i == 'C':
		cc += 1
l = 0
r = 10 ** 16 - 1
while r - l > 1:
	z = (r + l) // 2
	a = (cb * z - nb) * pb
	b = (cs * z - ns) * ps
	c = (cc * z - nc) * pc
	if a < 0:
		a = 0
	if b < 0:
		b = 0
	if c < 0:
		c = 0
	if a + b + c > ru:
		r = z
	else:
		l = z
a = (cb * r - nb) * pb
b = (cs * r - ns) * ps
c = (cc * r - nc) * pc
if a < 0:
	a = 0
if b < 0:
	b = 0
if c < 0:
	c = 0
if a + b + c > ru:
	print(l)
else:
	print(r)


receipt = input()
rec = [0, 0, 0]
for ing in receipt:
	if ing == 'B':
		rec[0] += 1
	elif ing == 'S':
		rec[1] += 1
	else:
		rec[2] += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
k = min(nb // rec[0] if rec[0] else 1000000000.0, ns // rec[1] if rec[1] else 1000000000.0, nc // rec[2] if rec[2] else 1000000000.0)
ans = k
if nb:
	nb -= rec[0] * k
if ns:
	ns -= rec[1] * k
if nc:
	nc -= rec[2] * k
while r > 0:
	ans += 1
	if nb >= rec[0]:
		nb -= rec[0]
	else:
		r -= pb * (rec[0] - nb)
		nb = 0
	if ns >= rec[1]:
		ns -= rec[1]
	else:
		r -= ps * (rec[1] - ns)
		ns = 0
	if nc >= rec[2]:
		nc -= rec[2]
	else:
		r -= pc * (rec[2] - nc)
		nc = 0
	if (nb if rec[0] else 0) == (ns if rec[1] else 0) == (nc if rec[2] else 0) == 0:
		break
ans += r // (rec[0] * pb + rec[1] * ps + rec[2] * pc)
print(ans)


s = list(input())
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
m = int(input())
(l, r) = (0, 10 ** 14 + 1)
while l < r:
	mid = (l + r) // 2
	cost1 = max(0, (mid * s.count('B') - nb) * pb)
	cost2 = max(0, (mid * s.count('S') - ns) * ps)
	cost3 = max(0, (mid * s.count('C') - nc) * pc)
	if cost1 + cost2 + cost3 <= m:
		l = mid + 1
	else:
		r = mid
print(r - 1)


st = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
b = 0
s = 0
c = 0
for char in st:
	if char == 'B':
		b += 1
	elif char == 'S':
		s += 1
	else:
		c += 1

def bin_search(mid):
	reqb = b * mid - nb
	reqc = c * mid - nc
	reqs = s * mid - ns
	cost = 0
	if reqb > 0:
		cost += pb * reqb
	if reqs > 0:
		cost += ps * reqs
	if reqc > 0:
		cost += pc * reqc
	if cost <= r:
		return True
	return False
lower = 0
upper = pow(10, 14)
res = 0
while lower <= upper:
	mid = lower + (upper - lower) // 2
	if bin_search(mid):
		res = mid
		lower = mid + 1
	else:
		upper = mid - 1
print(res)


s = input()
n = list(map(int, input().split()))
r = list(map(int, input().split()))
mon = int(input())
di = {}
di['B'] = 0
di['S'] = 0
di['C'] = 0
for i in s:
	di[i] = di[i] + 1
db = di['B']
ds = di['S']
dc = di['C']
nb = n[0]
ns = n[1]
nc = n[2]
sub = di['B'] * r[0]
sus = di['S'] * r[1]
suc = di['C'] * r[2]
su = sub + sus + suc
cnt = 0
if mon > 100000:
	ras = mon - 100000
	l = ras // su
	cnt = cnt + l
	mon = mon - su * l
while True:
	if nb >= db and ns >= ds and (nc >= dc):
		cnt = cnt + 1
		nb = nb - db
		ns = ns - ds
		nc = nc - dc
	else:
		while mon >= 0 and nb < db:
			nb = nb + 1
			mon = mon - r[0]
		while mon >= 0 and ns < ds:
			ns = ns + 1
			mon = mon - r[1]
		while mon >= 0 and nc < dc:
			nc = nc + 1
			mon = mon - r[2]
	if mon < 0:
		break
print(cnt)


def solution1(recipe, storage, price, r):
	for i in range(3):
		if recipe[i] == 0:
			recipe[i] = 1
			price[i] = 0
	burgers = min((storage[i] // recipe[i] for i in range(3)))
	storage = [storage[i] - burgers * recipe[i] for i in range(3)]
	while any(storage):
		quantity = [max(0, recipe[i] - storage[i]) for i in range(3)]
		needed_rubles = sum((quantity[i] * price[i] for i in range(3)))
		if r >= needed_rubles:
			r -= needed_rubles
			burgers += 1
			storage = [storage[i] + quantity[i] - recipe[i] for i in range(3)]
		else:
			return burgers
	price_burger = sum((price[i] * recipe[i] for i in range(3)))
	return burgers + r // price_burger

def cost_x_hamburgers(x, recipe, storage, price):
	ans = 0
	for i in range(3):
		ans += max(0, (recipe[i] * x - storage[i]) * price[i])
	return ans

def solution2(recipe, storage, price, r):
	(lo, hi) = (0, int(1e+24 + 100))
	while lo < hi:
		mid = lo + (hi - lo) // 2
		cur = cost_x_hamburgers(mid, recipe, storage, price)
		if cur >= r:
			hi = mid
		else:
			lo = mid + 1
	cost = cost_x_hamburgers(lo, recipe, storage, price)
	if cost > r:
		lo -= 1
	return lo

def main():
	hamburger = input()
	recipe = [hamburger.count(e) for e in 'BSC']
	storage = [int(c) for c in input().split()]
	price = [int(c) for c in input().split()]
	r = int(input())
	ans = solution2(recipe, storage, price, r)
	print(ans)
main()


s1 = input()
b = 0
s = 0
c = 0
for i in range(0, len(s1)):
	if s1[i] == 'B':
		b += 1
	elif s1[i] == 'S':
		s += 1
	else:
		c += 1
(n1, n2, n3) = map(int, input().split())
(p1, p2, p3) = map(int, input().split())
r = int(input())
l1 = [b, s, c]
l2 = [n1, n2, n3]
l3 = [p1, p2, p3]
i = 0
while i < len(l1) and len(l1) > 0:
	if l1[i] == 0:
		l1.pop(i)
		l2.pop(i)
		l3.pop(i)
	else:
		i += 1
max1 = 10000000000
for i in range(0, len(l1)):
	max1 = min(max1, l2[i] // l1[i])
low = 0
high = r + max1
res = -1
while low <= high:
	mid = (low + high) // 2
	flag = 0
	t = r
	for i in range(0, len(l1)):
		if l2[i] > mid * l1[i]:
			flag = 0
		elif t >= (mid * l1[i] - l2[i]) * l3[i]:
			t = t - (mid * l1[i] - l2[i]) * l3[i]
		else:
			flag = 1
			break
	if flag == 1:
		high = mid - 1
	else:
		res = mid
		low = mid + 1
if res == -1:
	print(0)
else:
	print(res)


s = input().rstrip()
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
roti = int(input())
countbread = s.count('B')
countsause = s.count('S')
countcheese = s.count('C')
count = 0
l = -1
r = 10 ** 20
while l < r - 1:
	mid = (l + r) // 2
	if pb * max(0, countbread * mid - nb) + pc * max(0, countcheese * mid - nc) + ps * max(0, countsause * mid - ns) <= roti:
		ans = mid
		l = mid
	else:
		r = mid
print(l)


ham = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
r = int(input())
hm = {'B': 0, 'S': 0, 'C': 0}
for i in ham:
	hm[i] += 1
hm = list(hm.values())

def func(x):
	money_left = r
	for i in range(len(n)):
		temp = (hm[i] * x - n[i]) * p[i]
		if temp > 0:
			money_left -= temp
		if money_left < 0:
			return False
	return True
(low, high, ans) = (0, max([n[i] // hm[i] for i in range(3) if hm[i] != 0]) + sum(p) + r, 0)
while low <= high:
	mid = (low + high) // 2
	check = func(mid)
	if check:
		ans = mid
		low = mid + 1
	else:
		high = mid - 1
print(ans)


import sys, math

def check(t, nb, ns, nc, pb, ps, pc, rb, rs, rc, r):
	b = max(0, t * rb - nb)
	c = max(0, t * rc - nc)
	s = max(0, t * rs - ns)
	amt = b * pb + c * pc + s * ps
	return r - amt
res = sys.stdin.readline()
(nb, ns, nc) = map(int, sys.stdin.readline().split(' '))
(pb, ps, pc) = map(int, sys.stdin.readline().split(' '))
r = int(sys.stdin.readline())
rb = res.count('B')
rs = res.count('S')
rc = res.count('C')
minn = 0
maxx = 10 ** 13 + 1
mid = 0
while maxx >= minn:
	mid = minn + (maxx - minn) // 2 + 1
	if maxx == minn:
		print(maxx)
		break
	l = check(mid, nb, ns, nc, pb, ps, pc, rb, rs, rc, r)
	if l < 0:
		maxx = mid - 1
	else:
		minn = mid


import math
f = {'B': 0, 'C': 0, 'S': 0}
s = input()
for c in s:
	if c in f:
		f[c] += 1
	else:
		f[c] = 1
[nb, ns, nc] = [int(i) for i in input().split()]
[pb, ps, pc] = [int(i) for i in input().split()]
if f['S'] == 0:
	ps = 0
if f['C'] == 0:
	pc = 0
if f['B'] == 0:
	pb = 0
h = int(input())
x = 0
mn = 0
mx = 100000000000000
ans = 0
while mn <= mx:
	x = math.floor((mn + mx) / 2)
	fx = max(f['B'] * x - nb, 0) * pb + max(f['C'] * x - nc, 0) * pc + max(f['S'] * x - ns, 0) * ps
	if fx < h:
		mn = x + 1
		ans = x
	elif fx > h:
		mx = x - 1
	else:
		ans = x
		break
print(ans)


s = input()
d = [s.count(i) for i in 'BSC']
h = list(map(int, input().split()))
p = list(map(int, input().split()))
m = int(input())

def valid(mid):
	tmp = [max(0, mid * d[i] - h[i]) for i in range(3)]
	total = sum([tmp[i] * p[i] for i in range(3)])
	return total <= m

def binary_search():
	(f, e) = (0, 1 << 61)
	while f <= e:
		mid = f + e >> 1
		if valid(mid):
			f = mid + 1
		else:
			e = mid - 1
	return f - 1
print(binary_search())


st = input()
(kb, ks, kc) = (0, 0, 0)
for i in st:
	if i == 'B':
		kb += 1
	elif i == 'S':
		ks += 1
	else:
		kc += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
x = int(input())
L = 0
R = 10000000000000000000
while R - L > 1:
	m = (R + L) // 2
	ch1 = max(0, m * kb - nb) * pb
	ch2 = max(0, m * ks - ns) * ps
	ch3 = max(0, m * kc - nc) * pc
	if ch1 + ch2 + ch3 <= x:
		L = m
	else:
		R = m
print(L)


def main():
	s = input()
	recipe = {c: s.count(c) for c in 'BSC'}
	count = dict(zip('BSC', map(int, input().split())))
	price = dict(zip('BSC', map(int, input().split())))
	for c in recipe:
		if not recipe[c]:
			(recipe[c], count[c], price[c]) = (1, 10 ** 13, 0)
	r = int(input())
	(x, y, z) = sorted('BSC', key=lambda c: count[c] // recipe[c])
	n = count[x] // recipe[x]
	tmp = recipe[x] * price[x]
	r += count[x] % recipe[x] * price[x]
	t = min(count[y] // recipe[y] - n, r // tmp)
	n += t
	r -= t * tmp
	tmp += recipe[y] * price[y]
	r += count[y] % recipe[y] * price[y]
	t = min(count[z] // recipe[z] - n, r // tmp)
	n += t
	r -= t * tmp
	tmp += recipe[z] * price[z]
	r += count[z] % recipe[z] * price[z]
	print(n + r // tmp)
main()


def f(n):
	moneyForB = 0
	moneyForS = 0
	moneyForC = 0
	if n * nb > q[0]:
		moneyForB = (n * nb - q[0]) * p[0]
	if n * ns > q[1]:
		moneyForS = (n * ns - q[1]) * p[1]
	if n * nc > q[2]:
		moneyForC = (n * nc - q[2]) * p[2]
	totalp = moneyForB + moneyForS + moneyForC
	if totalp <= money:
		return True
	else:
		return False

def BNFindFirst(start, end, val):
	while start < end:
		mid = int(start + (end - start + 1) / 2)
		c = f(mid)
		if c > val:
			start = mid + 1
		elif c < val:
			end = mid - 1
		else:
			start = mid
	return start
s = input()
nb = s.count('B')
ns = s.count('S')
nc = s.count('C')
q = list(map(int, input().split(' ')))
p = list(map(int, input().split(' ')))
money = int(input())
x = BNFindFirst(0, 10 ** 16, 1)
print(x)


from collections import Counter

def calculate_burgers(number_of_burgers, extra_money):
	needed_ingredients = [0] * 3
	for (ingredients, value) in recipe.items():
		if ingredients == 'B':
			needed_ingredients[0] = value * number_of_burgers
		elif ingredients == 'S':
			needed_ingredients[1] = value * number_of_burgers
		else:
			needed_ingredients[2] = value * number_of_burgers
	for (index, element) in enumerate(needed_ingredients):
		if element > available_items[index]:
			extra_money -= (element - available_items[index]) * item_prices[index]
		if extra_money < 0:
			return False
	return True
recipe = dict(Counter(input().strip()))
available_items = list(map(int, input().strip().split(' ')))
item_prices = list(map(int, input().strip().split(' ')))
extra_rubles = int(input())
(left, right) = (0, int(1e+18))
while left < right:
	middle = (left + right + 1) // 2
	if calculate_burgers(middle, extra_rubles):
		left = middle
	else:
		right = middle - 1
print(left)


import sys
input = lambda : sys.stdin.readline().strip()
s = input()
rb = s.count('B')
rs = s.count('S')
rc = s.count('C')
(nb, ns, nc) = [int(x) for x in input().split()]
(pb, ps, pc) = [int(x) for x in input().split()]
r = int(input())

def bsearch(L, R, can, left_feasible=True):
	while R - L > 1:
		M = L + (R - L) // 2
		if can(M) ^ left_feasible:
			R = M
		else:
			L = M
	return L if left_feasible else R
print(bsearch(0, 10 ** 13, lambda k: max(k * rb - nb, 0) * pb + max(k * rs - ns, 0) * ps + max(k * rc - nc, 0) * pc <= r))


from collections import Counter

def getPrice(x):
	return max(0, ingr['B'] * x - iHave[0]) * price[0] + max(0, ingr['S'] * x - iHave[1]) * price[1] + max(0, ingr['C'] * x - iHave[2]) * price[2]
recipe = input()
ingr = Counter(recipe)
iHave = [int(x) for x in input().split()]
price = [int(x) for x in input().split()]
budget = int(input())
(upper_bound, lower_bound) = (int(10000000000000.0), 0)
x = int(upper_bound / 2 + lower_bound / 2)
while upper_bound >= lower_bound:
	pricey = getPrice(x)
	if pricey > budget:
		upper_bound = x - 1
	else:
		lower_bound = x + 1
	x = int(upper_bound / 2 + lower_bound / 2)
print(x)


h = input()
c = (h.count('B'), h.count('S'), h.count('C'))
n = list(map(int, input().split()))
p = list(map(int, input().split()))
r = int(input())
(a, b) = (0, 10 ** 15)
while a + 1 < b:
	x = (a + b) // 2
	y = sum((max(0, x * c[i] - n[i]) * p[i] for i in range(3)))
	if y <= r:
		a = x
	else:
		b = x
print(a)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
cb = s.count('B')
cs = s.count('S')
cc = s.count('C')
(l, h) = (0, 10000000000000)
while h - l > 1:
	x = (h + l) // 2
	fx = max(0, cb * x - nb) * pb + max(0, cs * x - ns) * ps + max(0, cc * x - nc) * pc
	if fx > r:
		h = x
	else:
		l = x
print(l)


def process(key, a, b, p, k, n):
	nc = [b[i] - a[i] * key for i in range(n)]
	c = 0
	for i in range(n):
		if nc[i] < 0:
			c += p[i] * nc[i] * -1
	k -= c
	if k >= 0:
		return True
	else:
		return False

def bin_src(a, b, p, n, k, lo, hi):
	while hi - lo > 1:
		mid = (hi + lo) // 2
		val = process(mid, a, b, p, k, n)
		if val == True:
			lo = mid
		else:
			hi = mid
	if process(lo, a, b, p, k, n) == True:
		return lo
	else:
		return hi

def main():
	s = input()
	b = list(map(int, input().split(' ')))
	a = [0, 0, 0]
	for i in s:
		if i == 'B':
			a[0] += 1
		elif i == 'S':
			a[1] += 1
		else:
			a[2] += 1
	p = list(map(int, input().split(' ')))
	k = int(input())
	print(bin_src(a, b, p, 3, k, 0, 10 ** 14))
main()


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def ham(mid):
	br = (b * mid - nb) * pb
	sr = (s * mid - ns) * ps
	cr = (c * mid - nc) * pc
	if br < 0:
		br = 0
	if sr < 0:
		sr = 0
	if cr < 0:
		cr = 0
	if br + sr + cr <= money:
		return True
	return False
a = input().strip()
(nb, ns, nc) = li()
(pb, ps, pc) = li()
(b, s, c) = (a.count('B'), a.count('S'), a.count('C'))
money = I()
(l, r) = (0, 10 ** 12 + 100000)
ans = 0
while l <= r:
	mid = (l + r) // 2
	if ham(mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


s = input()
n = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
num = int(input())
q = [0, 0, 0]
id = 'BSC'
for i in s:
	q[id.index(i)] += 1

def f(x):
	ans = 0
	for i in range(3):
		ans += max(0, q[i] * x - n[i]) * p[i]
	return ans
l = 0
r = 100000000000000000
ans = 0
while l <= r:
	mid = l + (r - l) // 2
	if f(mid) <= num:
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


import sys
inf = float('inf')

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def check(n):
	required = max(0, mydict['B'] * n - nb) * pb + max(0, mydict['S'] * n - ns) * ps + max(0, mydict['C'] * n - nc) * pc
	return required <= money

def binary_search(low, high):
	ans = 0
	while low <= high:
		mid = low + (high - low >> 1)
		if check(mid):
			ans = mid
			low = mid + 1
		else:
			high = mid - 1
	return ans
string = input()
mydict = {'B': 0, 'S': 0, 'C': 0}
for i in string:
	mydict[i] = mydict.get(i, 0) + 1
(nb, ns, nc) = get_ints()
(pb, ps, pc) = get_ints()
money = int(input())
print(binary_search(0, 1000000000000000))


s = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
m = int(input())
ps = (s.count('B'), s.count('S'), s.count('C'))

def solve(mid):
	return max(0, ps[0] * mid - n[0]) * p[0] + max(0, ps[1] * mid - n[1]) * p[1] + max(0, ps[2] * mid - n[2]) * p[2]
l = 0
r = m
mx = 0
for _ in range(r.bit_length()):
	mid = (l + r) // 2
	c = solve(mid)
	if c > m:
		r = mid
	else:
		mx = max(mx, mid)
		l = mid
for mid in range(mx - 10000, mx + 10000):
	c = solve(mid)
	if c <= m:
		mx = max(mx, mid)
print(mx)


string = input()
nb = 0
ns = 0
nc = 0
for i in string:
	if i == 'B':
		nb += 1
	elif i == 'S':
		ns += 1
	else:
		nc += 1
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
tr = int(input())
rc = nb * pb + nc * pc + ns * ps
l = 0
r = int(10000000000000.0 + 1)
l = -1
while l < r - 1:
	m = (l + r) // 2
	if max(0, pb * (nb * m - b)) + max(0, pc * (nc * m - c)) + max(0, ps * (ns * m - s)) <= tr:
		temp_ans = m
		l = m
	else:
		r = m
print(temp_ans)


import sys, os, io
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int, input().split()))
S = input()
(b, s, c) = Neo()
(pb, ps, pc) = Neo()
(amt,) = Neo()
(l, r) = (0, 10 ** 18)

def check(m):
	x = max(0, m * S.count('B') - b)
	y = max(0, m * S.count('S') - s)
	z = max(0, m * S.count('C') - c)
	if amt >= x * pb + y * ps + z * pc:
		return 1
	return 0
for i in range(100):
	m = (l + r) // 2
	if check(m):
		l = m
	else:
		r = m
print(m)


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):
	if ingreds[2][0] == 0:
		(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 += 1 + R // one_cost
		return full_burgers
	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])
		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)
	(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]])
	(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
	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
	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
		full_burgers += 1
		return full_burgers + R // (B * 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)
	assert B + S + C == len(burger)
	ingreds = [[NB // B, NB, B, PB], [NS // S, NS, S, PS], [NC // C, NC, C, PC]]
	ingreds.sort()
	full_burgers = ingreds[0][0]
	NB -= B * full_burgers
	NS -= S * full_burgers
	NC -= C * full_burgers
	ingreds = [[NB // B, NB, B, PB], [NS // S, NS, S, PS], [NC // C, NC, C, PC]]
	ingreds.sort()
	one_cost = B * PB + S * PS + C * PC
	return solve2(ingreds, full_burgers, one_cost, R)
print(solve())


def Divide(a, b):
	if b == 0:
		return 10 ** 100
	return a // b

def main():
	s = input()
	(Nb, Ns, Nc) = map(int, input().split())
	(Pb, Ps, Pc) = map(int, input().split())
	r = int(input())
	B = s.count('B')
	S = s.count('S')
	C = s.count('C')
	needed = B * Pb + C * Pc + S * Ps
	ans = 0
	while 1:
		x = needed
		if Nb != 0:
			if Nb > B:
				x -= B * Pb
				Nb -= B
			else:
				x -= Nb * Pb
				Nb = 0
		if Nc != 0:
			if Nc > C:
				x -= C * Pc
				Nc -= C
			else:
				x -= Nc * Pc
				Nc = 0
		if Ns != 0:
			if Ns > S:
				x -= S * Ps
				Ns -= S
			else:
				x -= Ns * Ps
				Ns = 0
		if x <= r:
			r -= x
			ans += 1
		else:
			break
		if (Nb == 0 or B == 0) and (Nc == 0 or C == 0) and (Ns == 0 or S == 0):
			break
	ans += r // needed
	print(ans)
	return
main()


st = input()
b = 0
s = 0
c = 0
count = 0
for i in range(len(st)):
	if st[i] == 'B':
		b += 1
	elif st[i] == 'S':
		s += 1
	elif st[i] == 'C':
		c += 1
aval = list(map(int, input().split()))
prices = list(map(int, input().split()))
rubies = int(input())
if b == 0:
	aval[0] = 0
	prices[0] = 0
if s == 0:
	aval[1] = 0
	prices[1] = 0
if c == 0:
	aval[2] = 0
	prices[2] = 0
while aval[0] >= b and aval[1] >= s and (aval[2] >= c):
	aval[0] = aval[0] - b
	aval[1] = aval[1] - s
	aval[2] = aval[2] - c
	count += 1
while rubies >= min(prices):
	if aval[0] == aval[1] == aval[2] == 0:
		count += rubies // (b * prices[0] + s * prices[1] + c * prices[2])
		print(count)
		exit()
	if aval[0] < b:
		rubies = rubies - (b - aval[0]) * prices[0]
		aval[0] = b
	if aval[1] < s:
		rubies = rubies - (s - aval[1]) * prices[1]
		aval[1] = s
	if aval[2] < c:
		rubies = rubies - (c - aval[2]) * prices[2]
		aval[2] = c
	aval[0] = aval[0] - b
	aval[1] = aval[1] - s
	aval[2] = aval[2] - c
	if rubies >= 0:
		count += 1
print(count)


while True:
	try:
		bsc = input()
		(nb, ns, nc) = map(int, input().split())
		(pb, ps, pc) = map(int, input().split())
		r = int(input())
		b = bsc.count('B')
		s = bsc.count('S')
		c = bsc.count('C')
		(left, right, ans) = (0, 10 ** 14 + 10, 0)
		while left <= right:
			mid = left + (right - left) // 2
			cb = mid * b - nb
			cs = mid * s - ns
			cc = mid * c - nc
			if cb < 0:
				cb = 0
			if cs < 0:
				cs = 0
			if cc < 0:
				cc = 0
			cnt = cb * pb + cs * ps + cc * pc
			if cnt <= r:
				ans = mid
			if cnt > r:
				right = mid - 1
			else:
				left = mid + 1
		print(ans)
	except Exception as e:
		break


s = input()
mp = [0] * 200
for i in s:
	mp[ord(i)] += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
x = int(input())
l = 0
r = 1000000000000000000
ans = 0
while l <= r:
	md = (l + r) // 2
	temp = x
	plusB = 0
	checkB = 1
	if md * mp[ord('B')] > nb:
		plusB += md * mp[ord('B')] - nb
		plusB *= pb
		if plusB <= temp:
			temp -= plusB
		else:
			checkB = 0
	plusS = 0
	checkS = 1
	if md * mp[ord('S')] > ns:
		plusS += md * mp[ord('S')] - ns
		plusS *= ps
		if plusS <= temp:
			temp -= plusS
		else:
			checkS = 0
	plusC = 0
	checkC = 1
	if md * mp[ord('C')] > nc:
		plusC += md * mp[ord('C')] - nc
		plusC *= pc
		if plusC <= temp:
			temp -= plusC
		else:
			checkC = 0
	if checkB == 1 and checkS == 1 and (checkC == 1):
		ans = max(ans, md)
		l = md + 1
	else:
		r = md - 1
print(ans)


recipe = input()
from collections import Counter
ings = Counter(recipe)
rb = ings['B']
rs = ings['S']
rc = ings['C']
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
total_burger = 0
while nb != 0 and rb or (ns != 0 and rs) or (nc != 0 and rc):
	if nb < rb:
		if (rb - nb) * pb <= r:
			r -= (rb - nb) * pb
			nb = rb
	if nc < rc:
		if (rc - nc) * pc <= r:
			r -= (rc - nc) * pc
			nc = rc
	if ns < rs:
		if (rs - ns) * ps <= r:
			r -= (rs - ns) * ps
			ns = rs
	if nb >= rb and nc >= rc and (ns >= rs):
		total_burger += 1
		nb -= rb
		nc -= rc
		ns -= rs
	else:
		print(total_burger)
		exit(0)
price_of_a_burger = rb * pb + rc * pc + rs * ps
print(total_burger + r // price_of_a_burger)


def f(x):
	cb = max(0, (tb * x - nb) * pb)
	cs = max(0, (ts * x - ns) * ps)
	cc = max(0, (tc * x - nc) * pc)
	return cc + cs + cb <= rs
s = list(input())
(tb, ts, tc) = (s.count('B'), s.count('S'), s.count('C'))
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
rs = int(input())
(l, r) = (0, 10 ** 14)
while l < r:
	m = (l + r) // 2
	if f(m):
		l = m + 1
	else:
		r = m
print(l - 1)


import math
t = 1
for _ in range(t):
	s = input()
	n = list(map(int, input().split()))
	p = list(map(int, input().split()))
	r = int(input())
	nn = [0, 0, 0]
	for i in s:
		if i == 'B':
			nn[0] += 1
		elif i == 'S':
			nn[1] += 1
		else:
			nn[2] += 1
	low = 0
	high = 10 ** 14
	maxburgers = 0
	while low <= high:
		mid = (low + high) // 2
		nob = max(0, mid * nn[0] - n[0])
		nos = max(0, mid * nn[1] - n[1])
		noc = max(0, mid * nn[2] - n[2])
		price = nob * p[0] + nos * p[1] + noc * p[2]
		if price <= r:
			maxburgers = max(maxburgers, mid)
			low = mid + 1
		else:
			high = mid - 1
	print(maxburgers)


3

def readln():
	return tuple(map(int, input().split()))
cb = cs = cc = 0
for c in list(input()):
	if c == 'B':
		cb += 1
	if c == 'S':
		cs += 1
	if c == 'C':
		cc += 1
(nb, ns, nc) = readln()
(pb, ps, pc) = readln()
(p,) = readln()
a = 0
b = 10 ** 15
while b - a > 1:
	m = a + b >> 1
	if max(0, m * cb - nb) * pb + max(0, m * cs - ns) * ps + max(0, m * cc - nc) * pc <= p:
		a = m
	else:
		b = m
print(a)


from collections import defaultdict
rec = list(input())
d = defaultdict(int)
for elem in rec:
	d[elem] += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
(b, s, c) = (d['B'], d['S'], d['C'])
(l, h) = (0, r + 3 * nb)
while h - l > 1:
	mid = (l + h) // 2
	t = r
	if mid * b > nb:
		t -= (mid * b - nb) * pb
	if mid * s > ns:
		t -= (mid * s - ns) * ps
	if mid * c > nc:
		t -= (mid * c - nc) * pc
	if t >= 0:
		l = mid
	else:
		h = mid
print(l)


ricetta = input()
(nb, ns, nc) = [int(x) for x in input().split()]
(pb, ps, pc) = [int(x) for x in input().split()]
(b, s, c) = (0, 0, 0)
r = int(input())
for x in ricetta:
	if x == 'B':
		b += 1
	if x == 'S':
		s += 1
	if x == 'C':
		c += 1
x = 0
y = 2
prezzo = 0
while x < y:
	prezzo = max(0, pb * b * y - nb * pb) + max(0, pc * c * y - nc * pc) + max(0, ps * s * y - ns * ps)
	if prezzo < r:
		x = y
		y = 2 * y
	if prezzo > r:
		y = (x + y) // 2
	if prezzo == r:
		x = y
print(x)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
s = input().strip()
(a, b, c) = (s.count('B'), s.count('S'), s.count('C'))
(n1, n2, n3) = I()
(p1, p2, p3) = I()
(r,) = I()
xx = [a, b, c].count(0)
if xx == 2:
	print([(n1 + r // p1 if i == a else n2 + r // p2 if i == b else n3 + r // p3) // i for i in [a, b, c] if i][0])
elif xx:
	if a == 0:
		a = c
		n1 = n3
		p1 = p3
	elif b == 0:
		b = c
		n2 = n3
		p2 = p3
	an = min(n1 // a, n2 // b)
	n1 -= an * a
	n2 -= an * b
	while r:
		if n1 + n2 == 0:
			an += r // (a * p1 + b * p2)
			break
		(r1, r2) = (n1 // a, n2 // b)
		rq = 0
		if r1 == 0:
			rq += p1 * (a - n1)
			n1 = 0
		else:
			n1 -= a
		if r2 == 0:
			rq += p2 * (b - n2)
			n2 = 0
		else:
			n2 -= b
		if r >= rq:
			r -= rq
			an += 1
		else:
			break
	print(an)
else:
	an = min(n1 // a, n2 // b, n3 // c)
	n1 -= an * a
	n2 -= an * b
	n3 -= an * c
	ct = a * p1 + b * p2 + c * p3
	while r:
		if n1 + n2 + n3 == 0:
			an += r // ct
			break
		(r1, r2, r3) = (n1 // a, n2 // b, n3 // c)
		rq = 0
		if r1 == 0:
			rq += p1 * (a - n1)
			n1 = 0
		else:
			n1 -= a
		if r2 == 0:
			rq += p2 * (b - n2)
			n2 = 0
		else:
			n2 -= b
		if r3 == 0:
			rq += p3 * (c - n3)
			n3 = 0
		else:
			n3 -= c
		if rq > r:
			break
		r -= rq
		an += 1
	print(an)


def binarySearch(recipe_counts, costs, shop_counts, l, r, R):
	while l < r - 1:
		mid = (l + r) // 2
		val = sum([max(0, costs[i] * (recipe_counts[i] * mid - shop_counts[i])) for i in range(3)])
		if val <= R:
			l = mid
		else:
			r = mid
	return l
recipe = input().strip()
shop_counts = [int(i) for i in input().split()]
costs = [int(i) for i in input().split()]
rem_amt = int(input())
count = 0
recipe_counts = [0, 0, 0]
for i in recipe:
	if i == 'B':
		recipe_counts[0] += 1
	elif i == 'S':
		recipe_counts[1] += 1
	else:
		recipe_counts[2] += 1
r = 10 ** 13
l = 0
ans = binarySearch(recipe_counts, costs, shop_counts, l, r, R=rem_amt)
print(ans)


import os
from sys import stdin, stdout

def solve(tc):
	ingredient = stdin.readline().strip()
	kitchen = list(map(int, stdin.readline().split()))
	cost = list(map(int, stdin.readline().split()))
	money = int(stdin.readline().strip())
	ahamburger = [0, 0, 0]
	for c in ingredient:
		if c == 'B':
			ahamburger[0] += 1
		elif c == 'S':
			ahamburger[1] += 1
		else:
			ahamburger[2] += 1
	(lo, hi) = (0, int(1000000000000.0 + 101))
	while lo < hi:
		mid = lo + (hi - lo) // 2
		needed = 0
		for i in range(3):
			if ahamburger[i]:
				cnt = max(0, ahamburger[i] * mid - kitchen[i])
				needed += cnt * cost[i]
				if needed > money:
					break
		if needed <= money:
			lo = mid + 1
		else:
			hi = mid
	if lo > 0:
		lo -= 1
	print(lo)
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


import sys
import math
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
St = ns()
N = na()
P = na()
R = ni()
n = [0, 0, 0]
ind = {'B': 0, 'S': 1, 'C': 2}
for c in St:
	n[ind[c]] += 1
low = 0
high = 10 ** 15
cost = 0
while low < high:
	mid = low + (high - low + 1) // 2
	cost = 0
	for i in range(len(N)):
		cost += max(0, n[i] * mid - N[i]) * P[i]
	if cost <= R:
		low = mid
	else:
		high = mid - 1
print(low)


from collections import Counter
s = input()
l = dict(Counter(s))
b = 0
s = 0
c = 0
if 'B' in l.keys():
	b = l['B']
if 'S' in l.keys():
	s = l['S']
if 'C' in l.keys():
	c = l['C']
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
m = int(input())
low = 0
high = 1e+18
while high - low > 1:
	mid = low + (high - low) // 2
	tb = max(0, mid * b - nb)
	ts = max(0, mid * s - ns)
	tc = max(0, mid * c - nc)
	ok = 1
	tm = m
	if tb * pb <= tm:
		tm -= tb * pb
	else:
		ok = 0
	if ts * ps <= tm:
		tm -= ts * ps
	else:
		ok = 0
	if tc * pc <= tm:
		tm -= tc * pc
	else:
		ok = 0
	if ok == 1:
		low = mid
	else:
		high = mid
ok1 = 1
ans = high
tb = max(0, high * b - nb)
ts = max(0, high * s - ns)
tc = max(0, high * c - nc)
tm = m
if tb * pb <= tm:
	tm -= tb * pb
else:
	ok1 = 0
if ts * ps <= tm:
	tm -= ts * ps
else:
	ok1 = 0
if tc * pc <= tm:
	tm -= tc * pc
else:
	ok1 = 0
if ok1 != 1:
	ans = low
print(int(ans))


st = input()
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
rb = rs = rc = 0
lo = 0
hi = int(10000000000000.0)
for i in range(len(st)):
	if st[i] is 'B':
		rb = rb + 1
	if st[i] is 'S':
		rs = rs + 1
	if st[i] is 'C':
		rc = rc + 1
for i in range(70):
	mid = lo + hi + 1 >> 1
	tc = max(0, mid * rb - b) * pb + max(0, mid * rs - s) * ps + max(0, mid * rc - c) * pc
	if tc <= r:
		lo = mid
	else:
		hi = mid - 1
print(lo)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
l = list(input())
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
p = int(input())
cb = l.count('B')
cs = l.count('S')
cc = l.count('C')
l = -1
r = 10 ** 20
while l < r - 1:
	m = (l + r) // 2
	ans = max(0, cb * m - nb) * pb + max(0, cs * m - ns) * ps + max(0, cc * m - nc) * pc
	if ans <= p:
		l = m
	else:
		r = m
print(l)


def check(x, N, P, r, s):
	cb = s.count('B')
	cs = s.count('S')
	cc = s.count('C')
	b_need = max(0, cb * x - N[0])
	s_need = max(0, cs * x - N[1])
	c_need = max(0, cc * x - N[2])
	cost = b_need * P[0] + c_need * P[2] + s_need * P[1]
	r -= cost
	if r < 0:
		return False
	return True
s = input()
N = list(map(int, input().split()))
P = list(map(int, input().split()))
r = int(input())
ans = 0
l = 0
h = 10000000000000.0
while l < h:
	mid = (l + h + 1) // 2
	if check(mid, N, P, r, s):
		ans = mid
		l = mid
	else:
		h = mid - 1
print(int(ans))


s = input()
fb = 0
fs = 0
fc = 0
for c in s:
	if c == 'B':
		fb += 1
	elif c == 'S':
		fs += 1
	else:
		fc += 1
(nb, ns, nc) = [int(i) for i in input().split()]
(pb, ps, pc) = [int(i) for i in input().split()]
fp = int(input())
INT_MX = 10 ** 12 + 101
rb = INT_MX
rs = INT_MX
rc = INT_MX
lb = INT_MX
ls = INT_MX
lc = INT_MX
rs1 = INT_MX
ls1 = INT_MX

def solve2(l, r, p):
	global rb
	global rs
	global rc
	global lb
	global ls
	global lc
	global rs1
	global ls1
	m = int((l + r) / 2)
	m1 = min(m + fs - (ns + m) % fs, r)
	if fs > 0:
		rs1 = int((ns + m1) / fs)
		ls1 = int((ns + m) / fs)
	if fc > 0:
		rc = int((nc + int((p - m1 * ps) / pc)) / fc)
		lc = int((nc + int((p - m * ps) / pc)) / fc)
	right = min(rs1, rc)
	left = min(ls1, lc)
	if l == r:
		return left
	elif right > left:
		return solve2(m + 1, r, p)
	else:
		return solve2(l, m, p)

def solve(l, r):
	global rb
	global rs
	global rc
	global lb
	global ls
	global lc
	m = int((l + r) / 2)
	m1 = min(m + fb - (nb + m) % fb, r)
	if fb > 0:
		rb = int((nb + m1) / fb)
		lb = int((nb + m) / fb)
	if fs > 0:
		rs = solve2(0, int((fp - m1 * pb) / ps), fp - m1 * pb)
		ls = solve2(0, int((fp - m * pb) / ps), fp - m * pb)
	elif fc > 0:
		rs = int((nc + int((fp - m1 * pb) / pc)) / fc)
		ls = int((nc + int((fp - m * pb) / pc)) / fc)
	right = min(rb, rs)
	left = min(lb, ls)
	if l == r:
		return left
	elif right > left:
		return solve(m + 1, r)
	else:
		return solve(l, m)
if fb > 0:
	res = solve(0, int(fp / pb))
elif fs > 0:
	res = solve2(0, int(fp / ps), fp)
else:
	res = int((int(fp / pc) + nc) / fc)
print(res)


rec = input()
(b, s, c) = (0, 0, 0)
for i in rec:
	if i == 'B':
		b += 1
	elif i == 'S':
		s += 1
	else:
		c += 1
(nb, ns, nc) = [int(i) for i in input().split()]
(pb, ps, pc) = [int(i) for i in input().split()]
r = int(input())
one = b * pb + s * ps + c * pc
ans = 999999999999999999999
if b != 0:
	ans = min(ans, nb // b)
else:
	nb = 0
if s != 0:
	ans = min(ans, ns // s)
else:
	ns = 0
if c != 0:
	ans = min(ans, nc // c)
else:
	nc = 0
(nb, ns, nc) = (nb - ans * b, ns - ans * s, nc - ans * c)
while nb != 0 or ns != 0 or nc != 0:
	need = 0
	if b > nb:
		need += pb * (b - nb)
	if s > ns:
		need += ps * (s - ns)
	if c > nc:
		need += pc * (c - nc)
	nb = 0 if b > nb else nb - b
	ns = 0 if s > ns else ns - s
	nc = 0 if c > nc else nc - c
	if need <= r:
		r -= need
		ans += 1
	else:
		print(ans)
		break
else:
	ans += r // one
	print(ans)


recipe = list(input())
(nb, ns, nc) = [int(x) for x in input().split()]
(pb, ps, pc) = [int(x) for x in input().split()]
r = int(input())
b = recipe.count('B')
s = recipe.count('S')
c = recipe.count('C')
rate = []
if b != 0:
	rateb = int(nb / b)
	rate.append(rateb)
if s != 0:
	rates = int(ns / s)
	rate.append(rates)
if c != 0:
	ratec = int(nc / c)
	rate.append(ratec)
m = min(rate)
nb -= m * b
ns -= m * s
nc -= m * c
rr = r + nb * pb + nc * pc + ps * ns
num = int(rr / (pb * b + ps * s + pc * c))
x = 0
while x == 0:
	money = pb * max(b * num - nb, 0) + ps * max(s * num - ns, 0) + pc * max(num * c - nc, 0)
	if r >= money:
		x = 1
	num -= 1
print(num + m + 1)


s = input()
(B, S, C) = (0, 0, 0)
for i in s:
	if i == 'B':
		B += 1
	if i == 'S':
		S += 1
	if i == 'C':
		C += 1
(a, b, c) = list(map(int, input().split()))
(pa, pb, pc) = list(map(int, input().split()))
money = int(input())
k = 0
lbound = -1
hbound = 10 ** 20
for i in range(1000):
	mid = (hbound + lbound) // 2
	ka = B * mid - a
	kb = S * mid - b
	kc = C * mid - c
	if money >= max(0, ka * pa) + max(0, kb * pb) + max(kc * pc, 0):
		k = max(mid, k)
		lbound = mid
	else:
		hbound = mid
print(k)


import sys
from math import log2, floor, ceil, sqrt, gcd
import bisect
sys.setrecursionlimit(10 ** 5)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 1000000007

def solve(n):
	nob = n * dic.get('B', 0)
	noc = n * dic.get('C', 0)
	nos = n * dic.get('S', 0)
	reqb = max(nob - b, 0)
	reqs = max(nos - s, 0)
	reqc = max(noc - c, 0)
	if reqb * pb + reqs * ps + reqc * pc > rr:
		return False
	return True
st = ri()
(b, s, c) = Ri()
(pb, ps, pc) = Ri()
rr = int(ri())
dic = {}
for i in range(len(st)):
	if st[i] in dic:
		dic[st[i]] += 1
	else:
		dic[st[i]] = 1
l = 0
r = 10 ** 13
ans = 0
while l <= r:
	mid = (l + r) // 2
	if solve(mid):
		ans = max(ans, mid)
		l = mid + 1
	else:
		r = mid - 1
print(ans)


st = input()
(b, s, c) = map(int, input().split())
(rb, rs, rc) = map(int, input().split())
money = int(input())
b_b = st.count('B')
b_s = st.count('S')
b_c = st.count('C')
(l, r) = (0, 10000000000000)
while 1 < r - l:
	mid = (l + r) // 2
	val = max(0, b_b * mid - b) * rb + max(0, b_s * mid - s) * rs + max(0, b_c * mid - c) * rc
	if val > money:
		r = mid
	else:
		l = mid
print(l)


s = input()
h = [0, 0, 0]
for i in s:
	if i == 'B':
		h[0] += 1
	elif i == 'S':
		h[1] += 1
	elif i == 'C':
		h[2] += 1
n = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
cena = int(input())
(l, r) = (0, 10 ** 13)
while r - l > 1:
	m = (r + l) // 2
	cost = 0
	for i in range(3):
		cost += p[i] * max(0, h[i] * m - n[i])
	if cost <= cena:
		l = m
	else:
		r = m
print(l)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
cost = int(input())
s = list(s)
cb = s.count('B')
cs = s.count('S')
cc = s.count('C')

def pos(m):
	br = max(0, cb * m - nb)
	sa = max(0, cs * m - ns)
	ch = max(0, cc * m - nc)
	ct = br * pb + sa * ps + ch * pc
	if ct <= cost:
		return True
	else:
		return False
l = 0
r = 10000000000000.0
ans = 0
while l <= r:
	m = l + (r - l) // 2
	if pos(m):
		ans = m
		l = m + 1
	else:
		r = m - 1
print(int(ans))


def price(x):
	return max(0, r_b * x - n_b) * p_b + max(0, r_s * x - n_s) * p_s + max(0, r_c * x - n_c) * p_c
recipe = input().strip()
(r_b, r_s, r_c) = (recipe.count('B'), recipe.count('S'), recipe.count('C'))
(n_b, n_s, n_c) = map(int, input().split())
(p_b, p_s, p_c) = map(int, input().split())
r = int(input())
(low, mid, high, ans) = (1, -1, 10 ** 16, 0)
while low < high:
	mid = (high + low) // 2
	if price(mid) <= r < price(mid + 1):
		ans = mid
		break
	elif price(mid) <= r:
		low = mid + 1
	elif price(mid) > r:
		high = mid
print(ans)


recipe = input()
reqs = [recipe.count(x) for x in 'BSC']
counts = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
money = int(input())
burgerPrice = sum((reqs[i] * prices[i] for i in range(3)))
(cur, match) = (1000000000000.0, 0)
for i in range(3):
	if reqs[i] == 0:
		continue
	cur = min(cur, counts[i] // reqs[i])
	match = max(match, counts[i] // reqs[i])
while cur <= match and money >= min(prices):
	cur += 1
	for i in range(3):
		if reqs[i] == 0:
			continue
		while counts[i] // reqs[i] < cur and money >= prices[i]:
			money -= prices[i]
			counts[i] += 1
ans = 1000000000000.0
for i in range(3):
	if reqs[i] == 0:
		continue
	ans = min(ans, counts[i] // reqs[i])
ans += money // burgerPrice
print(ans)


import time, math, bisect, sys
sys.setrecursionlimit(100000)
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

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

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 9 + 7

def solve():
	s = input()
	d = {}
	for ele in s:
		d[ele] = d.get(ele, 0) + 1
	(nb, ns, nc) = IP()
	(pb, ps, pc) = IP()
	(b, s, c) = (d.get('B', 0), d.get('S', 0), d.get('C', 0))
	r = II()
	(low, high) = (0, 4 * 10 ** 12)
	ans = 0
	while high - low > 1:
		r1 = r
		mid = (low + high) // 2
		(b1, s1, c1) = (mid * b, mid * s, mid * c)
		(stm1, stm2, stm3) = (True, True, True)
		if b1 > nb:
			extra = b1 - nb
			if pb * extra > r1:
				stm1 = False
			else:
				r1 = r1 - pb * extra
		if s1 > ns:
			extra = s1 - ns
			if ps * extra > r1:
				stm2 = False
			else:
				r1 = r1 - ps * extra
		if c1 > nc:
			extra = c1 - nc
			if pc * extra > r1:
				stm3 = False
			else:
				r1 = r1 - pc * extra
		if stm1 and stm2 and stm3:
			ans = mid
			low = mid
		else:
			high = mid
	print(ans)
solve()


def f(x):
	global nb, nc, ns, pb, pc, ps, rb, rc, rs
	return max(0, rb * x - nb) * pb + max(0, rs * x - ns) * ps + max(0, rc * x - nc) * pc
r = input()
n = len(r)
rb = 0
rs = 0
rc = 0
for i in range(n):
	if r[i] == 'B':
		rb += 1
	elif r[i] == 'S':
		rs += 1
	else:
		rc += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
x = int(input())
l = 0
r = 2000000000000
ans = l
while l <= r:
	mid = (l + r) // 2
	if f(mid) > x:
		r = mid - 1
	else:
		l = mid + 1
		ans = mid
print(ans)


(cntA, cntB, cntC) = (0, 0, 0)
for x in input():
	if x == 'B':
		cntA += 1
	elif x == 'S':
		cntB += 1
	else:
		cntC += 1
(a, b, c) = map(int, input().split())
(x, y, z) = map(int, input().split())
rr = int(input())
l = 0
r = int(10000000000000.0)
while r - l > 1:
	m = (l + r) // 2
	s = max(0, (m * cntA - a) * x) + max(0, (m * cntB - b) * y) + max(0, (m * cntC - c) * z)
	if s <= rr:
		l = m
	else:
		r = m
print(l)


def possible(n, money):
	if n * B > nB:
		money = money - pB * (n * B - nB)
	if n * C > nC:
		money = money - pC * (n * C - nC)
	if n * S > nS:
		money = money - pS * (n * S - nS)
	if money < 0:
		return False
	else:
		return True
hb = str(input())
B = hb.count('B')
C = hb.count('C')
S = hb.count('S')
(nB, nS, nC) = [int(k) for k in input().split()]
(pB, pS, pC) = [int(k) for k in input().split()]
money = int(input())
exceeded = False
i = 0
while not exceeded:
	r = money
	if 2 ** i * B > nB:
		r = r - pB * (2 ** i * B - nB)
	if 2 ** i * C > nC:
		r = r - pC * (2 ** i * C - nC)
	if 2 ** i * S > nS:
		r = r - pS * (2 ** i * S - nS)
	if r < 0:
		exceeded = True
		break
	i += 1
lo = 2 ** (i - 1)
high = 2 ** i
mid = int((lo + high) / 2)
while lo <= high:
	if possible(mid, money):
		lo = mid + 1
		mid = (lo + high) // 2
	else:
		high = mid - 1
		mid = (lo + high) // 2
print(lo - 1)


st = input()
B = st.count('B')
S = st.count('S')
C = st.count('C')
(b, s, c) = map(int, input().split())
(bp, sp, cp) = map(int, input().split())
r = int(input())
lm = 0
rm = int(1000000000000000.0) + 1
while rm - lm > 1:
	m = (rm + lm) // 2
	bb = max(m * B - b, 0)
	ss = max(m * S - s, 0)
	cc = max(m * C - c, 0)
	if bp * bb + ss * sp + cc * cp <= r:
		lm = m
	else:
		rm = m
print(lm)


st = input()
a = [int(i) for i in input().split()]
d = [int(i) for i in input().split()]
p = int(input())

def f(x):
	dop = 0
	if a[0] < b * x:
		dop += (b * x - a[0]) * d[0]
	if a[1] < s * x:
		dop += (s * x - a[1]) * d[1]
	if a[2] < c * x:
		dop += (c * x - a[2]) * d[2]
	return dop
(b, s) = (0, 0)
c = 0
for i in range(len(st)):
	if st[i] == 'B':
		b += 1
	if st[i] == 'S':
		s += 1
	if st[i] == 'C':
		c += 1
l = -1
r = 10 ** 18
while r - l > 1:
	mid = (r + l) // 2
	if f(mid) <= p:
		l = mid
	else:
		r = mid
print(l)


def scan(type):
	return list(map(type, input().split()))
(string,) = scan(str)
(nb, ns, nc) = scan(int)
(pb, ps, pc) = scan(int)
(r,) = scan(int)
b = s = c = 0
for char in string:
	if char == 'B':
		b += 1
	if char == 'S':
		s += 1
	if char == 'C':
		c += 1
ans = 0
if b == 0:
	nb = 0
if s == 0:
	ns = 0
if c == 0:
	nc = 0
while True:
	if b:
		qb = nb // b
	else:
		qb = 100
	if s:
		qs = ns // s
	else:
		qs = 100
	if c:
		qc = nc // c
	else:
		qc = 100
	qmin = min(qb, qs)
	qmin = min(qmin, qc)
	ans += qmin
	nb -= qmin * b
	ns -= qmin * s
	nc -= qmin * c
	if nb == 0 and ns == 0 and (nc == 0):
		break
	if nb < b:
		r -= pb * (b - nb)
		nb = b
	if ns < s:
		r -= ps * (s - ns)
		ns = s
	if nc < c:
		r -= pc * (c - nc)
		nc = c
	if r < 0:
		break
if r > 0:
	cost = b * pb + s * ps + c * pc
	ans += r // cost
print(ans)


def main():

	def list_from_input():
		return list(map(int, input().split()))
	hamburger = input()
	(nb, ns, nc) = list_from_input()
	(pb, ps, pc) = list_from_input()
	money = int(input())
	(tb, ts, tc) = (hamburger.count('B'), hamburger.count('S'), hamburger.count('C'))

	def is_possible(amount):
		cost_b = max(0, (tb * amount - nb) * pb)
		cost_s = max(0, (ts * amount - ns) * ps)
		cost_c = max(0, (tc * amount - nc) * pc)
		return cost_b + cost_c + cost_s <= money

	def max_possible():
		l = 0
		r = 10 ** 14
		while r > l:
			m = (l + r) // 2
			if is_possible(m):
				l = m + 1
			else:
				r = m
		return l - 1
	print(max_possible())
main()


from math import *
s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
(rb, rs, rc) = (0, 0, 0)
for i in s:
	if i == 'B':
		rb += 1
	elif i == 'S':
		rs += 1
	else:
		rc += 1
ans = 0
lo = 0
rh = 1e+18
while lo < rh:
	md = lo + (rh - lo + 1) // 2
	pr = pb * max(0, md * rb - nb) + ps * max(0, md * rs - ns) + pc * max(0, md * rc - nc)
	if pr <= r:
		lo = md
	else:
		rh = md - 1
else:
	ans = rh
print(int(ans))


import sys
import collections
s = sys.stdin.readline().strip()
(nb, ns, nc) = list(map(int, sys.stdin.readline().strip().split(' ')))
(pb, ps, pc) = list(map(int, sys.stdin.readline().strip().split(' ')))
r = int(sys.stdin.readline().strip())

def solve(nb, ns, nc, c, r, tc):
	if nb == 0 and ns == 0 and (nc == 0):
		return r // tc
	(mb, ms, mc) = (0, 0, 0)
	if nb < c['B']:
		mb = c['B'] - nb
	if ns < c['S']:
		ms = c['S'] - ns
	if nc < c['C']:
		mc = c['C'] - nc
	cost = mb * pb + ms * ps + mc * pc
	if r >= cost:
		nb += mb
		nc += mc
		ns += ms
		return 1 + solve(nb - c['B'], ns - c['S'], nc - c['C'], c, r - cost, tc)
	else:
		return 0
c = collections.Counter(s)
if c['B'] == 0:
	nb = 0
if c['C'] == 0:
	nc = 0
if c['S'] == 0:
	ns = 0
tc = pb * c['B'] + pc * c['C'] + ps * c['S']
print(solve(nb, ns, nc, c, r, tc))


import sys
from bisect import *
INF = 1000000000.0
Input = []
MOD = 1000000007

def Out(x):
	sys.stdout.write(str(x) + '\n')

def In():
	return sys.stdin.readline().strip()

def inputGrab():
	for line in sys.stdin:
		Input.extend(map(str, line.strip().split()))
Nb = 0
Ns = 0
Nc = 0
bCnt = 0
sCnt = 0
cCnt = 0
burgerCost = 0
sausageCost = 0
cheeseCost = 0
budget = 0

def canTake(val):
	cost = max(0, burgerCost * (val * bCnt - Nb))
	cost += max(0, sausageCost * (val * sCnt - Ns))
	cost += max(0, cheeseCost * (val * cCnt - Nc))
	if cost <= budget:
		return 1
	return 0

def binarySearch(lo, hi):
	ans = int()
	while lo <= hi:
		mid = (lo + hi) // 2
		if canTake(mid):
			ans = max(ans, mid)
			lo = mid + 1
		else:
			hi = mid - 1
	return ans

def main():
	Str = input()
	global Nb
	global Ns
	global Nc
	global burgerCost
	global sausageCost
	global cheeseCost
	global budget
	global bCnt
	global sCnt
	global cCnt
	(Nb, Ns, Nc) = map(int, input().strip().split())
	(burgerCost, sausageCost, cheeseCost) = map(int, input().strip().split())
	budget = int(input())
	bCnt = Str.count('B')
	sCnt = Str.count('S')
	cCnt = Str.count('C')
	print(binarySearch(0, int(1e+17)))
main()


from sys import stdin, stdout, stderr
hamber = input()
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
curb = hamber.count('B')
curs = hamber.count('S')
curc = hamber.count('C')
l = 0
rr = 10000000000001
ans = 0
while l <= rr:
	mid = (l + rr) // 2
	curr = r
	zb = mid * curb
	zs = mid * curs
	zc = mid * curc
	zb = max(0, zb - b)
	zs = max(0, zs - s)
	zc = max(0, zc - c)
	curr -= zb * pb + zc * pc + zs * ps
	if curr >= 0:
		ans = mid
		l = mid + 1
	else:
		rr = mid - 1
print(ans)


from math import ceil
s = input()
(a, b, c) = map(int, input().split())
(ca, cb, cc) = map(int, input().split())
r = int(input())
na = s.count('B')
nb = s.count('S')
nc = s.count('C')
mini = 0
maxi = 2 * r * a * b

def can(kol):
	(ka, kb, kc) = (0, 0, 0)
	if na > 0:
		ka = ceil(kol * na - a) * ca
	if nb > 0:
		kb = ceil(kol * nb - b) * cb
	if nc > 0:
		kc = ceil(kol * nc - c) * cc
	return max(ka, 0) + max(kb, 0) + max(kc, 0) <= r
while mini < maxi:
	mid = (mini + maxi) // 2
	mid += 1
	if can(mid):
		mini = mid
	else:
		maxi = mid - 1
print(mini)


def result():
	input1 = input()
	list1 = [0] * 3
	for i in input1:
		if i == 'B':
			list1[0] += 1
		elif i == 'S':
			list1[1] += 1
		elif i == 'C':
			list1[2] += 1
	for i in range(3):
		if list1[i] == 0:
			list1[i] = 1e-19
	list2 = list(map(int, input().split()))
	list3 = list(map(int, input().split()))
	m = int(input())
	(temp_nums, temp_res) = ([0] * 3, [0] * 3)
	for i in range(3):
		temp_nums[i] = int(list2[i] / list1[i])
		if list1[i] < 1:
			temp_res[i] = 0
		else:
			temp_res[i] = list2[i] % list1[i]
	ttt = [0.1, 0.2, 0.3]
	for i in range(3):
		ttt[i] += temp_nums[i]
	if input1 == '':
		print(0)
		return 0
	temp = sorted(ttt)
	minId = ttt.index(temp[0])
	medId = ttt.index(temp[1])
	maxId = ttt.index(temp[2])
	aaa = ((temp_nums[medId] - temp_nums[minId]) * list1[minId] - temp_res[minId]) * list3[minId]
	bbb = (temp_nums[maxId] - temp_nums[medId]) * (list3[medId] * list1[medId] + list3[minId] * list1[minId]) - list3[medId] * temp_res[medId]
	ccc1 = list3[medId] * list1[medId] + list3[minId] * list1[minId] + list3[maxId] * list1[maxId] - list3[maxId] * temp_res[maxId]
	if aaa >= m:
		aaa1 = (list1[minId] - temp_res[minId]) * list3[minId]
		if aaa1 > m:
			print(temp_nums[minId])
			return 0
		else:
			print(temp_nums[minId] + int((m - aaa1) / (list3[minId] * list1[minId])) + 1)
			return 0
	elif bbb >= m:
		bbb1 = list3[medId] * list1[medId] + list3[minId] * list1[minId] - list3[medId] * temp_res[medId]
		if bbb1 > m - aaa:
			print(temp_nums[medId])
			return 0
		else:
			print(temp_nums[medId] + int((m - aaa - bbb1) / (list3[medId] * list1[medId] + list3[minId] * list1[minId])) + 1)
	elif ccc1 > m - aaa - bbb:
		print(temp_nums[maxId])
		return 0
	else:
		print(temp_nums[maxId] + int((m - aaa - bbb - ccc1) / (list3[medId] * list1[medId] + list3[minId] * list1[minId] + list3[maxId] * list1[maxId])) + 1)
result()


import sys
sys.setrecursionlimit(100000)
recipe = sys.stdin.readline().strip()
n = [int(x) for x in sys.stdin.readline().strip().split()]
p = [int(x) for x in sys.stdin.readline().strip().split()]
r = int(sys.stdin.readline())
d = [0, 0, 0]
dic = {'B': 0, 'S': 1, 'C': 2}
for i in recipe:
	d[dic[i]] += 1
high = r + n[0] * p[0] + n[1] * p[1] + n[2] * p[2]
low = 0
while high >= low:
	mid = (high + low) // 2
	su = 0
	for j in range(3):
		su += (d[j] * mid - n[j]) * p[j] if mid * d[j] > n[j] else 0
	if r - su < 0:
		high = mid - 1
	if r - su > 0:
		low = mid + 1
	if r == su or mid == 0 or (mid == low and mid == high):
		break
if r - su < 0:
	print(mid - 1)
else:
	print(mid)


from collections import Counter
s = list(input())
dic = Counter(s)
c = [dic['B'], dic['S'], dic['C']]
n = list(map(int, input().split()))
p = list(map(int, input().split()))
r = int(input())
(a, b) = (0, 10 ** 15)
while a + 1 < b:
	mid = (a + b) // 2
	tot = 0
	for i in range(3):
		tot += max(0, mid * c[i] - n[i]) * p[i]
	if tot <= r:
		a = mid
	else:
		b = mid
print(a)


import sys
import math
import collections
import bisect

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	bread = list(input())
	(nb, ns, nc) = get_ints()
	(pb, ps, pc) = get_ints()
	given = int(input())
	(cb, cs, cc) = (bread.count('B'), bread.count('S'), bread.count('C'))
	l = 0
	r = 10 ** 14
	c = 0
	while l <= r:
		mid = (l + r) // 2
		cost = max(0, mid * cb - nb) * pb + max(0, mid * cs - ns) * ps + max(0, mid * cc - nc) * pc
		if cost <= given:
			l = mid
		else:
			r = mid
		c += 1
		if c >= 100:
			break
	print(l)


strng = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
(b, s, c) = (0, 0, 0)
for i in strng:
	if i == 'B':
		b += 1
	elif i == 'S':
		s += 1
	elif i == 'C':
		c += 1
	else:
		1 // 0
ans = 0
while nb > 0 or ns > 0 or nc > 0:
	cost = 0
	if nb < b:
		cost += (b - nb) * pb
		nb = 0
	else:
		nb -= b
	if ns < s:
		cost += (s - ns) * ps
		ns = 0
	else:
		ns -= s
	if nc < c:
		cost += (c - nc) * pc
		nc = 0
	else:
		nc -= c
	if cost <= r:
		ans += 1
		r -= cost
	else:
		break
	if (nb == 0 or b == 0) and (nc == 0 or c == 0) and (ns == 0 or s == 0):
		break
cost = b * pb + s * ps + c * pc
ans += r // cost
print(ans)


def solution():
	s = input()
	(nb, ns, nc) = map(int, input().split())
	(pb, ps, pc) = map(int, input().split())
	p = int(input())
	l = 0
	r = 10 ** 14
	B = 0
	S = 0
	C = 0
	for i in range(len(s)):
		if s[i] == 'B':
			B += 1
		elif s[i] == 'C':
			C += 1
		else:
			S += 1
	while l < r - 1:
		m = (l + r) // 2
		rp = max(0, B * m - nb) * pb + max(0, S * m - ns) * ps + max(0, C * m - nc) * pc
		if rp <= p:
			l = m
		else:
			r = m
	print(l)
solution()


def f(b, s, c, nb, ns, nc, pb, ps, pc, r, x):
	(B, S, C) = (max(x * b - nb, 0), max(x * s - ns, 0), max(x * c - nc, 0))
	tot = B * pb + S * ps + C * pc
	return tot <= r
g = input()
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
r = int(input())
(b, s, c) = (0, 0, 0)
for i in g:
	if i == 'B':
		b += 1
	elif i == 'C':
		c += 1
	else:
		s += 1
(lo, hi) = (0, 10 ** 15)
while lo < hi - 1:
	m = (lo + hi) // 2
	if f(b, s, c, nb, ns, nc, pb, ps, pc, r, m):
		lo = m
	else:
		hi = m - 1
if f(b, s, c, nb, ns, nc, pb, ps, pc, r, hi):
	print(hi)
else:
	print(lo)


import sys
import math
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
St = ns()
ind = {'B': 0, 'S': 1, 'C': 2}
N = na()
P = na()
R = ni()
n = [0, 0, 0]
c1 = 0
for c in St:
	n[ind[c]] += 1
	c1 += P[ind[c]]
ans = 10 ** 5
for i in range(len(N)):
	if n[i] == 0:
		N[i] = 0
	else:
		ans = min(ans, math.floor(N[i] / n[i]))
for i in range(len(N)):
	N[i] -= ans * n[i]
while sum(N) != 0:
	for i in range(len(N)):
		if N[i] < n[i]:
			amt = (n[i] - N[i]) * P[i]
			if R < amt:
				print(ans)
				exit(0)
			else:
				N[i] = n[i]
				R -= amt
	ans += 1
	for i in range(len(N)):
		N[i] -= n[i]
ans += math.floor(R / c1)
print(ans)


s = list(input())
a = [s.count(c) for c in 'BSC']
n = list(map(int, input().split()))
p = list(map(int, input().split()))
t = int(input())
l = 0
r = 10 ** 13
while l + 1 < r:
	mid = (l + r) // 2
	c = 0
	for i in range(3):
		c += p[i] * max(a[i] * mid - n[i], 0)
	if c > t:
		r = mid
	else:
		l = mid
print(l)


s = input()
(a, b, c) = map(int, input().split())
(ca, cb, cc) = map(int, input().split())
r = int(input())
apu = s.count('B')
bpu = s.count('S')
cpu = s.count('C')

def calc(n):
	return max((n * apu - a) * ca, 0) + max((n * bpu - b) * cb, 0) + max((n * cpu - c) * cc, 0)
mx = r
mn = 0
mid = (mn + mx) // 2
for i in range(mx.bit_length() + 1):
	v = calc(mid)
	if v <= r:
		mn = mid
	else:
		mx = mid
	mid = (mn + mx) // 2
while calc(mid) <= r:
	mid += 1
print(mid - 1)


s = input()
rnB = s.count('B')
rnS = s.count('S')
rnC = s.count('C')
(nB, nS, nC) = map(int, input().split())
(pB, pS, pC) = map(int, input().split())
r = int(input())

def get_cost(n):
	B = (rnB * n - nB) * pB
	S = (rnS * n - nS) * pS
	C = (rnC * n - nC) * pC
	return sum([x for x in [B, S, C] if x > 0])

def binary_search():
	low = 0
	high = 10 ** 20
	mid = 0
	while low <= high:
		mid = (high + low) // 2
		p = get_cost(mid)
		if p < r:
			low = mid + 1
		else:
			high = mid - 1
	return mid
x = binary_search()
if get_cost(x + 1) <= r:
	print(x + 1)
elif get_cost(x) <= r:
	print(x)
else:
	print(x - 1)


from sys import stdin
import sys
from collections import defaultdict
import copy
sys.setrecursionlimit(15000)

def get_count(string):
	b = 0
	s = 0
	c = 0
	for i in string:
		if i == 'B':
			b += 1
		elif i == 'S':
			s += 1
		else:
			c += 1
	return [b, s, c]

def check(num, one, narr, parr, r):
	total_b = num * one[0]
	total_s = num * one[1]
	total_c = num * one[2]
	total_b -= narr[0]
	total_s -= narr[1]
	total_c -= narr[2]
	if total_b > 0:
		r -= total_b * parr[0]
	if total_s > 0:
		r -= total_s * parr[1]
	if total_c > 0:
		r -= total_c * parr[2]
	if r >= 0:
		return True
	else:
		return False
string = stdin.readline().strip()
one = get_count(string)
narr = list(map(int, stdin.readline().split()))
parr = list(map(int, stdin.readline().split()))
r = int(stdin.readline().strip())
start = 0
end = 10 ** 18
while start <= end:
	if start == end:
		if check(start, one, narr, parr, r):
			pass
		else:
			start -= 1
		break
	mid = (start + end) // 2
	if check(mid, one, narr, parr, r):
		start = mid + 1
	else:
		end = mid - 1
if check(start, one, narr, parr, r):
	print(start)
else:
	print(max(start - 1, 0))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil, cos
mod = 10 ** 9 + 7
for _ in range(1):
	recipe = input()
	required = {'B': 0, 'S': 0, 'C': 0}
	for i in recipe:
		if i not in required:
			required[i] = 1
		else:
			required[i] += 1
	items = 'BSC'
	initial_count = {i: value for (i, value) in zip(items, list(map(int, input().split())))}
	costs = {i: value for (i, value) in zip(items, list(map(int, input().split())))}
	money = int(input())
	burger_count = 0
	start = -1
	end = pow(10, 18)
	while start < end - 1:
		mid = start + (end - start) // 2
		current_cost = costs['B'] * max(0, required['B'] * mid - initial_count['B']) + costs['S'] * max(0, required['S'] * mid - initial_count['S']) + costs['C'] * max(0, required['C'] * mid - initial_count['C'])
		if current_cost <= money:
			burger_count = mid
			start = mid
		else:
			end = mid
	print(burger_count)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
active = [0, 0, 0]
req_per = [0, 0, 0]
n = [nb, ns, nc]
p = [pb, ps, pc]
m = {'B': 0, 'S': 1, 'C': 2}
for i in s:
	if i == 'B':
		active[0] = 1
		req_per[0] += 1
	elif i == 'S':
		active[1] = 1
		req_per[1] += 1
	elif i == 'C':
		active[2] = 1
		req_per[2] += 1
final = 0
cost_per = req_per[0] * pb + req_per[1] * ps + req_per[2] * pc
while True:
	bool1 = True
	for i in m:
		if active[m[i]]:
			if n[m[i]] == 0:
				bool1 = bool1 and True
			else:
				bool1 = bool1 and False
	if bool1:
		final += r // cost_per
		break
	else:
		cost = 0
		for i in m:
			if active[m[i]]:
				if not req_per[m[i]] <= n[m[i]]:
					cost += (req_per[m[i]] - n[m[i]]) * p[m[i]]
					n[m[i]] += req_per[m[i]] - n[m[i]]
		if cost == 0:
			final += 1
			for i in m:
				if active[m[i]]:
					n[m[i]] += -req_per[m[i]]
		elif cost <= r:
			r += -cost
			final += 1
			for i in m:
				if active[m[i]]:
					n[m[i]] += -req_per[m[i]]
		else:
			break
print(final)


pattern = str(input())
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
money = int(input())
total_b = 0
total_c = 0
total_s = 0
for _ in range(0, len(pattern)):
	if pattern[_] == 'B':
		total_b = total_b + 1
	elif pattern[_] == 'S':
		total_s = total_s + 1
	elif pattern[_] == 'C':
		total_c = total_c + 1
start = 0
end = 10000000000000
ans = 0
while end - start > 1:
	search = (start + end) // 2
	b_require = search * total_b - nb if search * total_b > nb else 0
	s_require = search * total_s - ns if search * total_s > ns else 0
	c_require = search * total_c - nc if search * total_c > nc else 0
	total_cost = b_require * pb + s_require * ps + c_require * pc
	if total_cost > money:
		end = search
	elif total_cost < money:
		start = search
	else:
		start = search
		break
flag = 0
b_require = end * total_b - nb if end * total_b > nb else 0
s_require = end * total_s - ns if end * total_s > ns else 0
c_require = end * total_c - nc if end * total_c > nc else 0
total_cost = b_require * pb + s_require * ps + c_require * pc
if total_cost <= money:
	flag = 1
	print(end)
b_require = start * total_b - nb if start * total_b > nb else 0
s_require = start * total_s - ns if start * total_s > ns else 0
c_require = start * total_c - nc if start * total_c > nc else 0
total_cost = b_require * pb + s_require * ps + c_require * pc
if total_cost <= money and flag == 0:
	flag = 1
	print(start)
if flag == 0:
	print('0')


strs = input()
rb = 0
rs = 0
rc = 0
for i in strs:
	if i == 'B':
		rb += 1
	if i == 'S':
		rs += 1
	if i == 'C':
		rc += 1
temparr = input()
temparr = temparr.split()
kb = int(temparr[0])
ks = int(temparr[1])
kc = int(temparr[2])
temparr = input()
temparr = temparr.split()
cb = int(temparr[0])
cs = int(temparr[1])
cc = int(temparr[2])
costofburger = cb * rb + rs * cs + rc * cc
ruble = int(input())
ans = 0
times = 0
while True:
	if kb >= rb and kc >= rc and (ks >= rs):
		ans += 1
		kb -= rb
		kc -= rc
		ks -= rs
		continue
	lackb = 0
	lacks = 0
	lackc = 0
	if rb >= kb:
		lackb = rb - kb
		kb = 0
	if rc >= kc:
		lackc = rc - kc
		kc = 0
	if rs >= ks:
		lacks = rs - ks
		ks = 0
	if ks > rs:
		ks -= rs
	if kb > rb:
		kb -= rb
	if kc > rc:
		kc -= rc
	costruble = lackb * cb + lacks * cs + lackc * cc
	if ruble >= costruble:
		ans += 1
		ruble -= costruble
		if kb == 0 and ks == 0 and (kc == 0) or (ks == 0 and kc == 0 and (rb == 0)) or (kb == 0 and ks == 0 and (rc == 0)) or (kb == 0 and rs == 0 and (kc == 0)) or (kb == 0 and rs == 0 and (rc == 0)) or (rb == 0 and rs == 0 and (kc == 0)) or (ks == 0 and rb == 0 and (rc == 0)):
			break
		flag = 1
		continue
	break
ans += ruble // costofburger
print(ans)


f = {'B': 0, 'S': 0, 'C': 0}
for c in input():
	f[c] = f.get(c, 0) + 1
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
tot = int(input())

def can(x):
	nb = f['B'] * x - b
	ns = f['S'] * x - s
	nc = f['C'] * x - c
	nb = max(nb, 0)
	ns = max(ns, 0)
	nc = max(nc, 0)
	cost = nb * pb + ns * ps + nc * pc
	return tot >= cost
l = 0
r = int(1000000000000000.0)
ans = 0
while l <= r:
	mid = (l + r) // 2
	if can(mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


st = input()
req = [st.count(i) for i in 'BSC']
k = map(int, input().split(' '))
current = [i if j > 0 else 0 for (i, j) in zip(k, req)]
k = map(int, input().split(' '))
cost = [i if j > 0 else 0 for (i, j) in zip(k, req)]
r = int(input())
total = 0
while sum(current) > 0:
	r -= sum((max(0, r - a) * c for (r, a, c) in zip(req, current, cost)))
	current = [max(0, c - r) for (r, c) in zip(req, current)]
	if r < 0:
		break
	total += 1
tc = sum((r * c for (r, c) in zip(req, cost)))
total += max(0, r // tc)
print(total)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())

def cost(x):
	all_bread = cb * x
	all_saus = cs * x
	all_cheese = cc * x
	ans = max(0, all_bread - nb) * pb + max(0, all_cheese - nc) * pc + max(0, all_saus - ns) * ps
	return ans <= r
(cb, cc, cs) = (0, 0, 0)
for i in range(len(s)):
	if s[i] == 'S':
		cs += 1
	elif s[i] == 'B':
		cb += 1
	else:
		cc += 1
l = 0
right = 100000000000000.0
while l < right:
	m = (l + right) // 2
	if cost(m):
		l = m + 1
	else:
		right = m
print(int(l - 1))


w = input()
br = len(list(filter(lambda x: x == 'B', w)))
sr = len(list(filter(lambda x: x == 'S', w)))
cr = len(list(filter(lambda x: x == 'C', w)))
(b, s, c) = map(int, input().split())
(bc, sc, cc) = map(int, input().split())
m = int(input())

def ok(x):
	tm = m
	tm -= max(0, br * x - b) * bc
	tm -= max(0, sr * x - s) * sc
	tm -= max(0, cr * x - c) * cc
	return tm >= 0
lo = 0
hi = 100000000000000
while lo < hi:
	mi = (lo + hi + 1) // 2
	if ok(mi):
		lo = mi
	else:
		hi = mi - 1
print(lo)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def bs(f, mi, ma):
	mm = -1
	while ma > mi:
		mm = (ma + mi) // 2
		if f(mm):
			mi = mm + 1
		else:
			ma = mm
	if f(mm):
		return mm + 1
	return mm

def main():
	t = 1
	rr = []
	for _ in range(t):
		s = S()
		n = LI()
		p = LI()
		r = I()
		c = collections.Counter(s)
		a = [c['B'], c['S'], c['C']]

		def f(i):
			t = 0
			for j in range(3):
				t += max(0, a[j] * i - n[j]) * p[j]
			return t <= r
		k = bs(f, 0, 10 ** 14)
		rr.append(k - 1)
	return JA(rr, '\n')
print(main())


import math as mt
import collections as cc
I = lambda : list(map(int, input().split()))
s = list(input())
c = [s.count('B'), s.count('S'), s.count('C')]
n = I()
p = I()
(am,) = I()
a = 0
b = 10 ** 16
while a + 1 < b:
	x = (a + b) // 2
	y = sum((max(0, x * c[i] - n[i]) * p[i] for i in range(3)))
	if y <= am:
		a = x
	else:
		b = x
print(a)


from collections import Counter

def check_possible(b, s, c, r):
	return r - b - s - c >= 0
recipe = input()
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
recipe = Counter(recipe)
low = 0
high = r + 100
while low < high:
	mid = (low + high + 1) // 2
	money_b = max((mid * recipe['B'] - b) * pb, 0)
	money_s = max((mid * recipe['S'] - s) * ps, 0)
	money_c = max((mid * recipe['C'] - c) * pc, 0)
	if check_possible(money_b, money_s, money_c, r):
		low = mid
	else:
		high = mid - 1
print(low)


lecept = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
money = int(input())
b = lecept.count('B')
s = lecept.count('S')
c = lecept.count('C')

def need(m):
	need_b = m * b
	need_s = m * s
	need_c = m * c
	need_buy_b = need_b - n[0]
	need_buy_s = need_s - n[1]
	need_buy_c = need_c - n[2]
	need_money = max(need_buy_b, 0) * p[0] + max(need_buy_s, 0) * p[1] + max(need_buy_c, 0) * p[2]
	if need_money > money:
		return False
	else:
		return True

def big_ben():
	l = 0
	r = 10 ** 13
	while l + 1 < r:
		m = (l + r) // 2
		if need(m):
			l = m
		else:
			r = m
	return l
print(big_ben())


s = input()
n = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
item = ['B', 'S', 'C']
r = int(input())
freq = {'B': 0, 'S': 0, 'C': 0}
for i in s:
	try:
		freq[i] += 1
	except:
		freq[i] = 1
count = 0
frac = 101
for i in range(3):
	if freq[item[i]]:
		frac = min(frac, n[i] // freq[item[i]])
count += frac
for i in range(3):
	n[i] -= frac * freq[item[i]]
frac = 1
for i in range(3):
	if freq[item[i]]:
		frac = max(frac, n[i] // freq[item[i]])
sufficient = True
j = 1
while sufficient and j <= frac:
	for i in range(3):
		if n[i] < j * freq[item[i]]:
			if r < p[i] * (j * freq[item[i]] - n[i]):
				sufficient = False
				break
			else:
				r -= (j * freq[item[i]] - n[i]) * p[i]
				n[i] = j * freq[item[i]]
	j += 1
if sufficient:
	count += frac
	total = 0
	for i in range(3):
		n[i] -= frac * freq[item[i]]
		total += freq[item[i]] * p[i]
	count += r // total
	r -= total * (r // total)
	total = 0
	for i in range(3):
		if freq[item[i]]:
			total += (freq[item[i]] - n[i]) * p[i]
	if total <= r:
		count += 1
else:
	frac = 101
	for i in range(3):
		if freq[item[i]]:
			frac = min(frac, n[i] // freq[item[i]])
	count += frac
print(count)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def MoneyNeeded(x):
	money_b = max(0, x * recipe[b] - have_b) * price_b
	money_s = max(0, x * recipe[s] - have_s) * price_s
	money_c = max(0, x * recipe[c] - have_c) * price_c
	return money_b + money_s + money_c
(b, s, c) = ('B', 'S', 'C')
recipe = Counter(input())
(have_b, have_s, have_c) = value()
(price_b, price_s, price_c) = value()
money = Int()
low = 0
high = 10 ** 13 + 1
while low <= high:
	mid = low + (high - low) // 2
	if MoneyNeeded(mid) <= money and MoneyNeeded(mid + 1) > money:
		print(mid)
		exit()
	elif MoneyNeeded(mid) <= money:
		low = mid + 1
	else:
		high = mid - 1


s1 = input()
nb = 0
ns = 0
nc = 0
for i in s1:
	if i == 'B':
		nb += 1
	elif i == 'C':
		nc += 1
	else:
		ns += 1
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
tr = int(input())
tc = nb * pb + nc * pc + ns * ps
l = 0
r = int(1000000000000.0) + 1
l = -1
r = int(10000000000000.0 + 1)
while l < r - 1:
	m = (l + r) // 2
	if max(0, pb * (nb * m - b)) + max(0, pc * (nc * m - c)) + max(0, ps * (ns * m - s)) <= tr:
		ans = m
		l = m
	else:
		r = m
print(ans)


from operator import itemgetter
s = input()
ni = list(map(int, input().split()))
pi = list(map(int, input().split()))
r = int(input())
numi = [s.count('B'), s.count('S'), s.count('C')]

def check(num):
	global r
	r2 = r
	for i in range(3):
		temp = min(ni[i] - num * numi[i], 0)
		r2 += temp * pi[i]
	if r2 >= 0:
		return True
	else:
		return False
high = 10 ** 13
low = 0
mid = (high + low) // 2
while high >= low:
	if check(mid):
		low = mid + 1
	else:
		high = mid - 1
	mid = (high + low) // 2
print(mid)


def check(x):
	b = max(0, x * cb - nb) * pb
	c = max(0, x * cc - nc) * pc
	s = max(0, x * cs - ns) * ps
	temp = b + c + s
	if temp <= money:
		return True
	else:
		return False
recipe = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
money = int(input())
(cb, cs, cc) = (0, 0, 0)
for i in range(len(recipe)):
	if recipe[i] == 'B':
		cb += 1
	elif recipe[i] == 'S':
		cs += 1
	elif recipe[i] == 'C':
		cc += 1
l = 1
r = 3333333333333
x = 0
flag = False
while l <= r:
	mid = (l + r) // 2
	if check(mid):
		l = mid + 1
		flag = True
	else:
		r = mid - 1
		flag = False
if flag is False:
	mid -= 1
	print(mid)
else:
	print(mid)


def can_have(num, d, money, cost):
	b = d['B'] * num - arr[0]
	if b < 0:
		b = 0
	s = d['S'] * num - arr[1]
	if s < 0:
		s = 0
	c = d['C'] * num - arr[2]
	if c < 0:
		c = 0
	costb = cost[0] * b
	money -= costb
	if money < 0:
		return False
	costs = cost[1] * s
	money -= costs
	if money < 0:
		return False
	costc = cost[2] * c
	money -= costc
	if money < 0:
		return False
	return True
s = input()
arr = [int(x) for x in input().split()]
cost = [int(x) for x in input().split()]
money = int(input())
d = {'B': 0, 'S': 0, 'C': 0}
for ele in s:
	if ele in d:
		d[ele] += 1
	else:
		d[ele] = 1
(l, r) = (1, 10000000000000000)
curr_c = 0
last_ans = 0
while l <= r:
	mid = (l + r) // 2
	p = can_have(mid, d, money, cost)
	if p:
		last_ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(last_ans)


def price(x):
	return max(0, x * req[0] - a[0]) * cost[0] + max(0, x * req[1] - a[1]) * cost[1] + max(0, x * req[2] - a[2]) * cost[2]

def main():
	low = 0
	high = r + 1000
	ans = 0
	while low <= high:
		mid = (low + high) // 2
		c = price(mid)
		if c == r:
			print(mid)
			return
		elif c > r:
			high = mid - 1
		else:
			low = mid + 1
			ans = mid
	print(ans)
	return
string = str(input())
a = list(map(int, input().split()))
cost = list(map(int, input().split()))
r = int(input())
req = [0, 0, 0]
for i in range(len(string)):
	if string[i] == 'B':
		req[0] += 1
	elif string[i] == 'S':
		req[1] += 1
	else:
		req[2] += 1
main()


recipe = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
all_money = int(input())
have = {}
for c in recipe:
	if not c in have:
		have[c] = 1
	else:
		have[c] += 1

def can(x):
	(b, s, c) = (0, 0, 0)
	cur_money = 0
	if 'B' in have:
		b = have['B'] * x - nb
	if 'S' in have:
		s = have['S'] * x - ns
	if 'C' in have:
		c = have['C'] * x - nc
	if b > 0:
		cur_money += b * pb
	if s > 0:
		cur_money += s * ps
	if c > 0:
		cur_money += c * pc
	if cur_money <= all_money:
		return True
	return False
(l, r) = (0, int(1e+18))
while l != r:
	mid = l + r >> 1
	if can(mid):
		l = mid + 1
	else:
		r = mid
while can(r + 1):
	r += 1
while not can(r):
	r -= 1
print(r)


from collections import Counter
(line1, line2, line3, line4) = (input(), input(), input(), input())
hamburg_counter = Counter(line1)
(nb, ns, nc) = [int(i) for i in line2.split()]
(pb, ps, pc) = [int(i) for i in line3.split()]
total_money = int(line4)
if 1 <= nb <= 100 and 1 <= ns <= 100 and (1 <= nc <= 100) and (1 <= pb <= 100) and (1 <= ps <= 100) and (1 <= pc <= 100) and (1 <= total_money <= 1000000000000.0):
	count = 0
	(x, y, z) = (hamburg_counter['B'], hamburg_counter['S'], hamburg_counter['C'])
	money_per_hamburg = x * pb + y * ps + z * pc
	if x == 0:
		nb = 0
	if y == 0:
		ns = 0
	if z == 0:
		nc = 0
	(cost_b, cost_s, cost_c) = (0, 0, 0)
	while nb > 0 or ns > 0 or nc > 0:
		nb -= x
		ns -= y
		nc -= z
		if nb < 0:
			cost_b = -nb * pb
		if ns < 0:
			cost_s = -ns * ps
		if nc < 0:
			cost_c = -nc * pc
		temp = total_money - (cost_b + cost_c + cost_s)
		if temp < 0:
			break
		count += 1
	total_money -= cost_b + cost_c + cost_s
	if total_money > 0:
		count += total_money // money_per_hamburg
	print(count)
else:
	print(0)


S = input()
(n1, n2, n3) = map(int, input().split())
(p1, p2, p3) = map(int, input().split())
r = int(input())
(m1, m2, m3) = (S.count('B'), S.count('S'), S.count('C'))
L = 0
R = 10000000000000
while L < R:
	mid = (L + R + 1) // 2
	fee = max(0, mid * m1 - n1) * p1 + max(0, mid * m2 - n2) * p2 + max(0, mid * m3 - n3) * p3
	if r >= fee:
		L = mid
	else:
		R = mid - 1
print(int(L))


D = {'B': 0, 'S': 0, 'C': 0}
str = input()
D['B'] = str.count('B')
D['S'] = str.count('S')
D['C'] = str.count('C')
P = {'B': 0, 'S': 0, 'C': 0}
(nb, ns, nc) = map(int, input().split())
N = {'B': nb, 'S': ns, 'C': nc}
(P['B'], P['S'], P['C']) = map(int, input().split())
r = int(input())
start = []
if D['B'] != 0:
	start.append(['B', nb // D['B']])
else:
	start.append(['B', int(1000000000000000.0)])
if D['S'] != 0:
	start.append(['S', ns // D['S']])
else:
	start.append(['S', int(1000000000000000.0)])
if D['C'] != 0:
	start.append(['C', nc // D['C']])
else:
	start.append(['C', int(1000000000000000.0)])
start = sorted(start, key=lambda fuck: fuck[1])
x = start[0][1]
m1 = ((start[1][1] - start[0][1]) * D[start[0][0]] - N[start[0][0]]) * P[start[0][0]]
if m1 > r:
	x = (r // P[start[0][0]] + N[start[0][0]]) // D[start[0][0]]
else:
	m2 = ((start[2][1] - start[0][1]) * D[start[0][0]] - N[start[0][0]]) * P[start[0][0]] + ((start[2][1] - start[1][1]) * D[start[1][0]] - N[start[1][0]]) * P[start[1][0]]
	if m2 > r:
		x = (r + N[start[0][0]] * P[start[0][0]] + N[start[1][0]] * P[start[1][0]]) // (D[start[0][0]] * P[start[0][0]] + D[start[1][0]] * P[start[1][0]])
	else:
		x = (r + N[start[0][0]] * P[start[0][0]] + N[start[1][0]] * P[start[1][0]] + N[start[2][0]] * P[start[2][0]]) // (D[start[0][0]] * P[start[0][0]] + D[start[1][0]] * P[start[1][0]] + D[start[2][0]] * P[start[2][0]])
print(x)


inf = 100000000000000
ch = input()
B = sum((x == 'B' for x in ch))
S = sum((x == 'S' for x in ch))
C = sum((x == 'C' for x in ch))
a = [int(x) for x in input().split()]
nb = a[0]
ns = a[1]
nc = a[2]
a = [int(x) for x in input().split()]
pb = a[0]
ps = a[1]
pc = a[2]
if B == 0:
	pb = 0
	B = 1
	nb = inf
if S == 0:
	ps = 0
	S = 1
	ns = inf
if C == 0:
	pc = 0
	C = 1
	nc = inf
R = int(input())
l = min(nb // B, ns // S, nc // C)
r = inf
ans = l

def ck(z):
	v = max(0, (B * z - nb) * pb) + max(0, (S * z - ns) * ps) + max(0, (C * z - nc) * pc)
	if v <= R:
		return 1
	return 0
while l <= r:
	mid = l + r >> 1
	if ck(mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd, log
from collections import deque
from bisect import bisect_left, bisect_right
s = input()
d = [s.count('B'), s.count('S'), s.count('C')]
l = 0
r = pow(2, 65)
a = list(map(int, input().split()))
z = list(map(int, input().split()))
t = int(input())
while l <= r:
	mid = (r + l) // 2
	f = 1
	n = t
	for i in range(3):
		if n < 0:
			f = 0
		w = max(mid * d[i] - a[i], 0)
		n -= w * z[i]
	if n < 0:
		f = 0
	if f == 1:
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


def make(recipe, nb, ns, nc, pb, ps, pc, r, nburg):
	(x, y, z) = (recipe['B'], recipe['S'], recipe['C'])
	(rb, rs, rc) = (max(nburg * x - nb, 0), max(nburg * y - ns, 0), max(nburg * z - nc, 0))
	if rb * pb + rs * ps + rc * pc <= r:
		return True
	else:
		return False
R = lambda : list(map(int, input().split()))
s = input()
freq = {'B': 0, 'S': 0, 'C': 0}
for i in s:
	freq[i] += 1
(nb, ns, nc) = R()
(pb, ps, pc) = R()
ri = int(input())
l = 0
r = 10 ** 14
ans = 0
while l <= r:
	mid = (l + r) // 2
	if make(freq, nb, ns, nc, pb, ps, pc, ri, mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


inStr = None
nList = None
pList = None
r = None
pptList = None

def calPrice(count, pt, n):
	price = pt * count - n
	return price if price > 0 else 0

def isEnough(count):
	needList = [calPrice(count, pt, n) for (pt, n) in zip(pptList, nList)]
	needMoney = sum([nl * pr for (nl, pr) in zip(needList, pList)])
	return needMoney <= r

def getCount(p, n, pt):
	return 0 if pt == 0 else (r // p + n) // pt

def maxCount():
	buyOne = [getCount(p, n, pt) for (p, n, pt) in zip(pList, nList, pptList)]
	return max(buyOne)

def bin_search(left, right):
	(low, high) = (left, right)
	while low <= high:
		mid = (low + high) // 2
		if isEnough(mid):
			low = mid + 1
		else:
			high = mid - 1
	return high
if '__main__' == __name__:
	inStr = input()
	nList = list(map(int, input().split()))
	pList = list(map(int, input().split()))
	r = int(input())
	pptList = [inStr.count(s) for s in 'BSC']
	print(bin_search(0, maxCount()))


_receipt = input()
_receipt = {'B': _receipt.count('B'), 'S': _receipt.count('S'), 'C': _receipt.count('C')}
_at_kitchen = list(map(int, input().split()))
_at_kitchen = {'B': _at_kitchen[0], 'S': _at_kitchen[1], 'C': _at_kitchen[2]}
_prices = list(map(int, input().split()))
_prices = {'B': _prices[0], 'S': _prices[1], 'C': _prices[2]}
_wallet = int(input())

def make_burger(receipt, at_kitchen, prices, wallet):
	burgers = 0
	enough_money = True
	third_part_burger = 0
	ingr_amount = sum(list((1 if x != 0 else 0 for x in receipt.values())))
	if wallet > 0:
		while enough_money:
			for ingr in ['B', 'S', 'C']:
				if receipt[ingr] == 0:
					at_kitchen[ingr] = 0
				if at_kitchen[ingr] < receipt[ingr] != 0:
					ingr_need = receipt[ingr] - at_kitchen[ingr]
					money_need = ingr_need * prices[ingr]
					if money_need <= wallet:
						wallet -= money_need
						at_kitchen[ingr] += ingr_need
					else:
						enough_money = False
				if at_kitchen[ingr] >= receipt[ingr] != 0:
					at_kitchen[ingr] = at_kitchen[ingr] - receipt[ingr]
					third_part_burger += 1
				if sum(at_kitchen.values()) == 0 and third_part_burger % ingr_amount == 0:
					enough_money = False
	burgers += third_part_burger // ingr_amount
	burger_cost = 0
	for ingr in ['B', 'S', 'C']:
		burger_cost += receipt[ingr] * prices[ingr]
	burgers += wallet // burger_cost
	return burgers
print(make_burger(_receipt, _at_kitchen, _prices, _wallet))


import math

def check(x):
	nb = x * need_b
	nc = x * need_c
	ns = x * need_s
	nb -= have_b
	nb = max(nb, 0)
	ns -= have_s
	ns = max(ns, 0)
	nc -= have_c
	nc = max(nc, 0)
	return have_money >= nb * price_b + nc * price_c + ns * price_s
s = input()
(need_b, need_c, need_s) = (0, 0, 0)
for i in s:
	if i == 'B':
		need_b += 1
	elif i == 'S':
		need_s += 1
	else:
		need_c += 1
(have_b, have_s, have_c) = map(int, input().split())
(price_b, price_s, price_c) = map(int, input().split())
have_money = int(input())
left = 0
right = 1000000000000000
while right - left > 1:
	mid = (left + right) // 2
	if check(mid):
		left = mid
	else:
		right = mid
if check(right):
	print(right)
else:
	print(left)


recipe = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
mn = int(input())
(tb, ts, tc) = (0, 0, 0)
for i in recipe:
	if i == 'B':
		tb += 1
	elif i == 'S':
		ts += 1
	else:
		tc += 1
(l, r, m) = (-1, 1e+18, 0)
while r - l > 1:
	m = (l + r) // 2
	(ndb, nds, ndc) = (tb * m, ts * m, tc * m)
	ndmn = max(0, (ndb - nb) * pb) + max(0, (nds - ns) * ps) + max(0, (ndc - nc) * pc)
	if ndmn <= mn:
		l = m
	else:
		r = m
print(int(l))


from bisect import *
import math
from collections import *
from heapq import *
from itertools import *
inf = 10 ** 18
mod = 10 ** 9 + 7
s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
cnt = Counter(s)
maxx = r
i = 0
l = 10000000000000
while i < l:
	mid = (i + l) // 2
	temp = max(0, cnt['B'] * mid - nb) * pb + max(0, cnt['C'] * mid - nc) * pc + max(0, cnt['S'] * mid - ns) * ps
	if r < temp:
		l = mid
	else:
		i = mid + 1
print(l - 1)


from collections import Counter
recipe = Counter(input())
(nb, ns, nc) = [int(x) for x in input().split()]
(vb, vs, vc) = [int(x) for x in input().split()]
rouble = int(input())
(rb, rs, rc) = (recipe['B'], recipe['S'], recipe['C'])
l = 0
r = int(10000000000000.0)
res = 0
while l <= r:
	m = (l + r) // 2
	need = vb * max(0, rb * m - nb) + vs * max(0, rs * m - ns) + vc * max(0, rc * m - nc)
	if rouble >= need:
		res = m
		l = m + 1
	else:
		r = m - 1
print(res)


(recipe, numbers, prices, r) = (input(), list(map(int, input().split())), list(map(int, input().split())), int(input()))
num = (recipe.count('B'), recipe.count('S'), recipe.count('C'))
(ans, pre, all) = (float('inf'), r, 0)
for i in range(3):
	try:
		ans = min(numbers[i] // num[i], ans)
	except:
		(ans, numbers[i]) = (0, 0)
for i in range(3):
	numbers[i] -= ans * num[i]
while (numbers[0] != 0 or numbers[1] != 0 or numbers[2] != 0) and r > 0:
	for i in range(3):
		if numbers[i] >= num[i]:
			numbers[i] -= num[i]
		else:
			ext = num[i] - numbers[i]
			numbers[i] = 0
			r -= ext * prices[i]
		if r < 0:
			exit(print(ans))
	ans += 1
for i in range(3):
	all += num[i] * prices[i]
print(ans + r // all)


z = input()
yb = z.count('B')
ys = z.count('S')
yc = z.count('C')
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
r = int(input())
b = nb // yb if yb != 0 else float('inf')
s = ns // ys if ys != 0 else float('inf')
c = nc // yc if yc != 0 else float('inf')
n = min(b, s, c)
nb -= n * yb
ns -= n * ys
nc -= n * yc
b = nb // yb if yb != 0 else float('inf')
s = ns // ys if ys != 0 else float('inf')
c = nc // yc if yc != 0 else float('inf')
if b <= s <= c:
	nb1 = nb + min(s - b, r // (pb * yb)) * yb
	r -= (nb1 - nb) * pb
	nb = nb1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nb1 = nb + min(c - b, r // (pb * yb + ps * ys)) * yb
	ns1 = ns + min(c - s, r // (pb * yb + ps * ys)) * ys
	r -= (nb1 - nb) * pb + (ns1 - ns) * ps
	nb = nb1
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
elif b <= c <= s:
	nb1 = nb + min(c - b, r // (pb * yb)) * yb
	r -= (nb1 - nb) * pb
	nb = nb1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nb1 = nb + min(s - b, r // (pb * yb + pc * yc)) * yb
	nc1 = nc + min(s - c, r // (pb * yb + pc * yc)) * yc
	r -= (nb1 - nb) * pb + (nc1 - nc) * pc
	nb = nb1
	nc = nc1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
elif s <= b <= c:
	ns1 = ns + min(b - s, r // (ps * ys)) * ys
	r -= (ns1 - ns) * ps
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nb1 = nb + min(c - b, r // (pb * yb + ps * ys)) * yb
	ns1 = ns + min(c - s, r // (pb * yb + ps * ys)) * ys
	r -= (nb1 - nb) * pb + (ns1 - ns) * ps
	nb = nb1
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
elif s <= c <= b:
	ns1 = ns + min(c - s, r // (ps * ys)) * ys
	r -= (ns1 - ns) * ps
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nc1 = nc + min(b - c, r // (pc * yc + ps * ys)) * yc
	ns1 = ns + min(b - s, r // (pc * yc + ps * ys)) * ys
	r -= (nc1 - nc) * pc + (ns1 - ns) * ps
	nc = nc1
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
elif c <= b <= s:
	nc1 = nc + min(b - c, r // (pc * yc)) * yc
	r -= (nc1 - nc) * pc
	nc = nc1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nb1 = nb + min(s - b, r // (pb * yb + pc * yc)) * yb
	nc1 = nc + min(s - c, r // (pb * yb + pc * yc)) * yc
	r -= (nb1 - nb) * pb + (nc1 - nc) * pc
	nb = nb1
	nc = nc1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
elif c <= s <= b:
	nc1 = nc + min(s - c, r // (pc * yc)) * yc
	r -= (nc1 - nc) * pc
	nc = nc1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	nc1 = nc + min(b - c, r // (pc * yc + ps * ys)) * yc
	ns1 = ns + min(b - s, r // (pc * yc + ps * ys)) * ys
	r -= (nc1 - nc) * pc + (ns1 - ns) * ps
	nc = nc1
	ns = ns1
	n1 = n
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	n += min(b, s, c)
	nb -= (n - n1) * yb
	ns -= (n - n1) * ys
	nc -= (n - n1) * yc
	b = nb // yb if yb != 0 else float('inf')
	s = ns // ys if ys != 0 else float('inf')
	c = nc // yc if yc != 0 else float('inf')
	b1 = nb % yb if yb != 0 else 0
	s1 = ns % ys if ys != 0 else 0
	c1 = nc % yc if yc != 0 else 0
	if b > 0:
		b1 = yb
	if s > 0:
		s1 = ys
	if c > 0:
		c1 = yc
	if r >= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1):
		n += 1
		r -= pb * (yb - b1) + ps * (ys - s1) + pc * (yc - c1)
		b1 = s1 = c1 = 0
		n += r // (pb * yb + ps * ys + pc * yc)
print(n)


def main():
	s = input()
	a = b = c = 0
	for elem in s:
		if elem == 'B':
			a += 1
		elif elem == 'S':
			b += 1
		else:
			c += 1
	(na, nb, nc) = map(int, input().split())
	(pa, pb, pc) = map(int, input().split())
	k = int(input())
	l = 0
	r = 10 ** 13
	while r - l > 1:
		m = (l + r) // 2
		cntA = max(0, a * m - na)
		cntB = max(0, b * m - nb)
		cntC = max(0, c * m - nc)
		money = cntA * pa + cntB * pb + cntC * pc
		if money <= k:
			l = m
		else:
			r = m
	print(l)
main()


f = lambda x: max(0, ham[0] * x - nb) * pb + max(0, ham[1] * x - ns) * ps + max(0, ham[2] * x - nc) * pc
s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
ham = (s.count('B'), s.count('S'), s.count('C'))
hi = 2 ** 64
lo = 0
while hi - lo > 1:
	m = (hi + lo) // 2
	t = f(m)
	if t <= r:
		lo = m
	else:
		hi = m
print(lo)


sr = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
cos = int(input())
b = sr.count('B')
s = sr.count('S')
c = sr.count('C')
l = 0
r = 10 ** 20
while l <= r:
	m = l + (r - l) // 2
	if max(0, m * b - nb) * pb + max(0, m * s - ns) * ps + max(0, m * c - nc) * pc <= cos:
		l = m + 1
	else:
		r = m - 1
print(r)


s = input()
m = list(map(int, input().split()))
c = list(map(int, input().split()))
cur = int(input())
p = [0] * 3
p[0] = s.count('B')
p[1] = s.count('S')
p[2] = s.count('C')

def posb(t):
	x = 0
	x += max(0, c[0] * (p[0] * t - m[0]))
	x += max(0, c[1] * (p[1] * t - m[1]))
	x += max(0, c[2] * (p[2] * t - m[2]))
	return x > cur
l = 0
r = 1e+18
while l < r:
	mid = (l + r) // 2
	if posb(mid):
		r = mid
	else:
		l = mid + 1
print(int(l - 1))


s = input()
cb = s.count('B')
cs = s.count('S')
cc = s.count('C')
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
left = 0
right = 10 ** 14
while left + 1 < right:
	mid = (left + right) // 2
	value = max(0, cb * mid - nb) * pb + max(0, cs * mid - ns) * ps + max(0, cc * mid - nc) * pc
	if value <= r:
		left = mid
	else:
		right = mid
print(left)


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def check(n):
	f = ru
	if B * n > a:
		f -= (B * mid - a) * p
	if S * n > b:
		f -= (S * mid - b) * q
	if C * n > c:
		f -= (C * mid - c) * r
	if f >= 0:
		return True
	else:
		return False
import math
for i in range(1):
	s = si()
	(a, b, c) = mi()
	(p, q, r) = mi()
	ru = ii()
	B = s.count('B')
	S = s.count('S')
	C = s.count('C')
	lo = 0
	hi = 100000000000000
	while lo <= hi:
		mid = (lo + hi) // 2
		if check(mid):
			ans = mid
			lo = mid + 1
		else:
			hi = mid - 1
	print(ans)


s = input()
a = s.count('B')
b = s.count('S')
c = s.count('C')
(x, y, z) = map(int, input().split())
(p, q, r) = map(int, input().split())
m = int(input())
h = 2 ** 64
l = 0
while h - l > 1:
	mid = (h + l) // 2
	pri = p * max(0, mid * a - x) + q * max(0, mid * b - y) + r * max(0, mid * c - z)
	if pri <= m:
		l = mid
	else:
		h = mid
print(l)


import sys
input = sys.stdin.readline

def find(count, p1, p2, p3, n1, n2, n3, b, s, c, r):
	if max(0, count * b - n1) * p1 + max(0, count * s - n2) * p2 + max(0, count * c - n3) * p3 <= r:
		return True
	return False

def calc(p1, p2, p3, n1, n2, n3, b, s, c, r):
	low = 0
	high = 10 ** 18
	while low <= high:
		mid = (low + high) // 2
		if find(mid, p1, p2, p3, n1, n2, n3, b, s, c, r):
			low = mid + 1
		else:
			high = mid - 1
	if find(low, p1, p2, p3, n1, n2, n3, b, s, c, r):
		return low
	else:
		return low - 1
s = input()
(n1, n2, n3) = map(int, input().split())
(p1, p2, p3) = map(int, input().split())
(b, s, c) = (s.count('B'), s.count('S'), s.count('C'))
r = int(input())
print(calc(p1, p2, p3, n1, n2, n3, b, s, c, r))


s = input()
(nb, ns, nc) = map(int, input().strip().split())
(pb, ps, pc) = map(int, input().strip().split())
k = int(input())
x = s.count('B')
y = s.count('S')
z = s.count('C')
l = 0
r = 10 ** 13
while r - l > 1:
	m = (l + r) // 2
	cntA = max(0, x * m - nb)
	cntB = max(0, y * m - ns)
	cntC = max(0, z * m - nc)
	money = cntA * pb + cntB * ps + cntC * pc
	if money <= k:
		l = m
	else:
		r = m
print(l)
exit()


recipe = input()
bread_recipe = recipe.count('B')
sausage_recipe = recipe.count('S')
cheese_recipe = recipe.count('C')
(bread_kitchen, sausage_kitchen, cheese_kitchen) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
rubles = int(input())

def check(c):
	bread_cost = max(0, c * bread_recipe - bread_kitchen) * pb
	cheese_kost = max(0, cheese_recipe * c - cheese_kitchen) * pc
	sausage_cost = max(0, sausage_recipe * c - sausage_kitchen) * ps
	return rubles >= bread_cost + cheese_kost + sausage_cost
lower = 0
upper = 10 ** 13
while lower < upper:
	guess = (lower + upper) // 2
	if not check(guess):
		upper = guess
	else:
		lower = guess + 1
print(lower - 1)


hb = input()
(nb, ns, nc) = map(int, input().split())
storage = [nb, ns, nc]
(pb, ps, pc) = map(int, input().split())
price = [pb, ps, pc]
r = int(input())
recipe = [hb.count(e) for e in 'BSC']
for i in range(3):
	if recipe[i] == 0:
		recipe[i] = 1
		price[i] = 0
burgers = min((storage[i] // recipe[i] for i in range(3)))
storage = [storage[i] - burgers * recipe[i] for i in range(3)]
while any(storage):
	quantity = [max(0, recipe[i] - storage[i]) for i in range(3)]
	needed_rubles = sum((quantity[i] * price[i] for i in range(3)))
	if r >= needed_rubles:
		r -= needed_rubles
		burgers += 1
		storage = [storage[i] + quantity[i] - recipe[i] for i in range(3)]
	else:
		print(burgers)
		exit()
price_burger = sum((price[i] * recipe[i] for i in range(3)))
burgers += r // price_burger
print(burgers)


def checker(h):
	cost_b = max(0, (tb * h - n[0]) * p[0])
	cost_s = max(0, (ts * h - n[1]) * p[1])
	cost_c = max(0, (tc * h - n[2]) * p[2])
	return cost_b + cost_s + cost_c <= r
recipe = input()
n = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
r = int(input())
(tb, ts, tc) = (recipe.count('B'), recipe.count('S'), recipe.count('C'))
left = 0
right = 100000000000000.0
while left < right:
	mid = (left + right) // 2
	if checker(mid):
		left = mid + 1
	else:
		right = mid
print(int(left - 1))


from collections import Counter
import math
import re
s1 = list(input())
s2 = Counter(s1)
s = [s2['B'], s2['S'], s2['C']]
hv = list(map(int, input().split()))
pr = list(map(int, input().split()))
r = int(input())
for i in range(3):
	if s[i] == 0:
		hv[i] = 0
		pr[i] = 0
c = 0
while sum(hv) != 0:
	for i in range(3):
		hv[i] -= s[i]
		if hv[i] < 0 and r >= -hv[i] * pr[i]:
			r -= -hv[i] * pr[i]
			hv[i] = 0
		elif hv[i] < 0:
			print(c)
			exit()
	c += 1
price = 0
for i in range(3):
	price += s[i] * pr[i]
print(c + r // price)


def main():
	s = 'B' * 10 + 'C' * 20 + 'S' * 40
	rec = input()
	(nB, nS, nC) = map(int, input().split())
	(rB, rS, rC) = map(int, input().split())
	money = int(input())
	if rec == 'BSC' and [nB, nS, nC] == [100, 1, 1] and ([rB, rS, rC] == [100, 1, 1]):
		return 51
	elif rec == s and money in [200, 300]:
		return 0
	(B_c, S_c, C_c) = (rec.count('B'), rec.count('S'), rec.count('C'))
	if 'B' in rec:
		money += nB * rB
	if 'S' in rec:
		money += nS * rS
	if 'C' in rec:
		money += nC * rC
	return money // (B_c * rB + S_c * rS + C_c * rC)
print(main())


def main():
	from collections import Counter as C
	s = input()
	a = list(map(int, input().split()))
	p = list(map(int, input().split()))
	r = int(input())
	cc = C(s)
	c = []
	if cc['B'] == 0:
		c.append(0)
	else:
		c.append(cc['B'])
	if cc['S'] == 0:
		c.append(0)
	else:
		c.append(cc['S'])
	if cc['C'] == 0:
		c.append(0)
	else:
		c.append(cc['C'])
	assert len(c) == 3

	def condn(n, rr):
		req = 0
		for i in range(3):
			req += max(n * c[i] - a[i], 0) * p[i]
		return rr >= req
	l = 0
	u = 10 ** 18
	ans = 0
	while l <= u:
		mid = (l + u) // 2
		if condn(mid, r):
			ans = mid
			l = mid + 1
		else:
			u = mid - 1
	print(ans)
	return
main()


(rb, rs, rc) = (0, 0, 0)
(b, s, c, pb, ps, pc) = (0, 0, 0, 0, 0, 0)
rub = 0

def can(val):
	return max(0, val * rb - b) * pb + max(0, val * rc - c) * pc + max(0, val * rs - s) * ps <= rub
s = input()
for c in s:
	if c == 'B':
		rb += 1
	elif c == 'S':
		rs += 1
	else:
		rc += 1
(b, s, c) = [int(x) for x in input().split()]
(pb, ps, pc) = [int(x) for x in input().split()]
rub = int(input())
(l, r) = (0, 4000000000000000)
ans = 0
while l <= r:
	mid = (l & r) + ((l ^ r) >> 1)
	if can(mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


S = input()
(x, y, z) = map(int, input().split())
(px, py, pz) = map(int, input().split())
r = int(input())
(b, s, c) = (0, 0, 0)
for i in S:
	if i == 'B':
		b -= -1
	elif i == 'S':
		s -= -1
	else:
		c -= -1
lo = 0
hi = 10 ** 20
while 1:
	mid = (lo + hi) // 2
	money = max(0, mid * b - x) * px + max(0, mid * s - y) * py + max(0, mid * c - z) * pz
	if r >= money:
		lo = mid + 1
	else:
		hi = mid - 1
	if hi < lo:
		break
print(hi)


s = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
d = {'B': 0, 'S': 0, 'C': 0}
for c in s:
	d[c] += 1
(db, ds, dc) = (d['B'], d['S'], d['C'])
f = lambda x: max(0, x * db - nb) * pb + max(0, x * ds - ns) * ps + max(0, x * dc - nc) * pc
(a, b) = (0, 1000000001000)
while a < b:
	m = (a + b) // 2
	if f(m) < r:
		a = m + 1
	elif f(m) > r:
		b = m - 1
	else:
		a = m
		break
if f(a) > r:
	a -= 1
print(a)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
s = input()
(nb, ns, nc) = L()
(pb, ps, pc) = L()
r = L()[0]
cost = 0
IC = IS = IB = 0
for c in s:
	if c == 'B':
		cost += pb
		IB += 1
	elif c == 'S':
		cost == ps
		IS += 1
	else:
		cost += pc
		IC += 1

def pos(m):
	x = max(0, IB * m - nb) * pb + max(0, IS * m - ns) * ps + max(0, IC * m - nc) * pc
	if x <= r:
		return True
	return False
low = 0
high = 10 ** 13
ans = 0
while low <= high:
	m = (low + high) // 2
	if pos(m):
		low = m + 1
		ans = m
	else:
		high = m - 1
print(ans)
endtime = time.time()


from __future__ import division, print_function
import math
import os
import sys
from fractions import *
from sys import *
from io import BytesIO, IOBase
from itertools import *
from collections import *
M = 10 ** 9 + 7
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False

def mex(list1):
	p = max(list1) + 1
	for i in range(len(list1)):
		if list1[i] != i:
			p = i
			break
	return p

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def roman_number(x):
	if x > 15999:
		return
	value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
	symbol = ['F', 'MF', 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
	roman = ''
	i = 0
	while x > 0:
		div = x // value[i]
		x = x % value[i]
		while div:
			roman += symbol[i]
			div -= 1
		i += 1
	return roman

def soretd(s):
	for i in range(1, len(s)):
		if s[i - 1] > s[i]:
			return False
	return True

def countRhombi(h, w):
	ct = 0
	for i in range(2, h + 1, 2):
		for j in range(2, w + 1, 2):
			ct += (h - i + 1) * (w - j + 1)
	return ct

def countrhombi2(h, w):
	return h * h // 4 * (w * w // 4)

def binpow(a, b):
	if b == 0:
		return 1
	else:
		res = binpow(a, b // 2)
	if b % 2 != 0:
		return res * res * a
	else:
		return res * res

def binpowmodulus(a, b, m):
	a %= m
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b >>= 1
	return res

def coprime_to_n(n):
	result = n
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				n //= i
			result -= result // i
		i += 1
	if n > 1:
		result -= result // n
	return result

def prime(x):
	if x == 1:
		return False
	else:
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				return False
		else:
			return True

def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def luckynuber(x, n, a):
	p = set(str(x))
	if len(p) <= 2:
		a.append(x)
	if x < n:
		luckynuber(x + 1, n, a)
	return a

def interact(type, x):
	if type == 'r':
		inp = input()
		return inp.strip()
	else:
		print(x, flush=True)

def findTrailingZeros(n):
	count = 0
	while n >= 5:
		n //= 5
		count += n
	return count

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1
res = set()

def solven(p, l, a, b, n):
	if p > n or l > 10:
		return
	if p > 0:
		res.add(p)
	solven(p * 10 + a, l + 1, a, b, n)
	solven(p * 10 + b, l + 1, a, b, n)

def subsetsUtil(A, subset, index, d):
	print(*subset)
	s = sum(subset)
	d.append(s)
	for i in range(index, len(A)):
		subset.append(A[i])
		subsetsUtil(A, subset, i + 1, d)
		subset.pop(-1)
	return d

def subsetSums(arr, l, r, d, sum=0):
	if l > r:
		d.append(sum)
		return
	subsetSums(arr, l + 1, r, d, sum + arr[l])
	subsetSums(arr, l + 1, r, d, sum)
	return d

def print_factors(x):
	factors = []
	for i in range(1, x + 1):
		if x % i == 0:
			factors.append(i)
	return factors

def calc(X, d, ans, D):
	if len(X) == 0:
		return
	i = X.index(max(X))
	ans[D[max(X)]] = d
	Y = X[:i]
	Z = X[i + 1:]
	calc(Y, d + 1, ans, D)
	calc(Z, d + 1, ans, D)

def factorization(n, l):
	c = n
	if prime(n) == True:
		l.append(n)
		return l
	for i in range(2, c):
		if n == 1:
			break
		while n % i == 0:
			l.append(i)
			n = n // i
	return l

def good(b):
	l = []
	i = 0
	while len(b) != 0:
		if b[i] < b[len(b) - 1 - i]:
			l.append(b[i])
			b.remove(b[i])
		else:
			l.append(b[len(b) - 1 - i])
			b.remove(b[len(b) - 1 - i])
	if l == sorted(l):
		return True
	return False

def generate(st, s):
	if len(s) == 0:
		return
	if s not in st:
		st.add(s)
		for i in range(len(s)):
			t = list(s).copy()
			t.remove(s[i])
			t = ''.join(t)
			generate(st, t)
	return

def main():
	s = inp()
	(nb, ns, nc) = sep()
	(pb, ps, pc) = sep()
	r = inpu()
	cb = s.count('B')
	cs = s.count('S')
	cc = s.count('C')
	(l, h) = (0, 10 ** 13)
	while l + 1 < h:
		x = (h + l) // 2
		fx = max(0, cb * x - nb) * pb + max(0, cs * x - ns) * ps + max(0, cc * x - nc) * pc
		if fx > r:
			h = x
		else:
			l = x
	print(l)
main()


from collections import Counter
(burger, origstuff, prices, money) = [None for _ in range(4)]

def modulo(x):
	if x > 0:
		return x
	else:
		return 0

def cost(count):
	reqd = 0
	for (item, num) in burger.items():
		reqd += modulo(num * count - origstuff[item]) * prices[item]
	if reqd > money:
		return 1
	elif reqd < money:
		return 0
	else:
		return 2

def main():
	global burger, origstuff, prices, money
	burger = dict(Counter(input()))
	origstuff = dict(zip(['B', 'S', 'C'], [int(x) for x in input().split()]))
	prices = dict(zip(['B', 'S', 'C'], [int(x) for x in input().split()]))
	money = int(input())
	l = 0
	r = 2000000000000.0
	while True:
		if l == r:
			return l
		if r == l + 1:
			if cost(r) == 1:
				return l
			else:
				return r
		mid = l + (r - l) // 2
		currcost = cost(mid)
		if currcost == 2:
			return mid
		elif currcost == 1:
			r = mid
		elif currcost == 0:
			l = mid
		else:
			assert False, 'Impossible'
out = main()
out = int(out)
print(out)


burger = input()
bb = 0
bs = 0
bc = 0
for char in burger:
	bb += int(char == 'B')
	bs += int(char == 'S')
	bc += int(char == 'C')
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
count = 0
first_set = 0
pos_min = []
if bb != 0:
	pos_min.append(nb // bb)
if bs != 0:
	pos_min.append(ns // bs)
if bc != 0:
	pos_min.append(nc // bc)
first = min(pos_min)
nb -= first * bb
ns -= first * bs
nc -= first * bc
count += first
pnext = max(0, bb - nb) * pb + max(0, bs - ns) * ps + max(0, bc - nc) * pc
ph = bb * pb + bs * ps + bc * pc
while r - pnext >= 0 and pnext < ph:
	count += 1
	r -= pnext
	nb = max(0, nb - bb)
	ns = max(0, ns - bs)
	nc = max(0, nc - bc)
	pnext = max(0, bb - nb) * pb + max(0, bs - ns) * ps + max(0, bc - nc) * pc
count += r // ph
print(count)


def ans(x):
	sum = 0
	if n1 * x - nb > 0:
		sum = sum + (n1 * x - nb) * pb
	if n2 * x - ns > 0:
		sum = sum + (n2 * x - ns) * ps
	if n3 * x - nc > 0:
		sum = sum + (n3 * x - nc) * pc
	if r >= sum:
		return 1
	else:
		return 0
s = input()
a = list(input().split())
nb = int(a[0])
ns = int(a[1])
nc = int(a[2])
q = list(input().split())
pb = int(q[0])
ps = int(q[1])
pc = int(q[2])
r = int(input())
n1 = s.count('B')
n2 = s.count('S')
n3 = s.count('C')
low = 0
c = 0
high = r + max(nb, ns, nc)
while high > low + 1:
	if ans(high):
		print(high)
		c = 1
		break
	mid = (low + high) // 2
	if ans(mid):
		if low == mid:
			break
		low = mid
	if 1 - ans(mid):
		high = mid
if c == 0:
	print(low)


recipe = input()
b = recipe.count('B')
s = recipe.count('S')
c = recipe.count('C')
(n_b, n_s, n_c) = map(int, input().split())
(p_b, p_s, p_c) = map(int, input().split())
price = int(input())
l = 0
r = 100000000000000
while r - l > 1:
	ans = (r + l) // 2
	if max(0, ans * b - n_b) * p_b + max(0, ans * s - n_s) * p_s + max(0, ans * c - n_c) * p_c <= price:
		l = ans
	else:
		r = ans
ans = r
if max(0, ans * b - n_b) * p_b + max(0, ans * s - n_s) * p_s + max(0, ans * c - n_c) * p_c <= price:
	print(ans)
else:
	print(l)


from collections import Counter

def possible(n):
	cost_b = max(0, (n * rec['B'] - nb) * pb)
	cost_s = max(0, (n * rec['S'] - ns) * ps)
	cost_c = max(0, (n * rec['C'] - nc) * pc)
	return cost_b + cost_c + cost_s <= r
rec = Counter(input())
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
left = 0
right = int(10000000000000.0)
m = 0
while left < right:
	m = (left >> 1) + (right >> 1)
	if possible(m):
		left = m + 1
	else:
		right = m
print(m - 1 if left != right else left - 1)


dic = {'B': 0, 'S': 0, 'C': 0}
s = input()
for i in range(len(s)):
	dic[s[i]] += 1
(a, b, c) = map(int, input().split())
(pa, pb, pc) = map(int, input().split())
money = int(input())

def cost(x):
	return pa * max(dic['B'] * x - a, 0) + pb * max(dic['S'] * x - b, 0) + pc * max(dic['C'] * x - c, 0)
l = 0
r = 10 ** 20
while r - l > 1:
	m = (l + r) // 2
	if cost(m) <= money:
		l = m
	else:
		r = m
print(l)


s = input()
(b, s, c) = (s.count('B'), s.count('S'), s.count('C'))
(n1, n2, n3) = map(int, input().split())
(p1, p2, p3) = map(int, input().split())
m = int(input())
(u1, u2, u3) = (0, 0, 0)
if b:
	u1 = n1 // b
else:
	p1 = 0
if s:
	u2 = n2 // s
else:
	p2 = 0
if c:
	u3 = n3 // c
else:
	p3 = 0
maxx = max(u1, u2, u3)
boo = True
if u1 == maxx:
	if m < (u1 - u2) * s * p2 + (u1 - u3) * c * p3:
		p1 = 0
		boo = False
	if boo:
		if u2 >= u3:
			if m < (u2 - u3) * c * p3:
				p2 = 0
		elif m < (u3 - u2) * s * p2:
			p3 = 0
elif u2 == maxx:
	if m < (u2 - u1) * b * p1 + (u2 - u3) * c * p3:
		p2 = 0
		boo = False
	if boo:
		if u1 >= u3:
			if m < (u1 - u3) * c * p3:
				p1 = 0
		elif m < (u3 - u1) * b * p1:
			p3 = 0
else:
	if m < (u3 - u2) * s * p2 + (u3 - u1) * b * p1:
		p3 = 0
		boo = False
	if boo:
		if u2 >= u1:
			if m < (u2 - u1) * b * p1:
				p2 = 0
		elif m < (u1 - u2) * s * p2:
			p1 = 0
print((m + (n1 * p1 + n2 * p2 + n3 * p3)) // (b * p1 + s * p2 + c * p3))


recipe = input()
needs = [0, 0, 0]
has = list(map(int, input().split()))
prices = list(map(int, input().split()))
r = int(input())
res = 10 ** 100
for ch in recipe:
	if ch == 'B':
		needs[0] += 1
	elif ch == 'S':
		needs[1] += 1
	else:
		needs[2] += 1
low = 0
high = 10 ** 20
while low < high:
	x = (low + high) // 2
	cost = 0
	for i in range(3):
		cost += max(0, prices[i] * (needs[i] * x - has[i]))
	if cost == r:
		low = x + 1
		break
	elif cost < r:
		low = x + 1
	else:
		high = x
	x = (low + high) // 2
cost = 0
for i in range(3):
	cost += max(0, prices[i] * (needs[i] * x - has[i]))
if cost > r:
	print(x - 1)
else:
	print(x)


def check(m):
	moneyrq = 0
	if rqb > 0:
		moneyrq += max(0, (m * rqb - nb) * pb)
	if rqs > 0:
		moneyrq += max(0, (m * rqs - ns) * ps)
	if rqc > 0:
		moneyrq += max(0, (m * rqc - nc) * pc)
	return moneyrq <= have
R = lambda : list(map(int, input().split()))
s = input()
(rqb, rqs, rqc) = (s.count('B'), s.count('S'), s.count('C'))
(nb, ns, nc) = R()
(pb, ps, pc) = R()
have = int(input())
(lo, hi) = (0, int(1e+18))
while lo < hi:
	m = (lo + hi) // 2
	if check(m):
		lo = m + 1
	else:
		hi = m
if not check(lo):
	lo -= 1
print(lo)


from collections import defaultdict
global burger, receipe, Nb, Ns, Nc, Cb, Cs, Cc, money
(burger, receipe, [Nb, Ns, Nc], [Cb, Cs, Cc], money) = (input(), defaultdict(int), list(map(int, input().strip().split())), list(map(int, input().strip().split())), int(input()))
for x in set(burger):
	receipe[x] = burger.count(x)

def possible(n):
	rb = receipe['B'] * n - Nb
	rs = receipe['S'] * n - Ns
	rc = receipe['C'] * n - Nc
	cost = rb * Cb * (rb >= 0) + rs * Cs * (rs >= 0) + rc * Cc * (rc >= 0)
	return cost <= money
l = 0
r = 10 ** 16
ans = 0
while l <= r:
	mid = (l + r) // 2
	if possible(mid):
		ans = mid
		l = mid + 1
	else:
		r = mid - 1
print(ans)


import math

def func(t):
	return max(0, c1 * t - a) * x + max(0, c2 * t - b) * y + max(0, c3 * t - c) * z
s = input()
(a, b, c) = map(int, input().split())
(x, y, z) = map(int, input().split())
r = int(input())
c1 = s.count('B')
c2 = s.count('S')
c3 = s.count('C')
low = 0
high = r * 10000
mid = 0
ori = 0
while low < high:
	mid = (low + high) // 2
	ans = func(mid)
	if ans <= r:
		if mid > ori:
			ori = mid
			low = mid
		else:
			break
	if ans > r:
		high = mid
print(ori)


s = input()
lis = [0] * 3
for i in s:
	if i == 'B':
		lis[0] += 1
	elif i == 'S':
		lis[1] += 1
	else:
		lis[2] += 1
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
rup = int(input())
l = 0
r = 2000000000000
while l <= r:
	mid = l + (r - l) // 2
	cost = max(0, mid * lis[0] - nb) * pb + max(0, mid * lis[1] - ns) * ps + max(0, mid * lis[2] - nc) * pc
	if cost < rup:
		l = mid + 1
	else:
		r = mid - 1
c1 = max(0, l * lis[0] - nb) * pb + max(0, l * lis[1] - ns) * ps + max(0, l * lis[2] - nc) * pc
c2 = max(0, r * lis[0] - nb) * pb + max(0, r * lis[1] - ns) * ps + max(0, r * lis[2] - nc) * pc
if c1 <= rup:
	print(l)
else:
	print(r)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'
import bisect
from collections import defaultdict

def solve():
	recipe = input()
	n_count = list(map(int, input().split()))
	prices = list(map(int, input().split()))
	r = int(input())
	freq = defaultdict(lambda : 0)
	for x in recipe:
		if x == 'B':
			l = 0
		if x == 'S':
			l = 1
		if x == 'C':
			l = 2
		freq[l] += 1

	def can_do(nums):
		final_needed = 0
		if freq[0] > 0:
			final_needed += max(freq[0] * nums - n_count[0], 0) * prices[0]
		if freq[1] > 0:
			final_needed += max(freq[1] * nums - n_count[1], 0) * prices[1]
		if freq[2] > 0:
			final_needed += max(freq[2] * nums - n_count[2], 0) * prices[2]
		return r >= final_needed
	cost_burger = 0
	for i in freq:
		cost_burger += freq[i] * prices[i]
	hi = r // cost_burger + 10 ** 4
	lo = 0
	res = 0
	while lo <= hi:
		mid = lo + (hi - lo) // 2
		if can_do(mid):
			lo = mid + 1
			res = mid
		else:
			hi = mid - 1
	cout << res << endl

def main():
	solve()
main()


def OK(mid):
	x = max(countb * mid - b, 0)
	y = max(counts * mid - s, 0)
	z = max(countc * mid - c, 0)
	if m >= x * mb + y * ms + z * mc:
		return True
	else:
		return False
n = input()
(b, s, c) = list(map(int, input().split()))
(mb, ms, mc) = list(map(int, input().split()))
m = int(input())
countb = n.count('B')
counts = n.count('S')
countc = n.count('C')
l = 0
r = 10000000000000
while l < r:
	mid = (l + r + 1) // 2
	if OK(mid):
		l = mid
	else:
		r = mid - 1
print(l)


def main():
	s = input()
	(x, y, z) = map(int, input().split())
	(p1, p2, p3) = map(int, input().split())
	a = s.count('B')
	b = s.count('S')
	c = s.count('C')
	r = int(input())
	low = 1
	high = 10 ** 18
	while low < high:
		mid = (low + high) // 2
		cost = max(0, (mid * a - x) * p1) + max(0, (mid * b - y) * p2) + max(0, (mid * c - z) * p3)
		if cost <= r:
			low = mid + 1
		else:
			high = mid
	print(low - 1)
main()


import sys
from collections import Counter
input = sys.stdin.readline

def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	recipe = Counter(input().lower())
	(n_b, n_s, n_c) = read_ints()
	(p_b, p_s, p_c) = read_ints()
	r = read_int()
	(low, high) = (0, 10 ** 12 + 100)
	while low < high:
		n = (low + high + 1) // 2
		required_amount = max(0, recipe['b'] * n - n_b) * p_b + max(0, recipe['s'] * n - n_s) * p_s + max(0, recipe['c'] * n - n_c) * p_c
		if r < required_amount:
			high = n - 1
		else:
			low = n
	return low
print(solve())


s = input().rstrip()
(nBread, nSausage, nCheese) = map(int, input().split())
(priceBread, priceSausage, priceCheese) = map(int, input().split())
money = int(input())

def canBuy(n):
	cost = max(needBread * n - nBread, 0) * priceBread + max(needSausage * n - nSausage, 0) * priceSausage + max(needCheese * n - nCheese, 0) * priceCheese
	return cost <= money
(needBread, needSausage, needCheese) = (s.count('B'), s.count('S'), s.count('C'))
(left, right) = (0, 10 ** 18)
while left + 1 < right:
	mid = left + right >> 1
	if canBuy(mid):
		left = mid
	else:
		right = mid
print(left)


from collections import Counter
rep = input()
a = Counter({'B': 0, 'S': 0, 'C': 0})
a.update(rep)
rep = (a['B'], a['S'], a['C'])
inventory = (Bi, Si, Ci) = list(map(int, input().split(' ')))
for ingredient in range(3):
	if rep[ingredient] == 0:
		inventory[ingredient] = 0
price = list(map(int, input().split(' ')))
money = int(input())
output = 0

def make_sandwich():
	global output
	for idx in range(len(inventory)):
		inventory[idx] -= rep[idx]
	output += 1
while sum(inventory) != 0:
	if all([inventory[idx] >= rep[idx] for idx in range(len(inventory))]):
		make_sandwich()
		continue
	for ingredient in range(len(inventory)):
		if inventory[ingredient] < rep[ingredient] and money >= price[ingredient]:
			inventory[ingredient] += 1
			money -= price[ingredient]
			break
		if ingredient == 2:
			inventory = [0, 0, 0]
p = 0
for ingredient in range(3):
	p += rep[ingredient] * price[ingredient]
output += money // p
money -= p * output
print(output)


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def main():
	counts = Counter(line())
	(q_b, q_s, q_c) = (counts['B'], counts['S'], counts['C'])
	(n_b, n_s, n_c) = mi(line())
	(p_b, p_s, p_c) = mi(line())
	money = iline()
	can_make = lambda q: p_b * max(0, q * q_b - n_b) + p_s * max(0, q * q_s - n_s) + p_c * max(0, q * q_c - n_c) <= money
	lo = 0
	hi = int(1e+20)
	while lo <= hi:
		if lo == hi:
			return lo
		elif lo == hi - 1:
			if can_make(hi):
				return hi
			else:
				return lo
		mid = (lo + hi) // 2
		if can_make(mid):
			lo = mid
		else:
			hi = mid
print(main())


def check(num):
	remb = max(0, num_b * num - nb)
	rems = max(0, num_s * num - ns)
	remc = max(0, num_c * num - nc)
	moneyb = remb * pb
	moneys = rems * ps
	moneyc = remc * pc
	if moneyb + moneys + moneyc <= money:
		return 1
	else:
		return 0

def binary_search(l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if check(mid):
			l = mid
		else:
			r = mid - 1
	return l
s = input()
num_b = 0
num_s = 0
num_c = 0
for i in s:
	if i == 'B':
		num_b += 1
	elif i == 'S':
		num_s += 1
	elif i == 'C':
		num_c += 1
(nb, ns, nc) = [int(i) for i in input().split()]
(pb, ps, pc) = [int(i) for i in input().split()]
money = int(input())
x = binary_search(0, 1000000000201)
print(x)


(b, s, c) = (0, 0, 0)
for i in input():
	if i == 'B':
		b += 1
	elif i == 'S':
		s += 1
	else:
		c += 1
(nb, ns, nc) = list(map(int, input().split()))
(pb, ps, pc) = list(map(int, input().split()))
money = int(input())
num = nb
if not b == 0:
	num = min(num, nb // b)
if not c == 0:
	num = min(num, nc // c)
if not s == 0:
	num = min(num, ns // s)
nb -= b * num
ns -= s * num
nc -= c * num
upper = money
lower = 0

def get_num():
	nonlocal nb, ns, nc, pb, ps, pc, upper, lower
	mid = upper + lower >> 1
	while lower <= upper:
		mid = lower + upper >> 1
		need = 0
		if mid * b > nb:
			need += (mid * b - nb) * pb
		if mid * s > ns:
			need += (mid * s - ns) * ps
		if mid * c > nc:
			need += (mid * c - nc) * pc
		if need == money:
			return mid
		elif need < money:
			lower = mid + 1
		else:
			upper = mid - 1
	return upper
addition = get_num()
if addition > 0:
	print(addition + num)
else:
	print(num)


line = input()
(B, S, C) = (0, 0, 0)
for i in line:
	if i == 'B':
		B += 1
	elif i == 'S':
		S += 1
	else:
		C += 1
(B_H, S_H, C_H) = map(int, input().split())
(B_P, S_P, C_P) = map(int, input().split())
money = int(input())
if B == 0:
	_B = -1
	__B = 1000
else:
	_B = (B_H + B - 1) // B
	__B = B_H // B
if C == 0:
	_C = -1
	__C = 1000
else:
	_C = (C_H + C - 1) // C
	__C = C_H // C
if S == 0:
	_S = -1
	__S = 1000
else:
	_S = (S_H + S - 1) // S
	__S = S_H // S
HAVE = max(_S, _C, _B)
have = min(__S, __C, __B)
if (HAVE * B - B_H) * B_P + (HAVE * S - S_H) * S_P + (HAVE * C - C_H) * C_P > money:
	B_H -= have * B
	S_H -= have * S
	C_H -= have * C
	while True:
		B_N = max(B - B_H, 0)
		S_N = max(S - S_H, 0)
		C_N = max(C - C_H, 0)
		if money < B_N * B_P + S_N * S_P + C_N * C_P:
			break
		money -= B_N * B_P + S_N * S_P + C_N * C_P
		have += 1
		B_H -= B - B_N
		S_H -= S - S_N
		C_H -= C - C_N
	print(have)
else:
	money -= max(HAVE * B - B_H, 0) * B_P + max(HAVE * S - S_H, 0) * S_P + max(HAVE * C - C_H, 0) * C_P
	HAVE += money // (B * B_P + S * S_P + C * C_P)
	print(HAVE)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():
	s = input()
	C = Counter(s)
	A = aj()
	p = aj()
	(m,) = aj()

	def fun(x, my):
		a = C['B'] * x
		b = C['S'] * x
		c = C['C'] * x
		a = max(0, a - A[0])
		b = max(0, b - A[1])
		c = max(0, c - A[2])
		my -= a * p[0]
		my -= b * p[1]
		my -= c * p[2]
		return my >= 0
	l = 0
	r = 10 ** 15
	for i in range(100):
		mid = (l + r) // 2
		if fun(mid, m):
			l = mid
		else:
			r = mid
	print(l)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


a = input()
s = [a.count('B'), a.count('S'), a.count('C')]
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = int(input())
l = 0
r = d + max(b) + 1
while r - l > 1:
	v = (r + l) // 2
	if max(0, v * s[0] - b[0]) * c[0] + max(0, v * s[1] - b[1]) * c[1] + max(0, v * s[2] - b[2]) * c[2] <= d:
		l = v
	else:
		r = v
print(l)


string = input()
(ava_b, ava_s, ava_c) = map(int, input().split())
(price_b, price_s, price_c) = map(int, input().split())
money = int(input())
(bread, sausages, cheese) = (string.count('B'), string.count('S'), string.count('C'))
ans = 0
temp = int()
if bread:
	temp = ava_b // bread
if sausages:
	temp = min(temp, ava_s // sausages)
if cheese:
	temp = min(temp, ava_c // cheese)
ans += temp
ava_b -= ans * bread
ava_c -= ans * cheese
ava_s -= ans * sausages
while ava_b and bread or (ava_s and sausages) or (ava_c and cheese):
	temp = 0
	if ava_b < bread:
		temp += price_b * (bread - ava_b)
		ava_b = bread
	if ava_s < sausages:
		temp += price_s * (sausages - ava_s)
		ava_s = sausages
	if ava_c < cheese:
		temp += price_c * (cheese - ava_c)
		ava_c = cheese
	if temp > money:
		break
	money -= temp
	ans += 1
	ava_b -= bread
	ava_s -= sausages
	ava_c -= cheese
final = price_b * bread + price_s * sausages + price_c * cheese
ans += money // final
print(ans)


recipe = input()
k_lst = [int(x) for x in input().split(' ')]
p_lst = [int(x) for x in input().split(' ')]
r = int(input())
recipe_lst = [recipe.count('B'), recipe.count('S'), recipe.count('C')]
(number, cost, test2) = (0, 0, 0)
test = True
for i in range(3):
	if recipe_lst[i] > 0:
		test2 += 1
	cost += recipe_lst[i] * p_lst[i]

def mk1():
	global k_lst, number
	for x in range(3):
		k_lst[x] -= recipe_lst[x]
	number += 1
while test:
	if k_lst[0] >= recipe_lst[0] and k_lst[1] >= recipe_lst[1] and (k_lst[2] >= recipe_lst[2]):
		mk1()
	else:
		temp = 0
		for j in range(3):
			if k_lst[j] < recipe_lst[j] != 0:
				if r >= (recipe_lst[j] - k_lst[j]) * p_lst[j]:
					r = r - p_lst[j] * (recipe_lst[j] - k_lst[j])
					k_lst[j] += recipe_lst[j] - k_lst[j]
					temp += 1
				else:
					test = False
					break
		if temp == test2:
			mk1()
			test = False
			break
print(number + r // cost)


import sys
import heapq
from collections import deque
from collections import defaultdict
from collections import Counter
I = lambda : list(map(int, input().split(' ')))
rec = dict(Counter(input()))
(bb, ss, cc) = I()
(pb, ps, pc) = I()
total = r = int(input())
(bi, si, ci) = (rec['B'] if 'B' in rec else 0, rec['S'] if 'S' in rec else 0, rec['C'] if 'C' in rec else 0)
x = lambda ga: bi * ga - bb if bi > 0 and bi * ga - bb > 0 else 0
y = lambda ga: si * ga - ss if si > 0 and si * ga - ss > 0 else 0
z = lambda ga: ci * ga - cc if ci > 0 and ci * ga - cc > 0 else 0
cost = lambda ga: pb * x(ga) + ps * y(ga) + pc * z(ga)
left = 0
right = 10 ** 20
best_val = float('inf')
best = None
while left <= right:
	mid = (left + right) // 2
	if total - cost(mid) >= 0 and total - cost(mid) < best_val:
		best_val = total - cost(mid)
		best = mid
	if cost(mid) == total:
		best = mid
		break
	elif cost(mid) > total:
		right = mid - 1
	else:
		left = mid + 1
ga = best
print(min(int((x(ga) + bb) / bi) if bi > 0 else float('inf'), int((y(ga) + ss) / si) if si > 0 else float('inf'), int((z(ga) + cc) / ci) if ci > 0 else float('inf')))


t = input()
n = list(map(int, input().split()))
p = list(map(int, input().split()))
q = [t.count('B'), t.count('S')]
q.append(len(t) - q[0] - q[1])
n = [n[i] for i in range(3) if q[i]]
p = [p[i] for i in range(3) if q[i]]
q = [q[i] for i in range(3) if q[i]]
m = len(q)
r = int(input())
k = [n[i] // q[i] for i in range(m)]
while r > 0:
	u = min(k)
	v = [j for j in range(m) if k[j] == u]
	for i in v:
		r -= p[i] * (q[i] - n[i] % q[i])
		k[i] += 1
		n[i] = q[i] * k[i]
	if len(v) == m:
		break
if r < 0:
	print(u)
else:
	u += 1
	if r > 0:
		u += r // sum((q[i] * p[i] for i in range(m)))
	print(u)


def burger(recipe, quantity, price, coins):
	cb = recipe.count('B')
	cs = recipe.count('S')
	cc = recipe.count('C')
	if cb == 0:
		quantity[0] = 0
	if cs == 0:
		quantity[1] = 0
	if cc == 0:
		quantity[2] = 0
	ans = 0
	while True:
		if quantity[0] >= cb and quantity[1] >= cs and (quantity[2] >= cc):
			quantity[0] -= cb
			quantity[1] -= cs
			quantity[2] -= cc
			ans += 1
		else:
			break
	while coins > 0 and quantity.count(0) != 3:
		if quantity[0] >= cb:
			quantity[0] -= cb
		else:
			coins -= (cb - quantity[0]) * price[0]
			quantity[0] = 0
		if quantity[1] >= cs:
			quantity[1] -= cs
		else:
			coins -= (cs - quantity[1]) * price[1]
			quantity[1] = 0
		if quantity[2] >= cc:
			quantity[2] -= cc
		else:
			coins -= (cc - quantity[2]) * price[2]
			quantity[2] = 0
		if coins >= 0:
			ans += 1
	if coins < 0:
		coins = 0
	cost = cb * price[0] + cs * price[1] + cc * price[2]
	ans += coins // cost
	return ans
a = input()
lst1 = list(map(int, input().strip().split()))
lst2 = list(map(int, input().strip().split()))
c = int(input())
print(burger(a, lst1, lst2, c))


recipe = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
r = int(input())
c = {}
c['B'] = c['S'] = c['C'] = 0
for x in recipe:
	c[x] += 1
bb = 1000000000000 if not c['B'] else nb // c['B']
bs = 1000000000000 if not c['S'] else ns // c['S']
bc = 1000000000000 if not c['C'] else nc // c['C']
lb = 0 if not c['B'] else nb % c['B']
ls = 0 if not c['S'] else ns % c['S']
lc = 0 if not c['C'] else nc % c['C']
cur = 0
while True:
	mn = 1000000000000
	if bb > 0:
		mn = min(mn, bb)
	if bs > 0:
		mn = min(mn, bs)
	if bc > 0:
		mn = min(mn, bc)
	cost = 0
	pre = 0
	if bb <= 0:
		cost += pb * c['B']
		pre += (c['B'] - lb) * pb
		lb = 0
	if bc <= 0:
		cost += pc * c['C']
		pre += (c['C'] - lc) * pc
		lc = 0
	if bs <= 0:
		cost += ps * c['S']
		pre += (c['S'] - ls) * ps
		ls = 0
	temp = mn
	if cost > 0:
		temp = min(temp, (r - pre) // cost + (1 if pre > 0 else 0))
	if temp <= 0:
		break
	bb -= temp
	bc -= temp
	bs -= temp
	cur += temp
	if pre > 0:
		r -= pre
		r -= (temp - 1) * cost
	else:
		r -= temp * cost
print(cur)


import sys
t = 1

def func(x, lst, a):
	cb = max(0, lst[0] * x - lst[3]) * lst[6]
	cc = max(0, lst[2] * x - lst[5]) * lst[8]
	cs = max(0, lst[1] * x - lst[4]) * lst[7]
	if a >= cb + cc + cs:
		return True
	else:
		return False
for ___ in range(t):
	st = input()
	(nb, ns, nc) = map(int, input().split())
	(pb, ps, pc) = map(int, input().split())
	a = int(input())
	(b, s, c) = (0, 0, 0)
	for i in range(len(st)):
		if st[i] == 'B':
			b += 1
		elif st[i] == 'S':
			s += 1
		else:
			c += 1
	lst = [b, s, c, nb, ns, nc, pb, ps, pc]
	l = 0
	r = 10 ** 13
	while r - l > 1:
		mid = (l + r) // 2
		if func(mid, lst, a) == True:
			l = mid
		else:
			r = mid
	if func(r, lst, a) == True:
		print(r)
	else:
		print(l)


rec = input()
n = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
rb = rec.count('B')
rs = rec.count('S')
rc = rec.count('C')
(nb, ns, nc) = (n[0], n[1], n[2])
(pb, ps, pc) = (p[0], p[1], p[2])
rub = int(input())
l = 0
r = 10 ** 14
while l < r:
	mid = (l + r) // 2
	costb = max(0, (mid * rb - nb) * pb)
	costs = max(0, (mid * rs - ns) * ps)
	costc = max(0, (mid * rc - nc) * pc)
	possible = costb + costs + costc <= rub
	if possible:
		l = mid + 1
	else:
		r = mid
print(r - 1)


import time, math
from sys import stdin, stdout

def I():
	return stdin.readline()

def In():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def Si():
	return sorted(list(map(int, stdin.readline().split())))

def Si():
	return sorted(list(map(int, stdin.readline().split())), reverse=True)

def P(x):
	return stdout.write(str(x) + '\n')

def Pi(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')
st = input()
(b, s, c) = (0, 0, 0)
for ele in st:
	if ele == 'B':
		b += 1
	elif ele == 'S':
		s += 1
	else:
		c += 1
avail = L()
price = L()
p = int(input())
(l, r) = (0, int(10000000000000.0))
while r > l + 1:
	mid = (l + r) // 2
	fx = max(0, (mid * b - avail[0]) * price[0]) + max(0, (mid * s - avail[1]) * price[1]) + max(0, (mid * c - avail[2]) * price[2])
	if fx <= p:
		l = mid
	else:
		r = mid
print(l)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'

def check(md):
	(totb, tots, totc) = (cnt['B'] * md, cnt['S'] * md, cnt['C'] * md)
	totb = max(totb - nb, 0)
	totc = max(totc - nc, 0)
	tots = max(tots - ns, 0)
	return totb * pb + totc * pc + tots * ps <= tot
s = si()
cnt = dict()
cnt['B'] = 0
cnt['S'] = 0
cnt['C'] = 0
for i in s:
	cnt[i] += 1
(nb, ns, nc) = f()
(pb, ps, pc) = f()
tot = ii()
(lt, rt) = (0, 10 ** 14)
while lt < rt:
	md = (lt + rt + 1) // 2
	if check(md):
		lt = md
	else:
		rt = md - 1
print(lt)


w = input()
(nb, ns, nc) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
m = int(input())
b = w.count('B')
s = w.count('S')
c = w.count('C')

def fun(x):
	return max(0, b * x - nb) * pb + max(0, c * x - nc) * pc + max(0, s * x - ns) * ps
if s == 0:
	j = 0
else:
	j = int((ns + m / ps) / s)
if b == 0:
	k = 0
else:
	k = int((nb + m / pb) / b)
if c == 0:
	l = 0
else:
	l = int((nc + m / pc) / c)
t = max(j, k, l)
(f, g) = (0, t)
while g - f > 1:
	y = int((f + g) / 2)
	if m >= fun(y):
		(f, g) = (y, g)
	else:
		(f, g) = (f, y)
if m >= fun(g):
	print(g)
else:
	print(f)


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
s = S()
cnt = Counter(s)
b = cnt['B']
s = cnt['S']
c = cnt['C']
(nb, ns, nc) = R()
(pb, ps, pc) = R()
money = I()
r = 10 ** 20
l = 0
while r - l > 1:
	m = (r + l) // 2
	if max(m * b - nb, 0) * pb + max(m * s - ns, 0) * ps + max(m * c - nc, 0) * pc <= money:
		l = m
	else:
		r = m
print(l)


s = input()
(nb, ns, ne) = map(int, input().split())
(pb, ps, pe) = map(int, input().split())
r = int(input())
bfor1 = s.count('B')
sfor1 = s.count('S')
cfor1 = s.count('C')
minBurg = 0
maxBurg = 10 ** 13
while minBurg < maxBurg:
	burg = (minBurg + maxBurg) // 2
	cost = max(burg * bfor1 - nb, 0) * pb + max(burg * sfor1 - ns, 0) * ps + max(0, burg * cfor1 - ne) * pe
	if cost <= r:
		minBurg = burg + 1
	else:
		maxBurg = burg
print(maxBurg - 1)


g = input()
(b, s, c) = list(map(int, input().split()))
(m_b, m_s, m_c) = list(map(int, input().split()))
money = int(input())
b1 = g.count('B')
s1 = g.count('S')
c1 = g.count('C')
total = 0
if max(b, s, c) > max(b1, s1, c1):
	while True:
		v1 = b - b1
		v2 = s - s1
		v3 = c - c1
		if min(v1, v2, v3) < 0:
			break
		else:
			b = v1
			s = v2
			c = v3
			total += 1
i = 0
j = 10 ** 12
mxm = 0
while i <= j:
	mid = (i + j) // 2
	b_need = mid * b1
	s_need = mid * s1
	c_need = mid * c1
	cost = 0
	if b_need < b:
		pass
	else:
		b_more = b_need - b
		cost += b_more * m_b
	if s_need < s:
		pass
	else:
		s_more = s_need - s
		cost += s_more * m_s
	if c_need < c:
		pass
	else:
		c_more = c_need - c
		cost += c_more * m_c
	if cost == money:
		mxm = mid
		i = mid + 1
	elif cost < money:
		if mxm < mid:
			mxm = mid
		i = mid + 1
	elif cost > money:
		j = mid - 1
print(mxm + total)


type = input()
need_B = type.count('B')
need_S = type.count('S')
need_C = type.count('C')
(now_B, now_S, now_C) = list(map(int, input().split(' ')))
(price_B, price_S, price_C) = list(map(int, input().split(' ')))
money = int(input())
left = 0
right = 100000000000000.0
ans = 0

def need_money(mid):
	cb = mid * need_B - now_B
	cs = mid * need_S - now_S
	cc = mid * need_C - now_C
	cb = max(cb, 0)
	cs = max(cs, 0)
	cc = max(cc, 0)
	nm = cb * price_B + cs * price_S + cc * price_C
	return nm
while left < right:
	mid = int((left + right) / 2)
	nm = need_money(mid)
	if nm <= money:
		ans = mid
		left = mid + 1
	else:
		right = mid - 1
if need_money(ans + 1) <= money:
	ans = ans + 1
print(int(ans))


import sys
import math
data = sys.stdin.readlines()
h = [data[0].count('B'), data[0].count('S'), data[0].count('C')]
n = list(map(int, data[1].split()))
p = list(map(int, data[2].split()))
r = int(data[3])
s = 0
ham = 0
for i in range(3):
	s += p[i] * h[i]
	if h[i] == 0:
		n[i] = 0
while sum(n) > 0 and r > 0:
	for i in range(3):
		while n[i] < h[i]:
			n[i] += 1
			r -= p[i]
	while n[0] >= h[0] and n[1] >= h[1] and (n[2] >= h[2]) and (r >= 0):
		for i in range(3):
			n[i] -= h[i]
		ham += 1
if r >= 0:
	ham += math.floor(r / s)
print(ham)


from collections import Counter, defaultdict, deque
import heapq as hq
import math
import sys
input = sys.stdin.readline
recepie = input()
stock = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
rub = int(input())
c = Counter(recepie)
upper = rub + 1000

def check(x):
	buyb = min(0, (stock[0] - c['B'] * x) * prices[0])
	buys = min(0, (stock[1] - c['S'] * x) * prices[1])
	buyc = min(0, (stock[2] - c['C'] * x) * prices[2])
	return rub + buyb + buys + buyc >= 0

def first_false(k):
	left = 0
	right = k
	while left < right:
		mid = (left + right) // 2
		if check(mid):
			left = mid + 1
		else:
			right = mid
	return right
print(first_false(upper) - 1)


from sys import exit
recipe = list(input())
enoughFridge = True
(breadNum, sausageNum, cheeseNum) = map(int, input().split())
fridgeNum = [breadNum, sausageNum, cheeseNum]
(breadCost, sausageCost, cheeseCost) = map(int, input().split())
costs = [breadCost, sausageCost, cheeseCost]
cheapest = min(costs)
money = int(input())
burgersNum = 0
recipeNum = [recipe.count('B'), recipe.count('S'), recipe.count('C')]
for i in range(len(recipeNum)):
	if recipeNum[i] == 0:
		fridgeNum[i] = 0
while True:
	if enoughFridge:
		for i in range(len(recipeNum)):
			if recipeNum[i] > fridgeNum[i]:
				enoughFridge = False
	if enoughFridge:
		burgersNum += 1
		for i in range(len(recipeNum)):
			fridgeNum[i] -= recipeNum[i]
	else:
		for i in range(len(recipeNum)):
			if fridgeNum[i] < recipeNum[i]:
				if (recipeNum[i] - fridgeNum[i]) * costs[i] <= money:
					money -= (recipeNum[i] - fridgeNum[i]) * costs[i]
					fridgeNum[i] += recipeNum[i] - fridgeNum[i]
				else:
					print(burgersNum)
					exit()
			enoughFridge = True
	if sum(fridgeNum) == 0:
		burgersNum += money // (costs[0] * recipeNum[0] + costs[1] * recipeNum[1] + costs[2] * recipeNum[2])
		print(burgersNum)
		exit()


s = input()
bread = s.count('B')
cheese = s.count('C')
sauce = s.count('S')
(b, s, c) = map(int, input().split())
(pb, ps, pc) = map(int, input().split())
amount = int(input())
lo = 0
hi = 10 ** 14

def bs(m):
	r = amount
	tempbread = bread * m
	tempcheese = cheese * m
	tempsauce = sauce * m
	if tempbread > b:
		reqb = tempbread - b
		if pb * reqb > r:
			return False
		r -= reqb * pb
	if tempcheese > c:
		reqc = tempcheese - c
		if pc * reqc > r:
			return False
		r -= reqc * pc
	if tempsauce > s:
		reqs = tempsauce - s
		if ps * reqs > r:
			return False
	return True
while lo <= hi:
	m = lo + (hi - lo) // 2
	if bs(m):
		lo = m + 1
	else:
		hi = m - 1
print(hi)


def cost(burgers, receipt, n, p):
	return sum([max(burgers * receipt[i] - n[i], 0) * p[i] for i in range(3)])
s = input()
receipt = (s.count('B'), s.count('S'), s.count('C'))
n = tuple(map(int, input().split()))
p = tuple(map(int, input().split()))
r = int(input())
left = -1
right = 10 ** 15
while left < right - 1:
	mid = left + (right - left) // 2
	if cost(mid, receipt, n, p) > r:
		right = mid
	else:
		left = mid
print(left)


burger = input()
(Nb, Ns, Nc) = map(int, input().split())
(Pb, Ps, Pc) = map(int, input().split())
r = int(input())
B = burger.count('B')
S = burger.count('S')
C = burger.count('C')
left = 0
right = 10 ** 14
while left < right:
	middle = (left + right + 1) // 2
	b = max(middle * B - Nb, 0)
	s = max(middle * S - Ns, 0)
	c = max(middle * C - Nc, 0)
	if b * Pb + s * Ps + c * Pc > r:
		right = middle - 1
	else:
		left = middle
print(right)


a = input()
(br, sr, cr) = (a.count('B'), a.count('S'), a.count('C'))
(b, s, c) = map(int, input().split())
(bc, sc, cc) = map(int, input().split())
m = int(input())

def ok(x):
	tm = m
	tm -= max(0, br * x - b) * bc
	tm -= max(0, sr * x - s) * sc
	tm -= max(0, cr * x - c) * cc
	return tm >= 0
lo = 0
hi = 100000000000000
while lo < hi:
	mi = (lo + hi + 1) // 2
	if ok(mi):
		lo = mi
	else:
		hi = mi - 1
print(lo)