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. tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用——模型层次太深,或者太复杂训练时候都不会收敛

    tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用. 数据目录在data,data下放了汉字识别图片: data$ ls0  1  10  11  12  13  14  15 ...

  2. js事件传播的一个疑惑

    在学习事件传播的时候,发现一个问题,当时是这样子的. 我给多层元素分别绑定了冒泡和捕获事件.按道理应该先从外向内执行完所有的捕获事件,再由内向外执行所有的冒泡事件. 但是天不随人愿啊,有个元素偏偏先执 ...

  3. 12306.cn网站自动登录器源代码

    去年过年放假的时候写了一个12306.cn网站的自动登录器,刚好那时候放假了,所以没把源代码放出来,现在将代码发出来,由于编写得比较仓促(从放假的下午19:00左右到晚上到00:00左右),很多细节问 ...

  4. bzoj 1096 仓库建设 —— 斜率优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1096 设 f[i] 为 i 作为最后一个仓库时前 i 个工厂的答案,最后的答案当然是 f[n ...

  5. 从MySQL获取数据

    安装 PM> install-package newtonsoft.json PM> install-package mysql.data string connectionString ...

  6. CS231n 2016 通关 第五、六章 Fully-Connected Neural Nets 作业

    要求:实现任意层数的NN. 每一层结构包含: 1.前向传播和反向传播函数:2.每一层计算的相关数值 cell 1 依旧是显示的初始设置 # As usual, a bit of setup impor ...

  7. UVa-10954

    10954 - Add All Time limit: 3.000 seconds Yup!! The problem name reflects your task; just add a set ...

  8. 技术胖Flutter第三季-14布局RowWidget的详细讲解

    flutter总的地址: https://jspang.com/page/freeVideo.html 视频地址: https://www.bilibili.com/video/av35800108/ ...

  9. Linux useradd 添加用户

    在 linux 中,如果我们想添加一个用户,那么使用的命令如下: 用户管理命令: useradd 基础的命令 命令名称:useradd 命令的所在路径:/usr/bin/useradd 执行权限:ro ...

  10. D3.js(v3)+react 制作 一个带坐标轴与比例尺的折线图

    本章使用路径生成器来绘制一个折线图.以中国和日本的GDP数据为例:   //数据 var dataList = [ { coountry : "china", gdp : [ [2 ...