atcoder#ABC269G. [ABC269G] Reversible Cards 2
[ABC269G] Reversible Cards 2
Score : points
Problem Statement
We have cards numbered to . Card has an integer written on the front and an integer written on the back. Here, . For each , solve the following problem.
The cards are arranged so that their front sides are visible. You may choose between and cards (inclusive) and flip them. To make the sum of the visible numbers equal to , at least how many cards must be flipped? Print this number of cards. If there is no way to flip cards to make the sum of the visible numbers equal to , print instead.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer for .
3 6
0 2
1 0
0 3
1
0
2
1
1
3
2
For , for instance, flipping just card makes the sum of the visible numbers . This choice is optimal. For , flipping all cards makes the sum of the visible numbers . This choice is optimal.
2 3
1 1
0 1
-1
0
1
-1
5 12
0 1
0 3
1 0
0 5
0 2
1
0
1
1
1
2
1
2
2
2
3
3
4