Flip Digits

AtCoder
IDagc049_b
Time2000ms
Memory256MB
Difficulty
Given are length-$N$ strings $S$ and $T$ consisting of `0` and `1`. You can do the following operation on $S$ any number of times: * Choose $i$ ($2 \leq i \leq N$) such that $S_i=$`1`, and replace $S_i$ with `0`. Additionally, invert $S_{i-1}$, that is, change it to `1` if it is `0` now and vice versa. Is it possible to make $S$ exactly match $T$? If it is possible, how many operations does it take at least? ## Constraints * $1 \leq N \leq 5 \times 10^5$ * $S$ is a string of length $N$ consisting of `0` and `1`. * $T$ is a string of length $N$ consisting of `0` and `1`. ## Input Input is given from Standard Input in the following format: $N$ $S$ $T$ [samples]
Samples
Input #1
3
001
100
Output #1
2

We can do as follows: `001` → (choose $i=3$) → `010` → (choose $i=2$) → `100`.
Input #2
3
001
110
Output #2
\-1
Input #3
5
10111
01010
Output #3
5
API Response (JSON)
{
  "problem": {
    "name": "Flip Digits",
    "description": {
      "content": "Given are length-$N$ strings $S$ and $T$ consisting of `0` and `1`. You can do the following operation on $S$ any number of times: *   Choose $i$ ($2 \\leq i \\leq N$) such that $S_i=$`1`, and replace ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc049_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given are length-$N$ strings $S$ and $T$ consisting of `0` and `1`. You can do the following operation on $S$ any number of times:\n\n*   Choose $i$ ($2 \\leq i \\leq N$) such that $S_i=$`1`, and replace ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments