【codeforce】 Hamburgers

来源:互联网 发布:js bytearray 遍历 编辑:程序博客网 时间:2024/06/05 02:40

Hamburgers
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 EnglishC).

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 cincout streams or the %I64dspecifier.

Output

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

Sample Input

Input
BBBSSC6 4 11 2 34
Output
2
Input
BBC1 10 11 10 121
Output
7
Input
BSC1 1 11 1 31000000000000
Output
200000000001


和上题类似

代码:

#include<cstdio>#include<cstring>#include<algorithm>#define MAXN 10000000000000using namespace std;char str[110];long long nb,ns,nc,pb,ps,pc;long long r;long long b=0,s=0,c=0;bool judge(long long mid)//二分可以做成的面包的个数,判断此时总钱数是否够用,即r>=0? {long long NB=mid*b,NS=mid*s,NC=mid*c; long long temp=r;//因为每次判断 r 的值还是原值,所以每次用其他变量代替 if(NB>nb)temp-=pb*(NB-nb);if(NS>ns)temp-=ps*(NS-ns);if(NC>nc)temp-=pc*(NC-nc);if(temp>=0)return 1;return 0; }int main(){scanf("%s",str);scanf("%lld%lld%lld",&nb,&ns,&nc);scanf("%lld%lld%lld",&pb,&ps,&pc);scanf("%lld",&r);int l=strlen(str);for(int i=0;i<l;i++)//一个汉堡每种原料的个数 {if(str[i]=='B')b++;if(str[i]=='S')s++;if(str[i]=='C')c++;}long long left=0,right=MAXN,mid;//r 的范围0~10^12, long long ans;//或ans=0;while(left<=right){mid=(left+right)/2;if(judge(mid))//总钱数够用,左区间右移,记录面包个数 {left=mid+1;ans=mid;}else//钱不够,右区间压缩 {right=mid-1;} }printf("%lld\n",ans);return 0; }   


0 0
原创粉丝点击