B - DZY Loves Strings
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.
Example
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
41
Note
In the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.
题目大意是,给你一个字符串,再给你一个可添加的字符数,然后给你一个公式,要使某个值最大化.
根据公式的特点,我们把ai最大的字符放在越后面输出越好,所以,会出现这种形式:
原串+n个ai最大的字符
那么就解决了.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 int a[40],n,ans,x;
6 int main(){
7 char s[10005];
8 scanf("%s",s);
9 scanf("%d",&n);
10 for (int i=0; i<26; i++) scanf("%d",&a[i]),x=max(x,a[i]);
11 int len=strlen(s);
12 for (int i=0; i<len; i++) ans+=(i+1)*a[s[i]-'a'];
13 for (int i=len; i<=len+n-1; i++) ans+=(i+1)*x;
14 printf("%d",ans);
15 return 0;
16 }