A bit string is a string made only of 0 and 1. You are given one bit string and must turn it into a fixed target string. The only operation you may use is swapping two adjacent bits.
The starting string is given as a plain sequence of bits. The target string is given as a run code instead. A run code lists the lengths of the consecutive blocks of 0s or 1s in the string, in order from the left. For example, the run code of "011100" is "1 3 2". Two strings always share a given run code: one that starts with 0 and one that starts with 1.
Compute the smallest number of operations needed to turn the starting string into the target string.
The first line has two integers N (1≤N≤15) and M (1≤M≤N). The second line has the N bits of the starting string, separated by spaces. The third line has the run code of the target string as M integers, in order.
Every number in the run code is at least 1, and the numbers add up to N. The given bit string can always be turned into the string that the run code describes.
Print the smallest number of operations needed.
Take the bit string "100101" and a target run code of "1 3 2". The two possible target strings are "011100" and "100011". Reaching "011100" takes 4 operations, while reaching "100011" takes only 1. The answer is therefore 1.