20196. The Magical Bag

http://www.spoj.com/problems/DIVEQL/