#P3828. Matchstick Equation Transformation

    ID: 17078 Type: Default 1000ms 256MiB

Matchstick Equation Transformation

Matchstick Equation Transformation

In this problem, you are given an equation of the form A = B where A and B are two numbers having the same number of digits. Each digit is represented in a 7‐segment (matchstick) style according to the image below:

0: 1111110
1: 0110000 (the two matchsticks for digit 1 must be placed on the right-hand side)
2: 1101101
3: 1111001
4: 0110011
5: 1011011
6: 1011111
7: 1110000
8: 1111111
9: 1111011

Initially the matchsticks form an equation that might be incorrect. You are allowed to perform three types of operations on the matchsticks:

  1. Add a matchstick at an arbitrary position.
  2. Remove a matchstick from an arbitrary position.
  3. Move a matchstick from one position to another.

After performing all operations, the numbers on both sides of the equals sign must be valid (i.e. one of the digits 0–9 as defined above) and the entire equation must be correct (both sides equal digit‐by‐digit). In addition, the following restrictions apply:

  • You cannot completely remove any digit. That is, while you may remove some matchsticks from a digit, you cannot make it vanish.
  • You cannot create a new digit. You may only add a matchstick to, or move a matchstick onto, an existing digit.
  • You cannot split or merge digits (for example, you may not turn an 8 into two 1’s or merge two 1’s into an 8).
  • For digit 1 the matchsticks must be arranged on the right‐hand side; any other arrangement doesn’t form a valid 1.

Each operation has a cost. If an add operation is the i-th add performed overall its cost is \( p_{1}\times i+q_{1} \). Similarly, the i-th remove operation costs \( p_{2}\times i+q_{2} \) and the i-th move operation costs \( p_{3}\times i+q_{3} \).

For example, if 3 adds, 1 move and 2 removals are performed (in that order for each type), the total cost will be:

[ \Bigl[(p_{1}\times1+q_{1})+(p_{1}\times2+q_{1})+(p_{1}\times3+q_{1})\Bigr] + (p_{3}\times1+q_{3}) + \Bigl[(p_{2}\times1+q_{2})+(p_{2}\times2+q_{2})\Bigr]. ]

Your task is to determine the minimum total cost needed to transform the given (possibly incorrect) equation into a correct one.


Note on matchstick transformations for a digit: Suppose you want to change a digit X to a digit Y. Denote the 7‐segment representation of X by a 7–bit string and that of Y likewise. Let the number of segments that are off in X but on in Y be a (these require an add) and the number of segments on in X but off in Y be r (these require a removal). Since you can move a matchstick to cover both an add and a removal simultaneously, let m = min(a,r). Then the transformation from X to Y can be achieved by performing m move operations, a − m add operations and r − m removal operations.

inputFormat

The input consists of two lines.

The first line contains six space–separated integers: p1, q1, p2, q2, p3, q3.

The second line contains an equation of the form A = B (with no spaces or with spaces removed) where A and B have the same number of digits. Each digit is one of 0–9 and is drawn in a 7–segment style as described above.

outputFormat

Output a single integer, the minimum total cost required to transform the given equation into a correct equation.

sample

1 1 1 1 1 1
1=1
0