Problem setter: [aditya10_]
Dynamic Programming, Bit Masking
We will take a mask of the number of items M. 1 at ith index represent that item of type i is included. Mask at any time represents the type of items which are ordered continuously from the left. We will find the cost of adding every type of item which was not there in the mask ie the bits which were unset.
Cost of adding an item of type X means the number of items to select such that item of type X occurs in succession. The item of type X here is added after the type of items which have already been placed continuously i.e. the bits which were set in the mask.
Dp array stores the cost of each mask.
The above can be represented as:
 Now How to find the cost of adding a type of item X? Let the total number of items be tot which are set in the mask. And the number of items of type x be c. Now the cost will be the number of items which are not of type x in the range [tot+1,tot+x]. For this we will maintain an array which stores the frequency of each type of item. And a pref array pref[N+1][M+1]. pref[idx][x] represent the number of items of type x upto index idx. ![code2] Author’s Solution: [click here] Tester’s Solution: [click here] : https://www.codechef.com/users/aditya10_ : https://discuss.codechef.com/upfiles/Capture_1xkltz8.PNG : https://discuss.codechef.com/upfiles/Capture1_TRPZSCD.PNG : https://ideone.com/25foEK : https://ideone.com/25foEK