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 ;
} //当然这并不是真正的贪心,再说吧。

最新文章

  1. SQL 基础语法(一)
  2. python笔记7:日期和时间
  3. 监听调试web service的好工具TCPMon
  4. JAVA的extends用法
  5. 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329
  6. 小情人emacs的自动补全
  7. 常用CDN公共库
  8. Android 使用加速度传感器实现摇一摇功能及优化
  9. Route-map简介
  10. rebar
  11. Ioc容器依赖注入-Spring 源码系列(2)
  12. PHP 安装 phpredis 扩展(二)
  13. 日常报错记录4:ssh工程复制粘贴顺序。
  14. Windows服务器外网无法访问web的解决方法
  15. 【缓存算法】FIFO,LFU,LRU
  16. Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载
  17. 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用
  18. Guns后台管理系统框架(毕业设计神器)
  19. [转]java:IO流学习小结
  20. [转]Linux 命令行快捷键

热门文章

  1. &lt;VS&gt;MFC程序显示命令行窗口
  2. CodeForces - 762E:Radio stations (CDQ分治||排序二分)
  3. python 基础之第十一天(面向对象)
  4. 如何使用 Jmeter 发送 Json 请求
  5. C++之log4cpp库的使用
  6. bzoj 3481 DZY Loves Math III——反演+rho分解质因数
  7. 洛谷P1073最优贸易——双向取值
  8. POJ2406(next原理理解)
  9. 修改winform安装包写日志文件权限
  10. ARM、DSP、FPGA的技术特点和区别