题目

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 English C).

The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (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.

Sample Input

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

题解

题意比较容易理解
思路也很清晰
但是需要考虑的情况太多了
很容易因为一个小细节而出现问题

第一次见到用 二分法 来解决这种问题的题

列举所有可能的答案
采用二分来找结果
由于最多也只是 64 位存储的内容
也即采用二分只需要查询64次

查询第一个严格大于的结果
再将结果减去 1 就是答案

比较神奇的思路
记录下模板

long long Division(long long l,long long r) {
    if(l == r) {
        return l;
    }
    long long mid = (l + r) / 2;
    if(Could(mid))
        return Division(l,mid);
    else
        return Division(mid + 1,r);
}
bool Could(long long n) {
    long long money = 0;
    for(int i = 0;i < maxn;i++) 
        if(n*need[i] > num[i])
            money += p[i] * (n*need[i]-num[i]);
    return money > r;
}

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <bitset>
#include <iomanip> 
using namespace std;

const int maxn = 3;

long long need[maxn];
long long num[maxn];
long long p[maxn];
long long temp[maxn];
long long r;
long long per;

bool Could(long long n) {
    long long money = 0;
    for(int i = 0;i < maxn;i++) 
        if(n*need[i] > num[i])
            money += p[i] * (n*need[i]-num[i]);
    return money > r;
}

long long Division(long long l,long long r) {
    if(l == r) {
        return l;
    }
    long long mid = (l + r) / 2;
    if(Could(mid))
        return Division(l,mid);
    else
        return Division(mid + 1,r);

}

bool Do() {
    string s;
    if(!(cin >> s))
        return false;

    need[0] = need[1] = need[2] = 0;
    for(size_t i = 0;i < s.size();i++) {
        if(s[i] == 'B')
            need[0]++;
        if(s[i] == 'S')
            need[1]++;
        if(s[i] == 'C')
            need[2]++;
    }

    per = 0;

    for(int i = 0;i < 3;i++)
        cin >> num[i];
    for(int i = 0;i < 3;i++) {
        cin >> p[i];
        per += need[i] * p[i];
    }
    cin >> r;
    cout << Division(0,2000000000000) - 1 << endl;

    return true;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    while(Do());
    return 0;
}