atcoder#ARC125A. [ARC125A] Dial Up
[ARC125A] Dial Up
Score : points
Problem Statement
Snuke has a sequence of integers consisting of s and s, and an empty integer sequence . The initial state is given to you as input.
Snuke can do the following three operations any number of times in any order.
- Shift to the right. In other words, replace with .
- Shift to the left. In other words, replace with .
- Append the current value of at the end of .
You are also given a sequence of integers . Determine whether it is possible to make equal to . If it is possible, find the minimum number of operations needed to do so.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If it is impossible to make equal to , print -1
.
If it is possible, print the minimum number of operations needed to do so.
3 4
0 0 1
0 1 1 0
6
The following sequence of six operations will do the job.
- Append the current value of at the end of , making .
- Shift to the right, making .
- Append the current value of at the end of , making .
- Append the current value of at the end of , making .
- Shift to the right, making .
- Append the current value of at the end of , making .
1 1
0
1
-1