One day, Lazy Bob was sitting on his chair on the side of the freeway, as he usually does, just staring into empty space (and watching for trucks of course). When all of a sudden, a genie showed up out of nowhere in a sick Lambo. The genie was named Genie Pre-Trained Transformer, or GPT for short, but most people call him Generous George.
Unfortunately, George was not as generous as his name implied, because instead of granting Bob three wishes, he gave Bob a puzzle. However, if Bob managed to solve this puzzle, he would receive an infinite amount of money (which Bob loves, because he wants his own collection of Lamborghinis). The puzzle is defined as follows:
• You are given a number containing $$$N$$$ digits ($$$1 ≤ N ≤ 200,000$$$). All of the digits are either $$$1$$$ or $$$2$$$.
• You are also given $$$K$$$ ($$$0 ≤ K ≤ 10^9$$$) coins which you can use to perform operations on the number.
• The first operation allows you to replace any digit with a $$$1$$$ for cost $$$c_1$$$ and with a $$$2$$$ for cost $$$c_2$$$.
• The second operation allows you to swap any two digits at indices $$$i$$$ and $$$j$$$ for cost $$$|i - j|$$$.
• Using only the coins that you have, minimize the number with these two operations.
Lazy Bob took one good look at this puzzle and decided that he could not bother to solve it. However, the prospect of having infinite Lambos (and maybe even his own trucks) does entice him. The genie also said something about having multiple guesses for the minimum, but each guess will cause Bob to lose $1 out of his infinite amount (unfortunately, Bob does not understand the concept of infinity, so he does not realize he could just keep guessing until he gets the right answer).
Therefore, Bob tasks you with solving this puzzle for him, and only gives you one try: he cannot afford to lose a single dollar. Help him find the minimum possible value the number can have.
Line 1: Four space-separated integers $$$N, K, c_1, c_2$$$ ($$$1 ≤ N ≤ 200,000$$$, $$$0 ≤ K, c_1, c_2 ≤ 10^9$$$). Line 2: One integer with $$$N$$$ digits (all of which are either $$$1$$$ or $$$2$$$) representing the number that Generous George gave to Lazy Bob.
Line 1: An $$$N$$$ digit number (all of which are either $$$1$$$ or $$$2$$$) representing the minimum possible number after some amount of operations.
2 1 2 321
12
In this example, Lazy Bob has $$$1$$$ coin, which is not enough for either of the replace operations. However, he can swap the digits at positions $$$1$$$ and $$$2$$$ with a cost of $$$|1-2|$$$ = $$$1$$$ coin, and get the minimum possible number: $$$12$$$.