#include <bits/stdc++.h>
using namespace std;
map<char, int> mp;
int nb, ns, nc, pb, ps, pc;
bool fun(unsigned long long int x, unsigned long long int p) {
  unsigned long long int temp;
  if (nb < x * mp['B']) {
    temp = (x * mp['B']) - nb;
    if (p >= pb * temp)
      p = p - (pb * temp);
    else
      return false;
  }
  if (ns < x * mp['S']) {
    temp = (x * mp['S']) - ns;
    if (p >= ps * temp)
      p = p - (ps * temp);
    else
      return false;
  }
  if (nc < x * mp['C']) {
    temp = (x * mp['C']) - nc;
    if (p >= pc * temp)
      p = p - (pc * temp);
    else
      return false;
  }
  return true;
}
int main() {
  string s;
  cin >> s;
  unsigned long long int l, h, m, p;
  for (int i = 0; i < s.length(); i++) {
    mp[s[i]]++;
  }
  cin >> nb >> ns >> nc >> pb >> ps >> pc;
  cin >> p;
  l = 0, h = 10000000000000, m = (l + h) / 2;
  while (l < h - 1) {
    if (fun(m, p) == true) {
      l = m;
    } else {
      h = m;
    }
    m = (l + h) / 2;
  }
  cout << l;
  return 0;
}
