Skip to main content
Back to problems
Codeforces
Easy
Greedy
Strings
DZY Loves Strings

Build a string by repeatedly picking the highest-weight available letters and then append extra copies of the best letter.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

You are given a lowercase string ss, an integer kk, and 26 integer weights for letters aa to zz.

You need to construct a new string by taking the letters of ss in order of decreasing weight. For letters with the same weight, keep their natural alphabetical order. After that, append kk 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 ss consisting of lowercase English letters.
  • The second line contains an integer kk.
  • The third line contains 26 integers: the weights of letters aa through zz.

Output Format

Output the constructed string after reordering ss and appending kk extra copies of the maximum-weight letter.

Constraints

  • 1s1051 \le |s| \le 10^5
  • 0k1050 \le k \le 10^5
  • Each weight fits in a 32-bit signed integer
  • ss contains only lowercase English letters
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.