eolymp
bolt
Try our new interface for solving problems
Problems

The Agency

The Agency

Following in the footsteps of a number of flight searching startups you want to create the fi rst interplanetary travel website. Your fi rst problem is to quickly fi nd the cheapest way to travel between two planets. You have an advantage over your competitors because you have realized that all the planets and the flights between them have a special structure. Each planet is represented by a string of \textbf{N} bits and there is a flight between two planets if their \textbf{N}-bit strings diff er in exactly one position. The cost of a flight is the cost of landing on the destination planet. If the \textbf{i}-th character in a planet's string is a \textbf{1} then the \textbf{i-}th tax must be paid to land. The cost of landing on a planet is the sum of the applicable taxes. Given the starting planet, ending planet, and cost of the ith tax compute the cheapest set of flights to get from the starting planet to the ending planet. \InputFile Input for each test case will consist of two lines. The first line will have \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{1000}), the number of bits representing a planet; \textbf{S}, a string of \textbf{N} zeroes and ones representing the starting planet; and \textbf{E}, a string representing the ending planet in the same format. The second line will contain \textbf{N} integers the \textbf{i}-th of which is the cost of the \textbf{i}-th tax. All costs will be between \textbf{1} and \textbf{1000000}. The input will be terminated by a line with a single \textbf{0}. \OutputFile For each test case output one number, the minimum cost to get from the starting planet to the ending planet, using the format given below.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3 110 011
3 1 2
5 00000 11111
1 2 3 4 5
4 1111 1000
100 1 1 1
30 000000000000000000000000000000 111111111111111111111111111111
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
0
Output example #1
Case 1: 4
Case 2: 35
Case 3: 106
Case 4: 4960

Example description: Penultimate two lines in the input data and long on your monitor may be spread over 2 lines each.

Source 2011 East Central Regional Contest