DZY Loves Strings

来源:互联网 发布:海岛奇兵升级数据 编辑:程序博客网 时间:2024/05/01 16:53

DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value wc. For each special string s = s1s2... s|s| (|s| is the length of the string) he represents its value with a function f(s), where

Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get?

Input

The first line contains a single string s (1 ≤ |s| ≤ 103).

The second line contains a single integer k (0 ≤ k ≤ 103).

The third line contains twenty-six integers from wa to wz. Each such number is non-negative and doesn't exceed 1000.

Output

Print a single integer — the largest possible value of the resulting string DZY could get.

Sample Input

Input
abc31 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Output
41

Hint

In the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.


将k个字符插入到字符串中,使计算的值最大,由越往后排,坐标越大,因此只需将值较大的字符插到字符串的尾部即可。

代码写的略微有些繁琐,见谅。

#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;


struct node{
    char ch;
    int num;
    node(char a,int b){
        ch=a;
        num=b;
    }
    friend bool operator <(node a,node b){
        if(a.num!=b.num)
        return a.num<b.num;
        else
        return a.ch<b.ch;
    }
};


map<char,int >mp;
vector<node >ve;


int k,sum;
string str;


void solve(){
    sum=0;
    int i;
    for( i=0;i<str.length();i++){
        sum=sum+mp[str[i]]*(i+1);
    }
    sort(ve.begin(),ve.end());
    vector<node>::iterator it=ve.end();
    it--;
    i++;
    while(k-->0){
        sum=sum+(*it).num*i;
        i++;
    }
}


int main(){
    char f;
    int b;
    while(cin>>str){
        cin>>k;
        mp.clear();
        ve.clear();
        for(f='a';f<='z';f++){
            cin>>b;
            mp[f]=b;
            ve.push_back(node(f,b));
        }
        solve();
        cout<<sum<<endl;
    }
    return 0;
}



0 0
原创粉丝点击