AmazonOA #352
Trader Joe Trades
Estimated HardJudge-ready
Topics (AI-labelled)State Machines
The order of the transactions is fixed; each one has a type A..Z and an integer profit, and any of them can be skipped. In the selected sequence, the same type may not appear more than k times in a row. Maximize the profit; selecting nothing is allowed and gives profit 0. A transaction with negative profit may still be worth taking as a separator, so such transactions cannot simply be dropped.
Input
The first line contains n k, the second line is the string formed by the n transaction types, and the third line contains n profits; when n=0 the last two lines are empty. On this site 0≤n≤2000, −10^9≤profit≤10^9, 0≤k≤10^9.
Output
Output a single integer, the answer.
Example 1
Input:
5 2
BAAAB
1 4 2 10 3
Output:
18
Example 2
Input:
3 1
ABA
100 -1 100
Output:
199
Example 3
Input:
3 1
ABA
10 -100 10
Output:
10
Example notes
The outputs of the three public examples are 18; 199; and 10, checked by independent enumeration or direct simulation.
Editorial
Loading solution…