#K47897. Optimal Seating Arrangement

    ID: 28300 Type: Default 1000ms 256MiB

Optimal Seating Arrangement

Optimal Seating Arrangement

Given a row of seats represented as a sequence of binary values, where 0 indicates an empty seat and 1 indicates an occupied seat, your task is to determine the optimal seat for the next guest. The optimal seat is defined as the seat that maximizes the minimum distance to the nearest occupied seat. In case of multiple optimal seats, choose the seat with the smallest index (1-indexed).

Note: If all seats are already occupied, output -1.

Mathematically, for an empty seat at position (i) (1-indexed) with nearest occupied seats at positions (j) (to the left) and (k) (to the right), the effective distance is defined as (d(i)=\min(i-j, k-i)), where if no occupied seat exists on one side, the corresponding distance is considered (+\infty).

inputFormat

The input is given via standard input (stdin) and consists of two lines:

1. The first line contains a single integer (n) (the total number of seats).
2. The second line contains (n) space-separated integers (either 0 or 1), representing the occupancy of each seat (0 for empty, 1 for occupied).

outputFormat

Output a single integer to standard output (stdout), representing the optimal seat number (1-indexed) for the next guest. If no seat is available, output -1.## sample

10
1 0 0 0 0 0 1 0 0 0
4