C. Hamburgers

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 nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc 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 nbnsnc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pbpspc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — 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

 http://codeforces.com/problemset/problem/371/C

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio> 
#include<queue>
#include<math.h>
using namespace std;
int B,S,C;//一个汉堡需要的 
int nb,nc,ns;//已有的 
int pb,pc,ps;//单价
long long money;
long long l,r,mid;
bool check(long long mid)
{
    long long 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;
    return need<=money;
}
int main()
{
    char a[110];int len;
    cin>>a+1;len=strlen(a+1);
    cin>>nb>>ns>>nc;
    cin>>pb>>ps>>pc; 
    cin>>money; 
    for(int i=1;i<=len;i++)
    {
        if(a[i]=='B')    B++;else
        if (a[i]=='C')    C++;else
        if(a[i]=='S')    S++;
    }
    l=0;r=money+max(max(nb,nc),ns);//这里要考虑最大值,原来的,加钱数。 
  二分时,一定要注意左右边界!!!!!
while(l<=r)//二分答案,能做出的汉堡数 { mid=(l+r)>>1; if(check(mid)) l=mid+1; else r=mid-1; } cout<<r; return 0; }
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio> 
#include<queue>
#include<math.h>
using namespace std;
int B,S,C;//一个汉堡需要的 
int nb,nc,ns;//已有的 
int pb,pc,ps;//单价
long long money;
long long ans;
long long need=0;
bool check(int mid)
{
    need=0;
    if(mid*B>nb&&B)     need+= (mid*B-nb)*pb;
    
    if(mid*S>ns&&S)  need += (mid*S-ns)*ps;

    if(mid*C>nc&&C)  need += (mid*C-nc)*pc;
    
    return need<=money;    
}
int main()
{
    char a[110];int len;
    cin>>a+1;len=strlen(a+1);
    cin>>nb>>ns>>nc;
    cin>>pb>>ps>>pc; 
    cin>>money; 
    for(int i=1;i<=len;i++)
    {
        if(a[i]=='B')    B++;else
        if (a[i]=='C')    C++;else
        if(a[i]=='S')    S++;
    }        
    while(check(10000))
    {
        nb=nc=ns=0;
        ans+=10000;
        money-=need;
    }
    while(check(1000))
    {
        nb=nc=ns=0;
        ans+=1000;
        money-=need;
    }
    while(check(100))
    {
        nb=nc=ns=0;
        ans+=100;
        money-=need;
    }    
    while(check(1))
    {
        if(B>=nb)    nb=0;else nb-=B;
        if(S>=ns)    ns=0;else ns-=S;
        if(C>=nc)    nc=0;else nc-=C;//这个处理就花费了我大部分时间,本来是写在check里的但是,对剩余材料的处理,必须是check==1时才能进行。所以要写在while里
        ans+=1;
        money-=need;
    }
    cout<<ans;
    return 0;
} //当然这并不是真正的贪心,再说吧。
原文地址:https://www.cnblogs.com/CLGYPYJ/p/7027998.html