This page is still under construction.

Parts of this page are still being built. What you see may change.

Returning Lights To Box

Interview

Time limit1sMemory limit512 MB

Summary
Given initial light states and a schedule of automatic toggles, find the earliest second by which switches can make every light off.
Level

Medium7 of 10

Topics
Binary search, Greedy, Prefix sum, Implementation
Solved
No attempts yet

Problem

Wesley needs to take down his holiday lights. He has a line of NN lights, some of which may be on, and Wesley needs all the lights to be off before he can unplug them (or else he will receive a deadly electrical shock).

Each light has a corresponding switch that can be used to turn the light on or off, and Wesley can use at most one of these switches every second, starting from the first second. However, these lights are finicky, and in the next MM seconds they will toggle their state on their own. Specifically, at the end of the ii-th second, the bib_i-th light flips its state: it turns on if it was off, or turns off if it was on. Wesley wants to take the lights down as soon as possible, so he would like to know the earliest time possible for all the lights to be off, assuming he uses switches in an ideal manner. In particular, output the least ii such that all lights can be turned off by the end of the ii-th second by some sequence of switch usages. If all lights are initially off, then the least such ii is 0.

Input

The first line contains two integers NN and MM, the number of lights and the number of unsolicited changes the lights will make (1≤N,M≤2⋅1051 \le N, M \le 2 \cdot 10^5).

The second line contains NN integers a1,a2,…,aNa_1, a_2, \ldots, a_N (0≤ai≤10 \le a_i \le 1), the initial state of the lights. Here, ai=1a_i = 1 indicates that the ii-th light is initially on, and ai=0a_i = 0 tells it is off.

The third and final line contains MM integers b1,b2,…,bMb_1, b_2, \ldots, b_M, which denotes that the bib_i-th light flips its state at the end of the ii-th second (1≤bi≤N1 \le b_i \le N).

Output

Output a single integer, the earliest time (in seconds) it will take for Wesley to turn all the lights off. If all the lights can be turned off before MM seconds have passed, Wesley will ignore any future toggles and take them down immediately.

Examples2

  1. Example 1

    Input
    3 3
    1 1 1
    1 2 3
    
    Expected output
    2
    
  2. Example 2

    Input
    5 8
    0 1 0 1 1
    1 2 2 1 4 3 2 1
    
    Expected output
    4