Valentine Magic using greedy

question - https://hack.codingblocks.com/app/contests/course/c/196/p/825
answer- https://ide.codingblocks.com/s/120900