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=;
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[];int len;
cin>>a+;len=strlen(a+);
cin>>nb>>ns>>nc;
cin>>pb>>ps>>pc;
cin>>money;
for(int i=;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)>>;
if(check(mid))
l=mid+;
else r=mid-;
}
cout<<r;
return ;
}
#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=;
bool check(int mid)
{
need=;
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[];int len;
cin>>a+;len=strlen(a+);
cin>>nb>>ns>>nc;
cin>>pb>>ps>>pc;
cin>>money;
for(int i=;i<=len;i++)
{
if(a[i]=='B') B++;else
if (a[i]=='C') C++;else
if(a[i]=='S') S++;
}
while(check())
{
nb=nc=ns=;
ans+=;
money-=need;
}
while(check())
{
nb=nc=ns=;
ans+=;
money-=need;
}
while(check())
{
nb=nc=ns=;
ans+=;
money-=need;
}
while(check())
{
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+=;
money-=need;
}
cout<<ans;
return ;
} //当然这并不是真正的贪心,再说吧。

C. Hamburgers的更多相关文章

  1. Codeforces Round #218 (Div. 2) C. Hamburgers

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. CodeForces 371C Hamburgers

    B题又耽误时间了...人太挫了.... C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes i ...

  3. B - Hamburgers

    Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...

  4. Hamburgers

    Hamburgers http://codeforces.com/problemset/problem/371/C time limit per test 1 second memory limit ...

  5. Codeforces 371C Hamburgers (二分答案)

    题目链接 Hamburgers 二分答案,贪心判断即可. #include <bits/stdc++.h> using namespace std; #define REP(i,n) fo ...

  6. 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers

    题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...

  7. H Kuangyeye and hamburgers

    链接:https://ac.nowcoder.com/acm/contest/338/H来源:牛客网 题目描述 Kuangyeye is a dalao of the ACM school team ...

  8. cf C. Hamburgers

    http://codeforces.com/contest/371/problem/C 二分枚举最大汉堡包数量就可以. #include <cstdio> #include <cst ...

  9. CodeForces 371C Hamburgers(经典)【二分答案】

    <题目链接> 题目大意: 给以一段字符串,其中只包含"BSC"这三个字符,现在有一定量免费的'B','S','C‘,然后如果想再买这三个字符,就要付出相应的价格.现在总 ...

随机推荐

  1. easyui-tabs 在ie8下基于iframe嵌套页面加载成功后切换空白问题

    这是一个很坑的问题,由于项目必须支持ie8的情况下,产生了这个问题.在我进行逐步对比的分析过后,终于发现了原因所在:

  2. HTML标题和段落

    HTML标题 通过<h1> - <h6> 等标签进行定义 <h1>一标题</h1> <h2>二标题</h2> <h3> ...

  3. 使用grunt中遇到的问题

    1.使用jshint进行代码检查时,grunt命令后报错: 因为出现了乱码,我猜测是因为编码原因导致的.遂在webstorm的setting中修改了编码为utf-8,问题解决.

  4. Spring 事务管理高级应用难点剖析: 第 2 部分

    本文是“Spring 事务管理高级应用难点剖析” 系列文章的第 2 部分,作者将继续深入剖析在实际 Spring 事务管理应用中容易遇见的一些难点,包括混合使用多种数据访问技术(如 Spring JD ...

  5. npm 引入第三方过滤器

    根据项目具体情况使用过滤器,如果不满足业务需求可以在vue的全局或者局部自定义fiter(过滤器) 一下是github提供的第三方过滤器: https://github.com/freearhey/v ...

  6. JUC类图

    JUC的类图总览.

  7. java:calendar类及一些比较实用的utils(二)

    在这里将我在项目中用到的一些关于使用Calendar的utils分享出来,只是一部分,后期遇到好的通用方法会继续添加,以和大家交流学习,如果你还不熟悉这个类的使用,但是急需某个util,可以加群:41 ...

  8. java定时器Timer的使用

    Time类主要负责完成定时计划任务的功能,就是在指定的时间的开始执行某个任务. Timer类的作用是设置计划任务,而封装任务内容的类是TimerTask类.此类是一个抽象类,继承需要实现一个run方法 ...

  9. java读取文件:二进制文件

    FileWriter只能接受字符串形式的参数,也就是说只能把内容存到文本文件.相对于文本文件,采用二进制格式的文件存储更省空间 InputStream用于按字节从输入流读取数据.其中的int read ...

  10. python之文件的读写(2)

    小R昨天因为在研究weblogic的漏洞就没来得及学习python(好吧,这都是借口,懒了,大家可不能像我这样.要坚持每天都学).   这个进度是有点慢呀.哎呀,没事没事,我还年轻,才20岁.  哈哈 ...