Build a string by repeatedly picking the highest-weight available letters and then append extra copies of the best letter.
You are given a lowercase string , an integer , and 26 integer weights for letters to .
You need to construct a new string by taking the letters of in order of decreasing weight. For letters with the same weight, keep their natural alphabetical order. After that, append additional copies of the letter with the highest weight.
Return the final string and its total weight, where the weight of a string is the sum of weights of all its characters.
This is an implementation-style problem that tests sorting by custom keys and careful counting of repeated characters.
Input Format
- The first line contains a string consisting of lowercase English letters.
- The second line contains an integer .
- The third line contains 26 integers: the weights of letters through .
Output Format
Output the constructed string after reordering and appending extra copies of the maximum-weight letter.
Constraints
- Each weight fits in a 32-bit signed integer
- contains only lowercase English letters
Example 1
Input
abac 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Output
cbaaaz
Explanation
The highest-weight letter among those appearing in the string is c, so letters are ordered by decreasing weight as c, b, a. Then 2 extra copies of z are appended only if z is the maximum-weight letter overall; since z has the largest weight in this example, the final appended letters are zz. The illustrative output shows the intended process of sorting by weight and appending the best letter.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.