您的位置:首页 > 其它

DZY Loves Strings

2014-08-19 13:57 239 查看
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
abc
3
1 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: