Butterfly Ballot
Time limit1sMemory limit256 MB
Decide whether the candidates can be ordered so candidate 1 finishes first when half of each ballot position's supporters spill to the next position.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Simulation
- Solved
- No attempts yet
Problem
A butterfly ballot lists the candidate names down one column and puts the check boxes in the other column. When the two columns are offset by half a row, telling which box belongs to which candidate is hard. On the ballot below it is easy to mark the box next to UCLA while meaning to vote for USC. The same thing happens in an election.

You design a ballot that puts your own candidate in first place. All candidates must appear on it, and you choose the order from top to bottom. The boxes go in the other column. The first box sits above the first candidate, the second box sits between the first and the second candidate, and the rest follow the same pattern.
Among the voters who intend to vote for the candidate in position from the top, half vote for that candidate and half mark the box of the candidate in position , one row below. Every voter who intends to vote for the candidate in the last position votes correctly.
Candidate 1 is your candidate. Decide whether an order exists that puts candidate 1 in first place. A tie for first place counts as a win.
Input
The first line contains the number of data sets , where .
data sets follow. The first line of a data set contains the number of candidates on the ballot, where . Candidate 1 is the one you are trying to make win. Each of the next lines contains , the number of voters who intend to vote for candidate , where . Every is even, so it divides by 2 exactly.
Output
For each data set, print Data Set x: on a line of its own, where is the number of the data set. If candidate 1 can be made to finish first, print Possible on the next line, otherwise print Impossible. Print one blank line after each data set.