Turn Off The Light
Time limit2sMemory limit256 MB
For each start position p, find the fewest steps needed to turn off all lights, then print the sum of p times that minimum, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
There are lights in a row, numbered to from left to right. Some of the lights are on at the start. Chiaki wants to turn off all the lights.
Chiaki starts at the -th light. Each step, she moves one position to the left or to the right, then presses the switch of the light at her new position. Pressing a switch turns a lit light off and an unlit light on.
For each , find the minimum number of steps needed to turn off all the lights.
Input
The first line contains an integer , the number of test cases. Each test case is given as follows.
The first line contains an integer (), the number of lights.
The second line contains a binary string . If , the -th light is on. If , the -th light is off.
The sum of over all test cases does not exceed .
Output
For each test case, print , where is the minimum number of steps needed when Chiaki starts at the -th light.