#include <bits/stdc++.h>
#include <cstdint>
#include <iomanip>
#define PowerUp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define BattleMode ll t;cin>>t;while(t--)
#define Thor solve();
#define Thunder if(t>0){cout<<line;}
#define ll long long
#define line "\n"
#define line3 cout<<line;
#define stop int abnmj; cin>>abnmj;
#define tap "\t"
#define loop(end,char) for(ll char=0;char<end;char++)
#define loopr(end,char) for(int char=end-1;char>=0;char--)
#define arrin(name) loop(name.size(),p){cin>>name[p];}
#define print(name) for(auto ik:name){cout<<ik<<" ";}
#define print2(name) for(auto ik:name){cout<<ik.first<<" "<<ik.second<<line;}
#define temple template<typename t,typename v>
#define all(x) x.begin(),x.end()
using namespace std;
const double pi = acos(-1);
const ll MOD = 1e9 + 7;
struct burger
{
ll bun, susage, cheese;
};
burger have, cost, recipe;
ll burger_cost(ll number)
{
ll total_cost = 0;
ll bun_cost = max((((number * recipe.bun) - have.bun) * cost.bun), (ll)0);
ll susage_cost = max((((number * recipe.susage) - have.susage) * cost.susage),(ll) 0);
ll cheese_cost = max((((number * recipe.cheese) - have.cheese) * cost.cheese),(ll) 0);
total_cost += bun_cost + susage_cost + cheese_cost;
return total_cost;
}
void solve()
{
string s;
ll budget;
cin>>s;
cin >> have.bun >> have.susage >> have.cheese;
cin >> cost.bun >> cost.susage >> cost.cheese;
cin>>budget;
map<char,ll> mp;
loop(s.size(), i)
{
mp[s[i]]++;
}
recipe.bun = mp['B'];
recipe.susage = mp['S'];
recipe.cheese = mp['C'];
ll left = 0, right = 1e12+1000, mid;
while (left < right)
{
mid = (right - left) / 2 + left;
if (burger_cost(mid)>budget)
right = mid;
else
left = mid+1;
}
if (burger_cost(right)>budget)
right--;
cout<<right;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
PowerUp
Thor
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDx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