Returning Lights To Box
InterviewTime limit1sMemory limit512 MB
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 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 seconds they will toggle their state on their own. Specifically, at the end of the -th second, the -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 such that all lights can be turned off by the end of the -th second by some sequence of switch usages. If all lights are initially off, then the least such is 0.
Input
The first line contains two integers and , the number of lights and the number of unsolicited changes the lights will make ().
The second line contains integers (), the initial state of the lights. Here, indicates that the -th light is initially on, and tells it is off.
The third and final line contains integers , which denotes that the -th light flips its state at the end of the -th second ().
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 seconds have passed, Wesley will ignore any future toggles and take them down immediately.