AmazonOA #46
Get Maximum Profit (Trade with K-Run Cap)
Estimated EasyJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Within each original maximal run of identical consecutive characters in the command string, at most k commands can be executed, and other commands may be skipped; choices do not change the original runs. Every command has a positive profit; find the maximum total profit.
Input
The first line contains n k (1≤k≤n≤200000), the second line contains a lowercase command string of length n, and the third line contains n profits (1..10⁹).
Output
Output a single integer answer.
Example 1
Input:
5 2
abbba
1 4 2 10 3
Output:
18
Example 2
Input:
3 1
aaa
1 2 3
Output:
3
Example 3
Input:
3 1
aba
1 2 3
Output:
6
Example notes
Example 1: the a at each end forms its own run, giving profits 1+3; the middle bbb takes 10 and 4, for a total of 18. Example 2: the only run can take at most 1 command, so choose profit 3. Example 3: each of the three original runs has only one command and all can be executed, for a total of 6.
Editorial
Loading solution…