PROBLEM LINK:Author: Sergey Kulik DIFFICULTY:EASY PREREQUISITES:Dynamic programming, bits PROBLEM:There are $N$ meals to buy numbered from $1$ to $N$. The $i$th meal can be bought separately for price $C_i$. Meals can also be bought in sets. There are $M$ sets and each set consists of some meals. The prices of the $i$th set is $P_i$. The goal is to buy all $N$ meals for the lowest possible price. In one test file there are multiple test cases to handle. QUICK EXPLANATION:The main idea is to use dynamic programming and bitmasks to compute $dp[mask]$ as the smallest possible cost of buying elements set in the $mask$. EXPLANATION:In subtasks $1$ and $2$ methods based on computing the exactly value of $dp_1$ defined in the explanation for subtask 3 below might be used. In particular let $dp[mask]$ be the lowest price to buy meals in $mask$. Then one possible idea for lower constrains is to iterate for all possible masks and for each single one, iterate over all possible seats of meals and try to lower the cost of meals covered by the current mask and the set of meals by combining their costs. Subtask 3First of all, notice that buying all meals is always possible, because they all can be bought separately. However, the price can be reduced by buying the meals in sets. Let $dp_1[mask_1]$ be the smallest price for buying meals in $mask_1$ using a single purchase  so using either a single purchase of a separated meal or a single purchase of a set of meals Let $dp_2[mask_2]$ be the smallest price for buying meals in $mask_2$ using any purchases. Clearly, $dp_2[mask_2]$ for $mask_2$ containing $N$ ones is the final answer. At the beginning, we can set values of $dp_1$ and $dp_2$ to some extremely large values in order to simplify the implementation. The first step towards the solution is to compute $dp_1$ values for all possible $2^N$ masks. Later we are going to use these values to compute values in $dp_2$. Let $mask_{separete, i}$ be the mask corresponding the meal $i$. Moreover, let $mask_{set, i}$ be the mask corresponding to the $i$th set of meals. Initially, we set: $dp_1[mask_{separete,i}] = C_i$ for all $i = 1, 2, \ldots, N$. Then, we set: $dp_1[mask_{set, i}] = \min(dp_1[mask_{set, i}], P_i)$ for $i = 1, 2, \ldots, M$. Since buying a set of $B$ meals might be the cheapest way to buy a set of $A$ meals for $A < B$, then we are going to try to lower these costs as follows:
The thing to notice there is that we iterate from the masks containing the most meals to the masks containing less meals in order to try to reduce their prices. Notice that $\texttt{mask ^ (1 << j)}$ is the $mask$ with the $j$th bit set to $0$. After than, we are ready to compute the values of $dp_2$. The idea is very simple, for a given $mask$ we are going to iterate over all submasks of $mask$ and ty to add a single meal to them to expand it to $mask$:
The above method has time complexity $O(2^N \cdot N + 3^N)$, where the first part is the complexity of calculating $dp_1$ values and the second part is the complexity of calculating $dp_2$ values. While the complexity of the first part is quite obvious, the complexity of the second part needs an explanation. For each mask, we iterate over all its submasks. We know that there are $\binom{N}{k}$ masks with $k$ ones and each such mask has $2^{Nk}$ submasks. Thus the number of iterations during computation of $dp_2$ can be measure as $\sum\limits_{k=0}^N \binom{N}{k} \cdot 2^{Nk} = 3^N$. AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 29 Oct '16, 23:01

How the complexity is O(N*2^N) ? answered 29 Oct '16, 23:34

Nice editorial :) I thought of the bitmask dp method, but couldn't figure out anything to reduce the complexity! answered 30 Oct '16, 16:53

can anyone tell me why it is O(2^n*n) for dp1 instead of O(2^n). answered 02 Nov '16, 00:08

@student2 First 2^n or (1st for loop) is used for generating all combination of generating bits in binary form and other one (2nd for loop) is used for manipulating these bits i.e., doing manipulation on individual bits! I hope you get this.. otherwise feel free to ask answered 02 Nov '16, 08:34

Thank you bansal.Can anyone tell me what are the submasks and how they can be computed answered 02 Nov '16, 20:53

could anyone explain in dp1 why we are considering to minimize A meals by just reducing 1 meals from B meals. suppose we have meals 3,4,5 for price 10 and meals 3,4,5,6,7 for price 5 then we have to just take second set price for both set.BTW I solved it by assigning like this but I don't understand in editorial. answered 15 Nov '16, 13:14

How this statement 'The thing to notice there is that we iterate from the masks containing the most meals to the masks containing less meals in order to try to reduce their prices.' holds true? We are iterating in descending order, that does'nt mean that all numbers containing highest number of bits come first. consider 4 and 3. 4 has 1 bit where 3 contains 2 bits answered 30 Nov '16, 01:21

Here is my solution, https://www.codechef.com/viewsolution/12155819 well commented. answered 30 Nov '16, 23:07

$2^{18}*18 + 3^{18}$ = $392139081$ > $2*10^8$, how does it pass within the time limit? Does the present server compute faster than 10^8 operations also?
^^ Same question. I also tried a 3**n approach (which was wrong) but it still gave tle: https://www.codechef.com/viewsolution/11980847
@vsp4, your code is recursive may be that is why it gave TLE, but still why does $3^n$ pass?
$10^8$ Operations per s is just a rough estimate. If you have a cache friendly algorithm with simple operations you can get much higher. On the old servers cache friendliness seemed to be paramount. In some problem I computed the transpose of a 1000x1000 int matrix in the straightforward naive fashion. It took on the order of a second!
How each mask has 2^(Nk) submasks ?