Binary Transformations
Time limit1sMemory limit256 MB
Given starting bits, target bits, and per-bit costs, flipping a bit i costs the sum of costs of all bits equal to 1 after the flip; find the minimum total price to reach the target.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
There are bits numbered from to . Bit starts with the value , which is or , and its cost is .
One operation picks a bit and flips its value, so becomes and becomes . The price of that operation is the sum of over every bit whose value is after the flip. When bit itself turns into , its own cost is part of that sum.
You may apply the operation to any bit, any number of times, in any order. Find the smallest total price that makes bit hold the value for every .
Input
The first line contains the integer (), the number of bits.
The second line contains integers (), the cost of each bit.
The third line contains a string of characters , the starting values.
The fourth line contains a string of characters , the required values.
Output
Print the smallest total price on one line.