Maximum subsequences
Practice
3.7 (7 votes)
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
82% Success 539 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Consider a string \(s\) of length \(n\) that consists of lowercase Latin alphabetic characters. You are given an array \(A\) of size 26 showing the value for each alphabet. You must output the subsequence of size \(k\) whose sum of values is maximum.

If there are multiple subsequences available, then print the lexicographically smallest sequence. 

String \(p\) is lexicographically smaller than string \(q\) if \(p\) is a prefix of \(q\) and is not equal to \(q\) or there exists \(i\), such that \(p_i < q_i\) and for all \(j < i\) it is satisfied that \(p_j = q_j\). For example, '\(abc\)' is lexicographically smaller than '\(abcd\)', '\(abd\)' is lexicographically smaller than '\(abec\)', and '\(afa\)' is not lexicographically smaller than '\(ab\)'.

Input format

  • The first line contains an integer \(t\) representing the number of test cases.
  • For each test case, you are given two integers \(n\) and \(k\), a string \(s\), and an array \(A\) of length 26.

Output format

Print a string for each test case.

Constraints
\(1\le t\le 1e5\\ 1\le k\le n\le 1e5\\ 0\le A[i]\le 1e9\ (1\le i\le 26)\\  Sum\ of\ n\ over\ t\ test\ cases\ does\ not\ exceed\ 1e5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
17 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:30
7 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:30
5 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms