Cycle string
Practice
4 (2 votes)
Dynamic programming
Algorithms
Approved
Easy Medium
Problem
93% Success 1633 Attempts 30 Points 1s Time Limit 512MB Memory 1024 KB Max Code

Given two cyclic sequences A and B each of length n, consisting of 0 and 1. In cyclic sequence \(a_{i+1}\) comes after \(a_i\) and \(a_0\) comes after \(a_{n-1}\). In a single turn you can flip three consecutive elements: \(a_i\), \(a_{(i+1)\bmod n}\) and \(a_{(i+2) \bmod n}\). Flipping element is altering its value: if it is 0, it becomes 1, and vice versa. What is the minimum number of turns you need to make to obtain sequence B from A?

Input format

First line of input contains single integer n (\(3 \leq n \leq 2 \cdot 10^5\)) — the length of A and B.

The second line of input contains n integers \(a_i\) (\(0 \leq a_i \leq 1\)) — sequence A.

The third line of input contains n integers \(b_i\) (\(0 \leq b_i \leq 1\)) — sequence B.

Output format

Print single integer — number of operations if it is possible to obtain B from A. Otherwise print "Impossible" (without quotes).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
12 votes
Tags:
Dynamic ProgrammingCombinatoricsEasy-MediumReadyMathematicsOpenApproved
Points:30
Tags:
Easy-Medium
Points:30
Tags:
Easy-Medium