atcoder#ARC112F. [ARC112F] Die Siedler
[ARC112F] Die Siedler
Score : points
Problem Statement
Snuke is playing a game using kinds of cards called Card through Card . There are kinds of card packs available in this game; the -th pack contains copies of Card . Every pack contains at least one card.
Initially, Snuke has copies of Card (it is guaranteed that he has at least one card in total).
He can do the following operations any number of times in any order:
- Get a card pack: Choose , , or and get all cards contained in the -th pack.
- Exchange cards: Choose , , or , discard copies of Card , and get one copy of Card . (He must have at least copies of Card .)
Snuke wants to have as few cards as possible in total. Find the minimum number of cards that Snuke can end up with.
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of cards that Snuke can end up with.
3 1
0 3 5
0 1 0
1
Let denote the fact that Snuke has copies of Card , copies of Card , and copies of Card . The following sequence of operations leaves Snuke with one card:
- get the -st card pack, which results in ;
- exchange cards with , which results in ;
- exchange cards with , which results in .
It is impossible to end up with zero cards, so this is the minimum possible number.
5 2
0 0 1 7 0
0 3 2 0 0
1 0 4 0 0
2
By getting two packs of the second kind and one pack of the first kind, and then exchanging cards some number of times, Snuke can end up with one copy of Card and one copy of Card .
12 10
0 2 4 4 1 5 6 8 0 9 18 19
1 2 4 3 4 0 6 10 9 18 5 7
0 3 0 1 1 4 11 13 9 19 9 10
1 2 4 1 5 8 1 6 15 0 11 1
0 2 0 6 9 3 13 14 16 9 14 14
1 3 2 5 6 1 9 7 1 7 6 22
0 0 4 5 2 3 8 3 13 14 17 4
0 3 3 4 0 7 0 9 14 2 17 14
0 2 4 1 3 3 3 14 17 6 15 13
0 0 1 0 1 0 4 5 9 4 17 17
1 2 1 3 5 7 0 13 7 6 1 0
9