Hamburgers assuming the solution is feasible

Source: Internet
Author: User

HamburgersTime limit:1000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatus

Description

Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there is 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" represent s the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.

Polycarpus has  n b  pieces of bread,   n s  pieces of sausage And  n c  pieces of cheese in the kitchen. Besides, the shop nearby have all three ingredients, the prices Are  p b  rubles for a piece of Bread,  p s  for a piece of sausage and  P c  for a piece of cheese.

Polycarpus has r rubles and he are ready-to-shop on them. What maximum number of hamburgers can he cook? You can assume this polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has a 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, the string contains only letters 'B ' (uppercase 中文版b), 'S ' ( Uppercase 中文版S) and 'C ' (uppercase English C).

The second line contains three integersNb,Ns,NC(1≤Nb,N s , n C ≤100)-the number of the pieces of B Read, sausage and cheese on polycarpus ' kitchen. The third line contains three Integers  p b ,   p s ,   P C   (1≤ p b , p s , p C ≤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, don't write the %lld specifier to read or write 64-bit integers inс++. It is preferred to use the CIN, cout 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
Bbbssc
6 4 1
1 2 3
4
Output
2
Input
Bbc
1 10 1
1 10 1
21st
Output
7
Input
Bsc
1 1 1
1 1 3
1000000000000
Output
200000000001
1#include <stdio.h>2#include <string.h>3 4 Const intinf=0x3f3f3f3f;5 Const Long Longinf=1e14+7;6 Chara[ the];7 Long LongB,s,c,nb,ns,nc,pb,ps,pc,r;8 9 BOOLCLong Longx)Ten { One     Long LongRB,RS,RC; Arb= (X*B-NB) >=0? (X*B-NB):0, rs= (X*s-ns) >=0? (X*s-ns):0, rc= (X*C-NC) >=0? (X*C-NC):0; -     if((RB*PB+RS*PS+RC*PC) <=R) -         return true; the     Else -         return false; - } -  + intMain () - { +     inti,j,k; A      while(SCANF ("%s", a)! =EOF) at     { -b=0, s=0, c=0; -          for(i=0; a[i]!=' /'; i++) -         { -             if(a[i]=='B') -b++; in             Else if(a[i]=='S') -s++; to             Else if(a[i]=='C') +C++; -         } the         //printf ("%d%d%d", b,s,c); *scanf"%i64d%i64d%i64d%i64d%i64d%i64d",&nb,&ns,&nc,&pb,&ps,&pc); $scanf"%i64d",&R);Panax Notoginseng         Long Longlb=0, ub=INF; -  the          while(ub-lb>1) +         { A             Long LongMid= (LB+UB)/2; the             if(C (mid)) +lb=mid; -             Else $ub=mid; $         } -  -printf"%i64d\n", LB); the     } - Wuyi     return 0; the}
View Code

Hamburgers assuming the solution is feasible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.