Private Space
Time limit1sMemory limit128 MB
Choose the smallest widest row width X (at most 12) so that all groups fit into triangular rows of widths X down to 1, keeping one empty seat between neighboring groups in a row.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, Greedy, Brute force
- Solved
- No attempts yet
Problem
People go to the cinema in groups (some go alone). Every group only wants to socialize within itself, so each group insists on at least one empty seat between itself and any neighbouring group in the same row — unless the group sits at one of the two ends of the row, where no empty seat is required on that side.
The cinema is triangular. If the widest row has seats, then the rows have seats (one row of each width from down to ). Because of a capacity limit, the widest row may have at most seats.
The visitors are described by a list , where is the number of groups that consist of exactly people. Every group must be seated in a single row (a group is never split across rows) and the seats a group occupies must be consecutive.
Find the smallest possible width of the widest row such that all groups can be seated at the same time while respecting the one-empty-seat rule.
Input
The first line contains a single integer (): the size of the largest possible group.
The second line contains integers; the -th of them (1-indexed) is , the number of groups of exactly people that must be seated.
Output
Print a single value: the smallest width of the widest row that seats everyone. If no width from to can seat all groups, print impossible instead.