Make a binary string as small as possible
A binary string S of length N stands for a number. You have CASH coins. Swapping any two characters costs A coins and flipping one character costs B coins; you can only do an operation you can pay for. Make the number as small as possible and print it modulo 10^9 + 7.
Reported in: Infosys (SP / DSE · earlier papers) (memory-based)
Input
First line: N. Second line: S. Third line: CASH A B.
Output
The smallest number modulo 10^9 + 7.
Constraints
1 ≤ N ≤ 1000; 0 ≤ CASH ≤ 10^9; 1 ≤ A, B ≤ 10^9.
Example 1
Input:
5
11010
4 1 3
Output:
3Example 2
Input:
4
1000
2 1 5
Output:
1Example 3
Input:
3
111
0 1 1
Output:
7Example 4
Input:
6
101101
5 2 3
Output:
7Hint: Only the highest 1s matter. With w swaps and f flips (w·A + f·B ≤ CASH), the best you can do is clear the top w + f ones and drop the w swapped ones onto the lowest 0s. For each w, use as many flips as the coins allow, and keep the smallest result.