Problem K
Conference Seating
You are organizing your very first conference! Unfortunately, you were not able to book a room on campus to host the conference. Strangely, the University was still willing to let you use a long hallway in the Natural Sciences Building.
Each chair may be placed at a position in a $2 \cdot N$ grid in the hallway (thankfully the number of attendees is at most $2 \cdot N$). So the position of each chair can be described by a pair of integers $(r,c)$ where $1 \leq r \leq 2$ and $1 \leq c \leq N$.
The conference takes place over two days. Due to fire regulations you cannot comprehend, the university tells you exactly where to place the chairs in the grid. Even more bewildering, the prescribed chair placements for the first day and the second day are different! So when the first day is done you will move chairs to the arrangement for the second day.
The effort required to move a chair from position $(r,c)$ to position $(r',c')$ is $|r-r'| + |c-c'|$. It is ok if you move a chair over another chair. The total effort in rearranging the chairs is the sum of the efforts of moving each chair.
Find the minimum total effort required to rearrange the chairs!
Input
The first line of input contains a single integer $N$ ($2 \leq N \leq 1\, 000$). The next two lines each contains a string of length $N$ consisting only of 0 and 1 values. This describes the grid of chair placements for the first day, with 1 indicating a chair must be placed here and a 0 indicating a chair should not be placed here.
Finally, two more lines follow in the same manner describing the chair placement for the second day of the conference. You are guaranteed the number of 1s in the first grid equals the number of 1s in the second grid.
Output
Output a single integer describing the minimum total effort required to move chairs from their places in the first grid to places in the second grid.
| Sample Input 1 | Sample Output 1 |
|---|---|
3 101 001 001 110 |
2 |
| Sample Input 2 | Sample Output 2 |
|---|---|
5 11000 10100 00010 00111 |
10 |
