Here is the problem.
And here is one of the AC solution : http://codeforces.com/contest/999/submission/39534469 which is based on how to distribute M cards among N persons optimally such that joy is maximum. But how will we take care of this case : Suppose there are 3 players whose favourite card is say x and it occurs 5 times.
suppose p1 get 2 card , p2 = 2 , p3 = 1 and they want to have 4 cards in total.
Now how to select the remaining cards for them, since suppose if i give number y to player 1 it can be the favourite for some other player which will decrease his joy and may affect total joy.