We are given n coins, values ci (1 ≤ ci ≤ k? Actually ci ≤ 500, but k ≤ 500 also). It is guaranteed that there exists a subset of coins with sum exactly k. Pari will select a subset of her coins that sums to k (any subset that works). Then she gives those coins to Arya. After that, Arya can consider the coins she received (the subset of size possibly up to n, but only those selected by Pari). Then we need to determine all values x such that there exists some way for Pari to pay with a subset summing to k (i.e., a particular subset S ⊆ [n] with sum(S) = k) such that Arya can make x using some subset of that selected set S. In other words, x is attainable as a subset sum of some subset S (the payment) that sums to k.
We need to output all x that are achievable in that sense, i.e., there exists S ⊆ coins, sum(S)=k, and x is a subset sum of S.
We need to list them sorted ascending, including 0? The examples include 0. Yes, because Arya can take empty subset of coins, sum 0. So 0 is always possible? Not necessarily if there is no payment? But we can always choose some payment set S, and then empty subset of S yields 0, so 0 is always achievable. The example includes 0, so we should include it.
We need to compute all x such that: ∃ subset S ⊆ [n] with sum(S)=k, and ∃ subset T ⊆ S with sum(T)=x.
Equivalently, x is attainable if there exists a subset of coins that sums to k, and that subset can also form x as a sub-subset sum. Another way: x is a sum of some subset of coins that is also a sub-sum of some subset that sums to k. This essentially means: there exists a subset A of coins such that sum(A)=k, and there exists B ⊆ A with sum(B)=x.
We need to find all such x.
Given constraints small (n,k ≤ 500, ci ≤ 500), we can likely do DP over sums.
Let’s think: The set of all possible x is the union over all subsets S with sum k of the set of subset sums of S. We need to compute which x can arise from at least one such S.
Alternatively, we can think of the condition: There exists a subset A of coins such that sum(A)=k, and there exists a subset B ⊆ A such that sum(B)=x. Equivalent to: There exists a pair (B, C) disjoint? Actually B ⊆ A, so let C = A \ B, then A = B ∪ C, with B and C disjoint, and sum(B)=x, sum(C)=k-x. So the condition becomes: There exist two disjoint subsets B and C (B ∩ C = ∅) such that sum(B)=x, sum(C)=k-x, and both B and C are subsets of the given set of coins (the original coins). But careful: The original coins can be used only once in total because A is a subset of all coins; B and C are disjoint subsets of the original coin multiset? Actually the coins are distinct? Each coin is an item. So we have a set of coins (distinct items). A is a subset of these items. B ⊆ A, C = A \ B. So B and C are disjoint subsets of the original coin set. So condition: There exist two disjoint subsets B, C such that sum(B)=x, sum(C)=k-x, and B∪C ⊆ the set of coins. That is, we are partitioning a subset of coins into two parts with sums x and k-x. But it's not necessary that B∪C uses all coins; it's just some subset. So the condition is: There exist two disjoint subsets of coins with respective sums x and k-x. Since B and C are disjoint, they use distinct coins.
Thus x is achievable iff there exist disjoint subsets B and C of the given coins such that sum(B)=x and sum(C)=k-x.
Note that 0 is allowed (B empty, C sums to k; that's always possible because we guarantee a subset summing to k, so x=0 works). Also, x=k works if we can have C empty and B sums to k (again there is a subset summing to k, so x=k works). So 0 and k are always present.
Now, the problem reduces to: Given a multiset of positive integers (coins), find all x such that there exist two disjoint subsets with sums x and k-x. This is essentially checking if the multiset can be partitioned into two subsets with those sums, but not necessarily using all coins; we only need a selection of some coins. That is like: we need to find if we can select two disjoint subsets that sum to x and k-x. This is similar to the "subset sum with two disjoint subsets summing to given values". However, we can also think of it as: we need to find if there is a subset of coins that sums to x, and another subset disjoint from it that sums to k-x. That is, we need to see if we can split some selection of coins into two groups with given sums.
But note that x and k-x are not independent; they sum to k. So we are essentially asking: can we find a subset of coins that sums to k and can be partitioned into two parts of sizes x and k-x? But any subset that sums to k can be partitioned arbitrarily: we can choose any subset of it as B and the rest as C. However, the existence of a subset S that sums to k does not guarantee that for a given x, there exists such S that contains a subset of sum x. Because we need the specific S to have a subset summing to x. So equivalently, we need to see if there exists S sum k such that x is a subset sum of S. That's the original. And we derived that as: existence of disjoint B and C with sums x and k-x. But careful: For a given S, the subset B is a subset of S. But we are free to choose S as any subset sum k. So if we can find disjoint B and C with sums x and k-x, then letting S = B ∪ C, sum(S)=k, and B is a subset of S giving sum x. So condition is exactly that. Conversely, if there exists S sum k with a subset B sum x, then let C = S \ B, disjoint with sums x and k-x. So condition is equivalent. So indeed x is achievable iff we can find two disjoint subsets B, C (both subsets of the original coin set) such that sum(B)=x and sum(C)=k-x.
Thus problem: Given n positive ints up to 500, find all x from 0 to k (maybe beyond? Actually x cannot exceed k because B ⊆ S and sum(S)=k, so sum(B) ≤ sum(S) = k, so x ∈ [0, k]) such that there exist disjoint B, C with sums x, k-x. So we only need to consider x between 0 and k inclusive.
Now, how to compute? Since n,k,ci ≤ 500, we could consider DP over sum. Standard subset sum DP: we can compute which sums are achievable using any subset of coins. That's easy: dp[sum] = true if we can achieve sum with some subset. But we also need disjointness condition. If we just have dp[sum] true for sum=x and also dp[k-x] true, that doesn't guarantee that the subsets used are disjoint. They could overlap in coins. But we need B and C disjoint.
Thus we need to see if we can select two disjoint subsets with given sums. This is similar to the "two-sum" or "knapsack with two subsets" problem. Since n and sums are small (up to 500), we can perhaps do DP that tracks which sums are achievable with a particular set of coins? Or we can treat it as: For each possible subset sum, we need to know if we can also achieve the complementary sum with the remaining coins after using that subset. In other words, we need to determine for each x, whether there exists a subset B with sum x such that the remaining coins (the complement of B) contain a subset C with sum k-x. But careful: The remaining coins after removing B are not necessarily all used; we only need a subset of the remaining coins that sum to k-x. So we can think of a DP that for each total sum of selected coins, we keep track of which sums of "used" coins we can achieve, and maybe also keep track of the complement? Alternatively, we can think of it as: We want to know if we can partition some subset of coins into two groups with sums x and k-x. This is similar to checking if there is a subset S that sums to k and such that x is a subset sum of S. That is exactly the original.
One approach: Consider all subsets S that sum to k. The total number of such subsets could be large, but n and k up to 500, maybe we can DP to compute the set of achievable subset sums over all subsets S with total sum k. However, we need union over many S. That union could be computed by DP that for each sum s (≤ k) we know which subset sums are achievable within a subset that sums to s? Actually we can think dynamic programming that for each coin we decide whether to include or exclude, and keep track of current total sum and also which subset sums are achievable? That seems too heavy.
Alternative: Because of small constraints, we could use boolean DP for "two disjoint subsets" directly. Consider DP over coins, state representing the pair of sums achieved by two disjoint subsets? For each coin, we can assign it to either: not used, used in B, or used in C. We want to know if we can achieve (x, k-x). Since sums up to 500, we could have DP[sumB][sumC] = true if possible with some processed coins. Then for each coin value c, we can update: from DP[a][b] we can set DP[a+c][b] and DP[a][b+c] as true (if we assign coin to B or C) and also keep DP[a][b] (not used). Starting DP[0][0]=true. After processing all coins, we can check for which x we have DP[x][k-x] true (since sumB = x, sumC = k-x). That would directly answer.
But careful: This DP would also allow cases where B and C are not necessarily disjoint? Actually by construction we assign each coin to at most one of B or C, so they are disjoint. Good. And we don't require that all coins are used; we can leave some unused, that's fine. So DP[a][b] true means there exists a selection of coins (some subset) where we designate a subset as B (sum a) and a disjoint subset as C (sum b), and the rest unused. So we can achieve (x, k-x) if DP[x][k-x] true.
Thus the answer set is { x | 0 ≤ x ≤ k, DP[x][k-x] true }.
The DP size: sumB up to k, sumC up to k, but we only need sums up to k for both, because x ≤ k and k-x ≤ k. So DP dimensions (k+1) x (k+1) = up to 501x501 ≈ 251,001, which is fine. However, DP transitions: for each coin (n up to 500), we need to update the boolean table. A naive update would be for each coin, iterate over all (a,b) from high to low to avoid double counting, and set new states. That's O(n * k^2) = 500 * 250k = 125 million, which is borderline but probably okay in C++ (since 125 million boolean operations might be fine with bitset optimization), but Python might be slower. However we can optimize using bitset operations. For each coin c, we can update DP as: DP_new[a][b] = DP_old[a][b] OR DP_old[a-c][b] OR DP_old[a][b-c]. But careful about out-of-bounds. This is similar to 2D convolution? Could be done with bitsets: for each fixed b, we can treat the vector over a as a bitset; adding c to B means shift the bitset for b by c; adding to C means shift the whole DP? Alternatively, we could compress DP as a set of pairs? But we can also do DP as a 2D boolean array with incremental update from high to low to avoid needing copy. Since we can update in-place if we iterate a from k down to 0, b from k down to 0? But careful: When updating, we need the old values. The typical knapsack update in 1D is: for sum from max down to c: dp[sum] |= dp[sum-c]. For 2D, we can treat it as two independent assignments. But we need to ensure that when we add a coin to B, we use DP[a-c][b] from the same iteration? If we iterate a from 0..k? If we iterate a ascending, we might use updated values and count the coin multiple times. So we need to iterate descending in both dimensions, but we have two dimensions: we need to avoid using a state that was just updated in the same coin. The standard approach for multiple knapsack with each item used at most once in either group is to treat DP as 2D and do nested loops in descending order for a and b? However, careful: When assigning coin to B, we need to look at DP[a-c][b] from the previous state (before processing this coin). If we iterate a from 0 to k, we might have already updated DP[a-c][b] in this coin (if we went ascending a), causing the coin to be used multiple times. So we need to iterate a from k down to 0, b from k down to 0 for updates? But then when updating for B, we need DP[a-c][b] which could be at lower a, which may not have been updated yet in this pass if we go descending a (starting high). Actually if we go descending a, then when we are at a, the states with smaller a (a-c) are still from previous iteration (since we haven't updated them yet if we go descending? Actually descending ensures that when we update DP[a][b], the DP[a-c][b] hasn't been updated in this pass because a-c < a, and we haven't visited those indices yet (since descending from high to low, we process larger a first, then smaller a later). But we need to ensure that DP[a-c][b] is from before adding this coin, so that's fine if we haven't updated it yet because we go from high a to low a, we haven't processed lower a values. However, we also need to consider update for adding to C: DP[a][b-c] requires b-c < b. If we also iterate b descending, then when we process DP[a][b], DP[a][b-c] hasn't been updated yet (since b-c < b, and we are descending). So it works. So we can do for each coin: for a from k down to 0: for b from k down to 0: if DP[a][b] is already true? Actually we need to set DP[a+c][b] etc. But if we iterate descending both, we can update DP[a][b] based on DP[a-c][b] or DP[a][b-c] by checking those indices? Let's formulate: Initially DP[a][b] (old). After processing coin, new DP[a][b] = old DP[a][b] OR (if a>=c then old DP[a-c][b]) OR (if b>=c then old DP[a][b-c]). So if we iterate descending a,b, we can compute new DP in place using the old values that are still stored. However, we need to be careful: if we modify DP[a][b] in place, then when we later compute DP[a'][b'] that references DP[a'][b'] itself? We must ensure that we don't overwrite the old values needed for other updates. If we go descending both, then when we compute DP[a][b], the cells DP[a-c][b] and DP[a][b-c] have not been updated yet because they have smaller indices. So we can use the old values there. But we also need to consider the OR with the old DP[a][b] itself. But if we are updating DP[a][b] in place, we will lose the old value if we set it to something else, but we just need to set it to old DP[a][b] OR something. Since we know the old DP[a][b] value before we modify it (we can read it), we can compute new value and assign. But we must not overwrite it before reading it for other cells? However, DP[a][b] is only used as a source for other cells if we consider DP[a+c][b] or DP[a][b+c]. Those involve larger a or b. Since we are iterating descending, when we are at (a,b), the cells with larger indices (a+c,b) or (a,b+c) have already been processed (since we go descending). Actually careful: For the update using the current coin, we consider adding the coin to B or C, which leads to new sums (a+c, b) or (a,b+c). If we iterate descending a,b, then when we process (a,b), the states with larger a or b (i.e., (a+c,b) etc.) have already been processed (since they are larger a), so we would be modifying them after they've been processed? That would be a problem because they'd then not get the effect of this coin? Let's examine typical 1D knapsack: for sum from max down to c: dp[sum] |= dp[sum-c]. That iterates descending so that when we update dp[sum] (larger index), the dp[sum-c] (smaller index) hasn't been updated in this iteration, and also dp[sum] itself hasn't been used to update others because we are going descending, larger indices are processed first. However, we are not using dp[sum] to update others; we only use dp[sum-c] to update dp[sum]. So in 1D, we go from high to low and use the old dp[sum-c] to set dp[sum]. That's fine.
Now in 2D, if we want to update DP[a][b] to become old DP[a][b] OR old DP[a-c][b] OR old DP[a][b-c]. This update uses smaller a and b. If we iterate descending a and b, we can compute new DP[a][b] using the old values from smaller indices that haven't been overwritten. However, we also need to consider that we might want to update DP[a+c][b] from DP[a][b] (adding coin to B). That's a different pattern. So we need to decide which representation is easier.
Option 1: Use DP as "possible pairs after processing some coins". Each coin, we update by adding new possibilities. The recurrence: new_possible = old_possible ∪ {(a+c,b) | (a,b)∈old} ∪ {(a,b+c) | (a,b)∈old}. If we implement using a 2D boolean array, we can compute new array from old. For each (a,b) with old[a][b] true, set new[a+c][b] = true and new[a][b+c] = true, and also keep new[a][b] = true. Then replace old with new. That's O(k^2 * n) if we iterate over all (a,b) each time and set new states. Since k ≤ 500, k^2=250k, n=500 => 125 million operations, plus we need to reset new array each time? That's okay maybe in C++ but Python might be slow. But we can try to optimize using bitset operations: For each fixed b, we can treat the row as bitset over a. When adding coin to B, we shift the row bitset by c. When adding to C, we need to move to a different row: For each b, we set new[a][b] = old[a][b] OR old[a][b-c] (if b>=c) for C addition? Actually if we have old[a][b-c] true, then by putting coin into C we get (a,b). That's like shifting the column? Alternatively, we can treat DP as a 2D array but maybe we can use a boolean DP with dimensions (k+1)(k+1) and do in-place updates using descending order but for the recurrence that uses smaller indices: new DP[a][b] = old DP[a][b] OR old DP[a-c][b] OR old DP[a][b-c]. That's analogous to a 2D knapsack where we are allowed at most one copy per item and we can assign it to either B or C. But does that recurrence capture exactly the same? Let's test. At step after processing some items, we want DP[a][b] = true if there exists a selection of processed items assigning to B and C (disjoint) achieving sums (a,b). When we process a new coin of value c, we can either not use it, or add it to B, or add it to C. So new DP'[a][b] = DP[a][b] (not used) OR (if a≥c) DP[a-c][b] (used in B) OR (if b≥c) DP[a][b-c] (used in C). Indeed, that recurrence is correct if we process items sequentially and update DP in-place using a method that doesn't reuse the coin multiple times. However, if we do in-place update by iterating a from 0..k and b from 0..k (ascending), we risk using the coin multiple times because when we compute DP[a][b] = DP[a][b] OR (a>=c && DP[a-c][b]) OR (b>=c && DP[a][b-c]), if DP[a-c][b] or DP[a][b-c] have already been updated in this iteration (i.e., include the current coin), then we would be using the coin twice (once in B and once in C or multiple times). So we need an order such that when we compute DP[a][b], the values DP[a-c][b] and DP[a][b-c] reflect the state before considering the current coin. That's possible if we iterate a from 0..k and b from 0..k but use a temporary copy (i.e., compute new DP based on old DP and then replace). Or we can iterate descending a and descending b. Does descending a,b guarantee that DP[a-c][b] and DP[a][b-c] are from old? Let's examine: Suppose we iterate a from k down to 0, for each a we iterate b from k down to 0. Consider DP[a][b]. At the moment we compute it, we need old DP[a][b] (which we haven't overwritten yet? Actually DP[a][b] currently holds old value if we haven't updated it yet. Since we are descending, we will have already updated cells with larger a or larger b? Possibly yes, because we started with high a,b. But for DP[a-c][b] with a-c < a, we haven't updated it yet if we process a descending, because a-c is smaller. So DP[a-c][b] still holds old value. For DP[a][b-c] with b-c < b, we haven't updated it yet because we are iterating b descending. So DP[a][b-c] is old. However, what about DP[a][b] itself? It is still old because we haven't updated it yet. So we can compute new value = old DP[a][b] OR (a>=c? old DP[a-c][b] : false) OR (b>=c? old DP[a][b-c] : false). Then assign to DP[a][b]. That seems correct because we are updating in place but using only old values. But is there any risk that DP[a][b] later might be used as a source for another cell that hasn't been processed? Possibly for larger a' or b'? Since we are descending, cells with larger a or larger b have already been processed. But after we update DP[a][b], it might be used as DP[a' - c'][b'] for some later item? Not in the same iteration; in this iteration, we are only referencing cells with smaller indices. So it won't be used again because later we only look at smaller a,b. So no problem. So we can do in-place update with double descending loops. That would be O(n * k^2) but with very simple inner operations (just boolean checks and assignments). 500 * 250k = 125M boolean operations, each operation may be cheap. In Python, that might be borderline but could pass with optimization if we use list of bytearrays or use Python's built-in list of bools? But 125M Python-level loops is likely too slow (500 * 501 * 501 = ~125 million iterations, each iteration involves Python overhead). That might be ~1-2 seconds? Actually Python loops are slower; 125 million simple operations could take several seconds (maybe 3-5 seconds) which might be okay if time limit is generous? Usually Codeforces problems with n,k ≤ 500 have solutions O(nk) using bitset or O(n*k^2) with bitset but not plain loops. So we need more optimized.
We can use bitset technique. Because DP[a][b] can be represented as a matrix of bits. But we can also think of a DP that tracks possible pairs of sums differently. Another viewpoint: The condition of existence of disjoint B, C with sums x and k-x is equivalent to there exists a subset of coins that sums to k, and also there is a subset of that subset summing to x. Could we compute the set of subset sums of all subsets that sum to k? That is, we want to know for each x, is there a subset S that sums to k and contains a subset summing to x. This can be rephrased as: x is achievable if there exists a subset T such that sum(T) = x and also there exists a subset U disjoint from T such that sum(U) = k-x. That's the same. So we could compute for each possible sum x, the DP for the rest? Another approach: Use DP that tracks the achievable sums with a certain property. For example, we could compute DP1[i] = possible sums using any subset; DP2[i] = possible sums that can be "complemented" to k? Not exactly.
Let's try to think of a more efficient DP.
Because k ≤ 500, we might use DP with bitsets over one dimension and iterate over the other.
Alternatively, we can compute all possible pairs (a,b) using a DP that is a bitset of size (k+1) for each b? That is, we can maintain an array of bitsets rows[b] where rows[b] is a bitset of a values such that (a,b) is achievable. Initially rows[0][0] = true. For each coin value c, we need to update: new rows[b] = rows[b] OR (rows[b] shifted left by c) (represent adding coin to B while C remains b) OR (rows[b-c] if b>=c?) Actually the operation for assigning coin to C: from a state (a, b) we can go to (a, b+c). So for a fixed b, we need to consider states where the old b' = b - c, and then the a values unchanged. That suggests: new_rows[b] includes rows[b] (no use), plus rows[b] shifted left by c (adding to B while C fixed), plus rows[b-c] (adding to C). But careful: rows[b-c] is the bitset for old b-c. If we have a state (a, b-c) from previous step, adding coin to C yields (a, b). So indeed new_rows[b] should be ORed with rows_old[b-c] (if b>=c). Also, we also need to incorporate the effect of adding coin to B while C remains unchanged: for each b, rows_old[b] shift left by c gives states (a+c, b). But those go into rows_new[b]? Wait, if we start from (a,b) old, and we add coin to B, we get (a+c, b). That means the new b is still b, but a becomes a+c. So that goes into rows_new[b] as well. So rows_new[b] = rows_old[b] (no use) OR (rows_old[b] << c) (add to B) OR (if b>=c) rows_old[b-c] (add to C). However, note that rows_old[b-c] is the bitset for b-c. This update seems to be combinable. But we need to ensure we don't double count the coin usage. The above recurrence uses rows_old, i.e., the state before processing the coin. So we need to compute rows_new from rows_old. That would require copying or using a temporary array. But we can implement this per coin: have dp_rows as list of bitsets of length (k+1). For each coin, we compute new_rows = dp_rows[:] maybe copy? Actually we need to compute new based on old. We can loop b from 0..k (ascending) but careful: For updating rows_new[b] using rows_old[b-c], if we compute rows_new sequentially with rows_old still intact, it's fine if we use old rows and store results in new. So we could do:
for each coin c:
new_rows = [bitset (size k+1) for b in range(k+1)] initially zeros
for b in range(k+1):
# from not using coin:
new_rows[b] = dp[b] (copy)
# from adding coin to B:
new_rows[b] |= dp[b] << c
# from adding coin to C (if b>=c):
if b >= c:
new_rows[b] |= dp[b-c]
dp = new_rows
But note: When we do dp[b] << c, that may set bits beyond k? We need to mask to k because a can't exceed k (since a <= k). So we should mask after shift. Similarly for dp[b] itself already within range.
But what about adding coin to both? That's not allowed. The above three cases are exclusive? They are ORed, so it's fine. But are we missing the case where we add to B and also to C? That would require two uses of same coin, not allowed. So correct.
Now complexity: For each coin, we loop b from 0..k (501). For each b, we perform bitset operations: copy (dp[b]), shift left, OR, and possibly OR with dp[b-c]. Bitset size is (k+1) bits ~ 501 bits. So each bitset operation is O(k/wordsize) ~ 501/64 ≈ 8 machine words. So per coin per b, we have constant overhead (like a few 64-bit operations). So total operations: n * (k+1) * (k/64) = 5005018 ≈ 2 million 64-bit word ops, which is trivial. In Python, we can use integers as bitsets if we represent each bitset as an integer (Python int of arbitrary length). Each integer bitwise operation is efficient in C, albeit for large ints with many bits it's okay. 501 bits is tiny, so using Python ints as bitsets will be very fast. So we can implement DP rows as list of ints of length k+1, each int representing a bitset of a-values (bits 0..k). For each b, we need to do:
dp_b = dp[b] # int
not use: new = dp_b
add to B: new |= (dp_b << c) & mask
add to C: if b>=c: new |= dp[b-c]
assign new_rows[b] = new
But careful: The "not use" and "add to B" both use dp_b; we can compute as new = dp_b | ((dp_b << c) & mask). Then if b>=c: new |= dp[b-c]. That's correct.
Now initial dp: dp[0] has bit 0 set; all others zero. We'll have dp = [0]*(k+1); dp[0] = 1 (bit 0 set).
After processing all coins, we look for x such that bit x is set in dp[k-x]? Actually we want dp[x][k-x] true. That means for b = k-x, the bitset for a should have bit x set. So we need to check for each x from 0..k, if (dp[k-x] >> x) & 1 is 1. But careful: dp[b] is bitset of a. So dp[b] & (1 << x) indicates that there exists pair (a=x,b). So answer includes x if dp[k-x] has bit x set. Also note: Since we allow unused coins, the DP includes all possible (a,b) that can be formed. But we must ensure that we don't accidentally count pairs where a+b < k? That's fine because we only check a+b = k? Actually we require exactly a+b = k. But the DP can also have states where a+b < k (since we can leave some coins unused). But we only check those where a+b = k, i.e., b = k - a. So we need to check for each x, if dp[k-x] has bit x set. This gives exactly pairs (x, k-x) possible.
But we also need to consider that we could have a+b = k but with some unused coins? That's allowed because S = B ∪ C may not include all coins; unused coins are irrelevant. So yes.
Thus this DP approach should work. However, need to verify that the recurrence is correct.
Let's test on small examples.
Example 1: n=6, k=18, coins: 5,6,1,10,12,2.
We'll simulate the DP with bitset rows.
But before coding, let's ensure the recurrence works as intended.
Define dp[b] as an integer where bit a set if (a,b) reachable after processing some coins.
Initially: dp[0] = 1 (bit 0). Others 0.
Processing coin c:
For each b from 0..k (new dp):
new_dp[b] = dp[b] | ((dp[b] << c) & mask) (cases: not use, use in B)
if b >= c: new_dp[b] |= dp[b-c] (use in C)
We need to be careful about the order: In the above, dp refers to the old dp. So we compute new_dp using only old dp. This is fine.
But consider the case where we use coin in both B and C? Not allowed, but the recurrence could accidentally combine contributions? For instance, from a state (a,b) we could go to (a+c,b) (add to B) and from the same old state we also could go to (a,b+c) (add to C). But these are separate states. However, what about a state that is both in dp[b] and dp[b-c]? That doesn't cause double usage because they originate from different old states. So fine.
Now need to ensure that we aren't missing the possibility of using the coin in B for states where b already includes some coins? That's covered by dp[b] << c (since dp[b] includes states with that b). And using in C for states where b-c exists: from dp[b-c] we add to b. Good.
Now, is there any issue about using the coin in B and also later in C from the same coin? That would require two uses, but our recurrence only uses the old dp for both cases, not updated dp. Since we compute new_dp from old dp, each state in new_dp is derived from a state that didn't include the current coin. So each coin is used at most once.
Thus DP is correct.
Now after processing all coins, we get dp_final[b] bits for a.
Then answer: ans = [x for x in range(0,k+1) if (dp_final[k-x] >> x) & 1]
But is it possible that dp_final[k-x] has bit x set due to some pair where B sum x, C sum k-x, but the B and C are not disjoint? They are by construction disjoint because each coin assigned to exactly one of B, C, or none. So fine.
Now test on example.
Example 1:
n=6,k=18, coins [5,6,1,10,12,2].
Let's simulate quickly (maybe write small code mentally?).
But we can trust the DP. The answer from example is 16 numbers: 0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18. Our computed answer should match.
Example 2: 3 50: 25 25 50. Answer: 0,25,50.
Check: With coins 25,25,50. We need pairs (x,50-x). Possibilities:
- B empty, C = any subset summing to 50: there are subsets: 50 itself, 25+25. So x=0 works.
- B=25, C=25 (disjoint) using two 25s, gives x=25, k-x=25. So x=25 works.
- B=50, C empty: x=50 works.
- Could x=something else like 10? No.
- Could x=25 and also using a 50? But need both sum to 50, if B=25 from one 25, C=25 from the other 25, works. If B=25 from one 25 and C=25 from 50? No, 50 cannot be 25.
- Could B=25 (one coin), C=25 (same coin)? Not allowed.
- Could B=25, C=25, but also leftover 50 unused? That yields sums 25 and 25, total 50, but S = B∪C = both 25s, sum=50, so ok. So 25 works.
- Could we have B=25 and C=50? Sum=75>50 not allowed.
- Could we have B=50 and C=25? Sum=75 no.
Thus answer matches.
Now what about x=0 and x=50 always works because there is subset sum to 50. Indeed.
Now DP should capture that.
Now we need to implement.
Potential pitfalls: Masking to k. Since a can go up to k. Bits beyond k should be truncated. So mask = (1 << (k+1)) - 1? Actually we need bits 0..k inclusive, so (1 << (k+1)) - 1 gives bits 0..k set. But (1 << (k+1)) might be large for k=500, 1 << 501 is huge but Python can handle. So mask = (1 << (k+1)) - 1. However, note that when we shift dp[b] << c, bits might exceed k; we need to mask after shift to keep only bits up to k. But we also need to keep dp[b] itself within mask (which it is, because we always mask when we assign). So we can compute: new = dp[b] | ((dp[b] << c) & mask). For C addition: if b >= c: new |= dp[b-c]. Note that dp[b-c] is already masked because we kept it masked. So new might have bits beyond k? Possibly from dp[b-c]? dp[b-c] is masked so no. So new is safe. However, we might have bits beyond k from the shift; so we mask that.
Now after each coin, we should assign new_dp[b] = new & mask (just in case). But we already masked the shift, and OR with other masked ints, so new is within mask. But it's safe to AND with mask at the end.
Now performance: For each coin, we need to loop b from 0 to k inclusive. That's ~501 iterations. For each, we do bitwise ops on Python ints of ~501 bits (~8 64-bit words). So about 500501 ~ 250k bitwise operations per coin? Actually per coin we do 501 * constant (few ops). So total about 500501 = 250,500 iterations of loop body, which is trivial. So Python will be fine.
Edge Cases:
- k up to 500, n up to 500, ci up to 500 but not necessarily <= k? They can be up to 500, but k could be less than some ci. However, the DP loops up to k, so we need to ensure that when we shift dp[b] << c, if c > k, then (dp[b] << c) will shift bits beyond k, but we mask anyway, so it may be zero effectively because bits beyond k are removed. However, there is also the case of using coin in C: if c > k, then for b from c..k? Since b <= k, if c > k then b>=c never true, so no addition. So coins > k are effectively irrelevant because they cannot be used in a subset that sums to k (since all values positive). But we could still possibly use them if we don't use them? They will just be never used because we can't add them to B or C without exceeding k? Actually B sum or C sum must be ≤ k because we only care about pairs with total ≤ k? But DP also tracks states where sums a,b individually ≤ k? Actually we allow a and b up to k because we only need up to k for the answer. But if c > k, using it in B would give a > k, which we mask away, so it won't be considered. However, could it be used in C? same. So coins > k are irrelevant. But note that the problem guarantees a subset sum to k exists, but coins > k may still exist. They will not affect the DP for sums ≤ k. So we can simply ignore coins > k to speed up? But not necessary.
We must ensure that DP initial size is k+1 for b, but a also limited to k. So we mask to bits 0..k.
Now, after final DP, we compute ans as described. However, we should also verify that (dp_final[k-x] >> x) & 1 yields true for x = 0 and x = k. Since we guarantee existence of subset sum to k, dp_final[k] should have bit 0? Let's check: (a,b) = (0,k) means we have B empty, C sum k. Is (0,k) reachable? That would be from a subset C that sums to k. Our DP should include that. Indeed, if we use the subset that sums to k as C, and B empty. So dp_final[k] should have bit 0 set. So (dp_final[k] >> 0) & 1 = true, so x=0 works. For x=k, we need (a,b) = (k,0). Is (k,0) reachable? That would be B sums to k, C empty. Since there is subset summing to k, that should also be reachable. So dp_final[0] should have bit k set. So that works.
Now check example 1 output: 0,1,2,3,5,6,7,8,10,11,12,13,15,16,17,18. Indeed includes 0 and 18.
Thus algorithm:
-
Read n,k.
-
Read list of coins.
-
Preprocess: maybe filter ci <= k? Not required but could reduce operations? But c_i can be up to 500, k maybe smaller, but we can ignore coins > k because they can't be used in any subset summing to k (since all positive). However, could they be used as part of B or C? B and C sums individually can be up to k. So if ci > k, using it would exceed k, so it's useless. So we can filter to only coins with ci <= k. This may reduce n. But careful: Could there be a scenario where using a coin > k could be used but still a+b=k? If ci > k, then either a or b would be at least ci > k, so a+b would be > k. So not allowed. So indeed we can discard ci > k. But the guarantee that there exists subset summing to k only uses coins ≤ k. So fine.
-
Initialize mask = (1 << (k+1)) - 1.
-
dp = [0] * (k+1)
-
dp[0] = 1 # bit 0 set
-
For each coin c (if c > k: skip):
new_dp = [0] * (k+1)
for b in range(k+1):
# not use + use in B:
val = dp[b] | ((dp[b] << c) & mask)
# use in C:
if b >= c:
val |= dp[b-c]
new_dp[b] = val & mask # mask again for safety
dp = new_dp
-
After loop, compute ans = []
for x in range(0, k+1):
if (dp[k-x] >> x) & 1:
ans.append(x)
-
Output length and sorted list (they are naturally in ascending order if we iterate x from 0..k).
Check if we need to output numbers in ascending order; yes.
Time complexity: O(n * k * (k/word)), but with bitset it's effectively O(nk). Memory: O(k^2/word) ~ O(k) for the bitset rows? Actually dp array has k+1 ints, each of size O(k) bits, so memory O(k^2/word) ~ (501501/8) bytes ~ 31KB, fine.
Now test the approach with examples.
We'll write a quick mental test for example 2:
n=3,k=50, coins [25,25,50]. But 50 <= k, so include all.
Initialize dp[0]=1 (binary ...0001).
Coin 25:
- b from 0..50.
Compute new_dp.
For b=0:
dp[0]=1. val = 1 | ((1<<25) & mask) = 1 | (1<<25). (since mask covers up to 50)
b>=c? b=0>=25 false.
new_dp[0] = (1 | (1<<25)) => bits 0 and 25 set.
For b=25:
dp[25] initially 0. val = 0 | (0<<25)=0. b>=c? 25>=25 true, so val |= dp[25-25]=dp[0]=1. So new_dp[25] = 1 => bit 0 set.
For other b, if b has dp[b] maybe 0, so new_dp[b] becomes 0 or possibly something from C if b>=25? For b=50: dp[50] initially 0, val=0, b>=25? yes, val |= dp[25]? dp[25] old is 0, so still 0.
Thus after first coin, dp:
dp[0] has bits 0,25
dp[25] has bit 0
others 0.
Coin 25 again (second 25):
Now use old dp.
Compute new_dp.
We'll compute some relevant b.
b=0: dp[0] old has bits 0,25.
val = dp[0] | ((dp[0]<<25) & mask). dp[0]<<25 yields bits 25 and 50? Actually dp[0] bits: 0 and 25. Shift left 25: bits 25 and 50. & mask keeps bits up to 50. So that yields bits 25 and 50. OR with dp[0] gives bits 0,25,50. b>=c? 0>=25 false. new_dp[0] = bits 0,25,50.
b=25: old dp[25] has bit 0.
val = 0 (dp[25] is 1? Actually bit0 set so dp[25] = 1) | ((1<<25)&mask) = 1 | (1<<25) gives bits 0,25. Then b>=c? 25>=25 true, val |= dp[0] old? dp[0] old is bits 0,25. So OR gives bits 0,25. So new_dp[25] = bits 0,25.
b=50: old dp[50] = 0.
val = 0 | 0 =0. b>=c? 50>=25 true, val |= dp[25] old (bits 0). So new_dp[50] = 1 (bit0).
Now other b maybe not needed.
So after second coin, dp:
dp[0]: bits 0,25,50
dp[25]: bits 0,25
dp[50]: bit 0
Now coin 50:
Old dp as above.
Compute new_dp.
b=0: old dp[0] bits 0,25,50.
val = dp[0] | ((dp[0]<<50)&mask). dp[0]<<50: bits 50? Actually bits 0,25,50 shifted left 50: bits 50,75,100. Mask up to 50 => only bit 50 remains if 50? Wait 0+50=50, 25+50=75 (beyond 50 masked), 50+50=100 masked. So ((dp[0]<<50) & mask) yields just bit 50 if 0+50? Actually bit 0 shifted becomes bit 50, that's within mask; bit 25 becomes 75 out; bit 50 becomes 100 out. So only bit 50 from shifting. So val = bits 0,25,50 (original) OR bit 50 => bits 0,25,50. b>=c? 0>=50 false. new_dp[0] = bits 0,25,50.
b=25: old dp[25] bits 0,25.
val = bits 0,25 | ((bits 0,25)<<50) -> shift 50 yields bits 50,75; mask gives bit 50. So val = bits 0,25,50. b>=c? 25>=50 false. new_dp[25] = bits 0,25,50.
b=50: old dp[50] bit 0.
val = 0 | ((0)<<50)=0. b>=c? 50>=50 true, val |= dp[0] old (bits 0,25,50). So new_dp[50] = bits 0,25,50.
b= other? maybe b=75? but b only up to 50. So final dp after all coins:
dp[0] bits 0,25,50
dp[25] bits 0,25,50
dp[50] bits 0,25,50
Now compute answer: For each x in 0..50, check (dp[50-x] >> x) & 1.
x=0: dp[50] >>0 &1 = (bits... ) has bit0 set? dp[50] bits include 0,25,50, so bit0=1 => true.
x=25: dp[25] >>25 &1 = dp[25] bit 25? dp[25] bits include 0,25,50, so bit25=1 => true.
x=50: dp[0] >>50 &1 = dp[0] bit 50? dp[0] bits include 0,25,50, so bit50=1 => true.
x= other: e.g., x=1: dp[49]? we haven't computed dp[49]? Actually dp[49] likely 0? Because we didn't set bits for b=49. So dp[49] >>1? false. So ans = {0,25,50}. Good.
Now test example 1 quickly using script mentally? Not needed.
So algorithm seems correct.
Now we need to ensure we output the number of suitable values and then the values in ascending order.
Edge Cases: n=1, k=1, coin=1. Then dp after processing: dp[0] bits: from not using? Actually after coin 1, dp[0] = 1 | ((1<<1)&mask) = bits 0 and 1. dp[1] = (dp[1] initially 0) but b>=c? 1>=1 so OR dp[0] old? old dp[0] was 1 only before processing? Let's compute: start dp[0]=1. Process coin 1: new_dp[0] = dp[0] | (dp[0]<<1) = 1 | 2 = 3 (bits 0,1). new_dp[1] = dp[1] | (dp[1]<<1) = 0, and b>=c so OR dp[0] old = 1 => new_dp[1] = 1 (bit0). So final dp: dp[0]=3, dp[1]=1. Then ans: x=0: dp[1] bit0=1 => true; x=1: dp[0] bit1=1 => true. So ans = [0,1]. Output correct.
Now if there are multiple coins, we need to ensure that DP considers all possibilities of using coins in either group.
Potential pitfalls: The DP recurrence we used (new = old[b] | (old[b] << c) | (old[b-c] if b>=c) is correct only if we treat the "not use" and "use in B" correctly with old[b] and the shift of old[b]. But what about using the coin in C from a state where we also didn't use the coin? That's covered by old[b-c] which represents a state where we had (a, b-c) before adding the coin to C. However, is it possible that we could use the coin in C and also not use it? That's contradictory. The recurrence old[b-c] already accounts for cases where the coin is not used in that state; then we add it to C, so new state (a, b). That's fine. So correct.
But wait: Could we also have a state where we already used the coin in B? That would be from old state (a-c, b) but we are not considering that for C addition because we only use old[b-c]. That's fine.
Now, could there be a scenario where we use the coin in B from a state where we previously used it in C? Not possible because each state in old didn't include the current coin.
So recurrence is sound.
Now, we need to consider that dp[b] is an integer representing bits a. But what about b values that are greater than k? We only keep up to k. So when we shift dp[b] << c, bits may go beyond k; we mask them. So effectively, we only consider states where a ≤ k. That's fine.
Now, after all coins, we have dp. But we need to answer for x such that there exists B and C with sums x and k-x. However, the DP we constructed includes states where a+b can be less than k, because we didn't enforce that all coins are used. But we only consider those where a+b = k. So we query exactly.
Now one more thought: Could there be a scenario where a+b = k is achievable but we never actually got that state because we restricted a and b ≤ k? That's fine.
Now implementation details: Use Python's ints as bitsets. The mask = (1 << (k+1)) - 1. However, for k=500, 1<<501 is a huge integer but manageable.
Initialize dp = [0]*(k+1)
dp[0] = 1
For each c in coins (if c <= k):
new_dp = [0]*(k+1)
for b in range(k+1):
# Get old bits
old_bits = dp[b]
# cases not use and use in B
new_bits = old_bits | ((old_bits << c) & mask)
# use in C
if b >= c:
new_bits |= dp[b-c] # old dp[b-c]
new_dp[b] = new_bits & mask
dp = new_dp
Note: The line new_bits |= dp[b-c] uses the old dp, which is correct because we are reading from the original dp before update. But careful: In the loop, we are reading dp[b-c] while also potentially overwriting dp later. Since we are not modifying dp until after we compute all new_dp, it's safe. So we can just use dp within the loop, referencing the original dp.
Now, we might be able to further optimize by only iterating b up to k and using a copy of dp for old? That's fine.
Now, what about coins equal to 0? Coins are at least 1 per constraints, so fine.
Now output: Print q = len(ans) and then the ans list joined by spaces.
Let's test with sample input in our head or using reasoning.
But I want to double-check the recurrence on a more complex example to ensure no off-by-one or missing states. Consider coins [2,3], k=5. Possible S subsets that sum to 5: {2,3} only. Subset sums of S: 0,2,3,5. So answer should be {0,2,3,5}. Also maybe {?} Could we have S = {5}? No coin of 5. So only those. Also check if we can have B and C disjoint with sums x,5-x. Possibilities: x=0 (C=5 via 2+3), x=2 (B=2, C=3), x=3 (B=3, C=2), x=5 (B=2+3, C=0). So answer = 0,2,3,5. Now test DP.
coins: c=2,3.
Initialize: dp[0]=1.
Coin 2:
- b=0: old=1; new=1 | (1<<2)=1|4=5 (bits 0,2). b>=2? false. new_dp[0]=5.
- b=2: old=0; new=0; b>=2 true, new|=dp[0]=1; new_dp[2]=1 (bit0).
Others: b=1,3,4,5 old=0; may get from C? if b>=2, dp[b-2] is 0 => new=0.
So after first coin: dp[0]=5 (bits 0,2); dp[2]=1 (bit0); others 0.
Coin 3:
Process with old dp.
For b=0: old=5 (bits0,2). new = 5 | ((5<<3)&mask). 5<<3 = bits 3 and 5? 0->3, 2->5, so (5<<3) = 1<<3? Actually 5 binary 101, shift left 3 => 101000 = 40, bits 3 and 5. Mask for k=5 (bits 0..5) => mask = 0b111111 = 63. So (5<<3) & mask = bits 3 and 5. So new = bits 0,2,3,5. b>=3? false. new_dp[0] = bits 0,2,3,5.
b=1: old=0; new=0; b>=3? false. new_dp[1]=0.
b=2: old=1 (bit0). new = 1 | ((1<<3)&mask) = 1 | 8 = bits 0,3. b>=3? b=2<3 false. new_dp[2] = bits 0,3.
b=3: old=0; new=0; b>=3 true, new|=dp[0] old =5 (bits0,2). So new_dp[3] = bits 0,2.
b=4: old=0; new=0; b>=3 true, new|=dp[1] old =0; =>0.
b=5: old=0; new=0; b>=3 true, new|=dp[2] old =1 (bit0). So new_dp[5] = bit0.
Now final dp:
dp[0] = bits 0,2,3,5
dp[2] = bits 0,3
dp[3] = bits 0,2
dp[5] = bit0
others 0.
Now query:
x=0: dp[5] bit0=1 => yes.
x=2: dp[3] bit2? dp[3] bits 0,2 => bit2=1 => yes.
x=3: dp[2] bit3? dp[2] bits 0,3 => bit3=1 => yes.
x=5: dp[0] bit5? dp[0] bits 0,2,3,5 => bit5=1 => yes.
x=1: dp[4] bit1? dp[4] is 0 => no.
x=4: dp[1] bit4? dp[1] is 0 => no.
Matches.
So algorithm works.
Now, we should consider any alternative possibilities that might be missed. For instance, could there be a scenario where we need to consider B and C not necessarily forming a partition of some subset? Already captured.
Thus solution is fine.
Now complexity: O(n * k * (k/word)) but as we implement with Python ints and loops, it's O(nk). With n up to 500 and k up to 500, the inner loop runs 500501 = 250,500 iterations. Each iteration does a few bitwise ops on Python ints of size ~501 bits (which is about 8 bytes per bit? Actually Python ints are big integers, but operations are still fast for small ints). 250k iterations is absolutely fine in Python. For each iteration, we do a few operations: shift left, AND, OR, maybe a conditional. That should be quick (< 0.1 sec). So it's more than enough.
We need to ensure we handle the case where k is 500 and n is 500, and we do about 250k loops, each loop does maybe 5 Python operations, total ~1.25 million operations, which is trivial.
But careful: The loop is inside for each coin: for c in coins: for b in range(k+1): ... So it's n * (k+1) iterations. For n=500, k=500, that's 250,500 iterations, not 250k * something else. Actually 500 * 501 = 250,500. Yes. So that's fine.
Thus we can implement straightforwardly.
But wait: The loop body includes dp[b-c] which may be an int. That's fine.
Now, we can also reduce memory by using two arrays alternating. We'll do new_dp = [0]*(k+1) each time. That's also fine.
Now one more check: The DP we described might consider states where both B and C include the same coin? No, because we never allow that.
Now let's think if there's any nuance about the problem: "Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x". That's exactly as we used. So our answer includes 0 and k always? Not necessarily if there is no subset summing to k? But guarantee says one can make value k using these coins. So yes, 0 and k always present.
Now let's consider a tricky case: Suppose there are multiple copies of same value. The DP handles them as separate items, so each can be used at most once. That's correct.
Now maybe there is a nuance: Could the chocolate be paid with exactly k, and then Arya uses some subset of those coins to make x. But does Arya have to use only the coins she received? Yes, that's the subset. So our interpretation is correct.
Now the answer set is all x from 0 to k that can be represented as sum of a sub-multiset of a set that sums to k. That's exactly what we computed.
Now we should test more cases with overlapping possibilities, etc.
Consider n=2, k=4, coins [2,2]. Subsets summing to 4: {2,2}. Subset sums of that: 0,2,4. So answer {0,2,4}. Our DP: coins 2,2.
After first coin: dp[0]=bits0,2; dp[2]=bit0.
After second coin:
b=0: old bits0,2 -> new = bits0,2 | shift2 => bits0,2,4? Actually 0->2,2->4, so bits0,2,4. b>=2? false. So dp[0]=bits0,2,4.
b=2: old bits0 -> new= bits0 | shift2 => bits0,2. b>=2 true => OR dp[0] old = bits0,2 -> new bits0,2 (OR with that gives same). So dp[2]=bits0,2.
b=4: old=0 -> new=0; b>=2 true => OR dp[2] old = bits0 => dp[4]=bit0.
Now ans: x=0: dp[4] bit0=1; x=2: dp[2] bit2=1; x=4: dp[0] bit4=1. So works.
Consider n=3, k=6, coins [1,2,3]. Subsets summing to 6: {1,2,3} and {?} maybe {1,2,3} only because other combinations: 6 itself? no 6 coin; 2+? 2+? 4? Not; 3+? 3+? 3+? 1+2+3=6. So only one. Subset sums of {1,2,3}: 0,1,2,3,1+2=3,1+3=4,2+3=5,1+2+3=6 => {0,1,2,3,4,5,6}. But wait, there are duplicates (3 appears twice but as sum of 3 and also 1+2). So set is {0,1,2,3,4,5,6}. So answer should be all numbers from 0 to 6. Let's test DP: coins 1,2,3.
Initial dp[0]=1.
c=1:
b=0: old=1 -> new=1| (1<<1)=1|2=3 (bits0,1). b>=1? false -> dp0=3
b=1: old=0 -> new=0; b>=1 true -> OR dp[0] old=1 -> dp1=1
others 0.
So after coin1: dp[0]=3 (bits0,1); dp[1]=1 (bit0).
c=2:
b=0: old=3 (bits0,1) -> new = 3 | ((3<<2)&mask) = 3 | ( (bits2,3) ) = bits0,1,2,3. dp0 = bits0,1,2,3.
b=1: old=1 (bit0) -> new = 1 | ((1<<2)&mask) = 1|4=5 (bits0,2). b>=2? b=1<2 false. dp1 = bits0,2.
b=2: old=0 -> new=0; b>=2 true -> OR dp[0] old =3 -> dp2 = bits0,1? Actually dp[0] old was 3 (bits0,1) -> dp2 = 3 (bits0,1)
b=3: old=0 -> new=0; b>=2 true -> OR dp[1] old =1 -> dp3 = 1 (bit0)
b=4: old=0 -> new=0; b>=2 true -> OR dp[2] old =0 ->0
b=5: old=0 -> new=0; b>=2 true -> OR dp[3] old=0 ->0
b=6: similarly.
So after coin2: dp0=bits0,1,2,3; dp1=bits0,2; dp2=bits0,1; dp3=bit0.
c=3:
b=0: old bits0,1,2,3 -> new = old | ((old<<3)&mask). old<<3: shift bits 0->3,1->4,2->5,3->6. mask up to 6 => bits 3,4,5,6. So new = bits0,1,2,3,4,5,6. dp0 = all bits 0..6.
b=1: old bits0,2 -> new = bits0,2 | ((bits0,2)<<3) = bits0,2 | bits3,5 = bits0,2,3,5. b>=3? b=1<3 false. dp1 = bits0,2,3,5.
b=2: old bits0,1 -> new = bits0,1 | ((bits0,1)<<3) = bits0,1 | bits3,4 = bits0,1,3,4. b>=3? b=2<3 false. dp2 = bits0,1,3,4.
b=3: old bit0 -> new = 1 | ((1<<3)&mask) = 1|8=9 (bits0,3). b>=3 true -> OR dp[0] old? dp[0] old before processing coin3 (i.e., from after coin2) was bits0,1,2,3. So new |= 15? Actually dp[0] old = bits0,1,2,3 (binary ...0001111) = 15. So new = 9 | 15 = 15 (bits0,1,2,3). dp3 = bits0,1,2,3.
b=4: old 0 -> new=0; b>=3 true -> OR dp[1] old = bits0,2 = 5. So dp4 = 5 (bits0,2).
b=5: old 0 -> new=0; b>=3 true -> OR dp[2] old = bits0,1 = 3. So dp5 = 3 (bits0,1).
b=6: old 0 -> new=0; b>=3 true -> OR dp[3] old = 1 (bit0). So dp6 = 1 (bit0).
Thus final dp:
dp0: bits 0-6 all set (127)
dp1: bits0,2,3,5 (bits: 0,2,3,5) -> binary: 1 + 4 + 8 + 32 =45
dp2: bits0,1,3,4 -> 1+2+8+16=27
dp3: bits0,1,2,3 ->15
dp4: bits0,2 ->5
dp5: bits0,1 ->3
dp6: bit0 ->1
Now ans: for each x 0..6, check dp[6-x] bit x.
x=0: dp[6] bit0=1 -> yes
x=1: dp[5] bit1? dp5 has bits0,1 -> bit1=1 -> yes
x=2: dp[4] bit2? dp4 has bits0,2 -> bit2=1 -> yes
x=3: dp[3] bit3? dp3 has bits0,1,2,3 -> bit3=1 -> yes
x=4: dp[2] bit4? dp2 bits0,1,3,4 -> bit4=1 -> yes
x=5: dp[1] bit5? dp1 bits0,2,3,5 -> bit5=1 -> yes
x=6: dp[0] bit6? dp0 bits 0..6 -> bit6=1 -> yes
So all 0..6, matches.
Great.
Thus solution is solid.
Now we need to output the list. Should we include 0? The examples include 0, so yes.
Now, about input: The first line contains n and k. Next line contains n integers. There's guarantee that a subset sum to k exists. So we can rely on that.
Now I'll implement.
Implementation steps:
- Read n, k.
- Read list coins.
- Optionally filter coins with ci <= k. But keep them? If ci > k, they are useless. So we can filter: coins = [c for c in coins if c <= k].
- mask = (1 << (k+1)) - 1.
- dp = [0] * (k+1)
- dp[0] = 1
- For each c in coins:
new_dp = [0] * (k+1)
for b in range(k+1):
old = dp[b]
# not use and use in B
new = old | ((old << c) & mask)
# use in C
if b >= c:
new |= dp[b-c]
new_dp[b] = new & mask # mask again to be safe
dp = new_dp
- ans = [x for x in range(k+1) if (dp[k-x] >> x) & 1]
- print(len(ans))
- print(' '.join(map(str, ans)))
Now test with given examples.
We should also consider if k=0? But constraints say k ≥ 1 (1 ≤ n, k ≤ 500). So k≥1.
Now complexity fine.
Now about memory: dp list of length k+1, each entry an integer. new_dp same.
Potential pitfalls: For large k=500, mask = (1 << (k+1)) - 1 is okay. However, if we have k=500, then 1<<501 is a huge integer but Python can handle. It's just a 501-bit integer, which is small.
Now one more nuance: When we do dp[b-c] inside the loop, note that dp[b-c] is from the old dp, but we are also using dp[b] earlier. Since we have not changed dp yet, it's fine.
Now, we must consider that we are using Python's bitwise shift on integers which is arbitrary precision, but we mask after shift to keep bits up to k.
Now let's test with a case where c is 0? Not possible.
Now, confirm that the DP doesn't accidentally allow using a coin both in B and C via the shift and then the C addition from the same old state? Not possible because shift and C addition are from different old states.
Now, we might need to consider that there could be multiple ways to get the same (a,b). That's fine.
Now, can we optimize further by not iterating b from 0..k for each coin, but only up to current possible max? Not necessary.
Now, check if we might have integer overflow? Python no.
Now, check if we need to use sys.stdin.read for faster input? Could but not necessary.
Now, I'll produce final answer.
But before finalizing, we should confirm that the DP indeed covers all possibilities for B and C. Let's formalize:
We process coins one by one. At step i, dp[b] contains bits a such that there is a selection of the first i coins (some possibly unused, some assigned to B or C) with B sum = a, C sum = b.
Base: i=0, dp[0]=bit0, meaning (0,0).
Induction: For coin i with value c, we consider all previous states. For a previous state (a,b), we can:
- Not use coin: (a,b) remains.
- Use in B: (a+c,b)
- Use in C: (a,b+c)
So the new set is union of these.
Now the recurrence we used: new_dp[b] = old_dp[b] (not use, and also B addition from states where C = b) OR (old_dp[b] << c) (B addition) OR (if b >= c) old_dp[b-c] (C addition from states where after adding become b). To see that it captures exactly the union:
- For states where the new C sum is exactly b, they can arise from:
- old state with C = b and not using the coin or using it in B (C unchanged). That gives old_dp[b] (not use) and old_dp[b] shifted (using in B). So new_dp[b] includes old_dp[b] | (old_dp[b] << c).
- old state with C = b-c and using the coin in C, giving new C = b. That adds old_dp[b-c] (since B unchanged). So we OR old_dp[b-c] if b>=c.
Thus indeed it's the union. So correct.
Now, what about states where the new C sum is b and B sum is a, but from an old state with B sum a-c and C sum b, using the coin in B? That is covered by the shift of old_dp[b] (since old_dp[b] includes states with C=b and B = a-c). So yes.
Thus DP correct.
Now we can implement.
Let's think about the possibility of using bitset for the whole DP as a 2D bitset but we already have row-wise.
Now, potential bug: The shift operation might produce bits beyond k, but we mask. However, note that when we compute new_bits = old_bits | ((old_bits << c) & mask), we might lose some bits if old_bits << c sets bits beyond k that we mask away; that's fine. But also, when we later OR with dp[b-c], we should ensure that dp[b-c] is already masked. It is.
Now, could there be a scenario where we need to consider using a coin in B and simultaneously we want to use a coin in C that is the same coin? No.
Now, we should also consider if we need to include x values beyond k? The problem statement: "what values does Arya can make with them?" Arya uses some subset of the coins she receives, so the sum cannot exceed total sum of received coins, which is k. So x ∈ [0,k]. So fine.
Now, output: print q and then the list.
Now, there is also possibility that the answer may include duplicates? We use set, but we produce distinct by checking each x only once. Since DP boolean, it's set.
Now, test with a case where multiple coins and k large.
Example: n=500, k=500, all ci=1. Then there are many subsets. The answer should be all integers from 0 to 500 because any x ≤ k can be made: choose S that sums to k (500 ones), then any subset of S can sum to any number from 0 to 500. But wait, if we have 500 ones, then S can be any set of 500 ones. Subset sums of S can be any number from 0 to 500. So answer all. Let's see if DP yields that: After processing 500 coins of 1, we should be able to achieve any (a,b) with a+b ≤ 500, because we can partition some selection into B and C arbitrarily. Actually DP state (a,b) means we have selected some number of coins, each assigned to B or C, with total B sum a and C sum b. With 500 ones, we can achieve any (a,b) with a+b ≤ 500, by selecting exactly a+b coins, assign a of them to B and b to C. So after processing all coins, dp[b] should have bits a for all a such that a+b ≤ 500. So for a given x, we need (x, k-x) = (x,500-x). Since x+(500-x)=500, we have a+b=500. So if a+b=500 and a+b ≤ 500, it's possible. So indeed dp[500-x] should have bit x set for all x from 0 to 500. So answer all. Our DP should produce that.
Now, complexity wise, processing 500 coins of 1, each time we do loop b=0..500, and shift 1, OR, etc. It'll work.
Now, one more nuance: The DP might be improved by only considering coins ≤ k, as said. Since if c > k, it's useless. So we filter.
Now, ensure that we use Python's fast I/O: not needed but can.
Now, we should also consider that the DP as described uses O(k^2) bits storage, but each integer holds bits. In Python, each integer is an object, so a list of 501 ints is fine. Creating a new list of 501 ints per coin (500 times) would allocate 500 * 501 ≈ 250k integers, each maybe 28 bytes, so ~7 MB, which is okay. But we could reuse two lists to avoid allocation overhead. But it's fine.
Alternatively, we could update in-place using descending loops with bitsets? But our current approach with new_dp each iteration is simpler and safe.
We might consider using array of Python ints, but we need to copy the bits for each b. We are creating new list each iteration. That's okay.
Now, let's think about potential pitfalls with large values of k causing mask to be huge: mask = (1 << (k+1)) - 1. For k=500, that's 2**501 - 1, which is a big integer but still manageable.
Now, a possible improvement: Instead of masking each shift individually, we could mask at the end, but better to mask shift to avoid huge integers (shifting beyond 501 bits will produce an integer with more bits, but Python can handle anyway). But to keep performance consistent, it's okay.
Now, final code.
But there is another alternative solution: Since the problem essentially asks for all x such that there exists a subset of coins that sums to x and another subset disjoint that sums to k-x, we could compute DP for subset sums and then check for each x if there is a subset sum x and the rest can form k-x. There is known technique: compute DP for subset sums and also compute DP for "multiset" but need disjointness. Could use DP with bitset of possible sums and also track something like "can we achieve sum using a subset and have remaining sum also possible"? There is a standard solution for partition problems: check if you can split a set into two subsets with given sums. But here we are not required to use all coins.
But our approach is fine.
Now, double-check that we haven't missed any x where x is not representable as sum of some subset of a k-sum set. For instance, consider coins [4,6], k=10. Subsets sum to 10: {4,6}. Subset sums: 0,4,6,10. So answer {0,4,6,10}. Our DP will yield that.
Now, consider coins [2,4,4], k=6. Subsets sum to 6: {2,4} (using one 4) and {2,4} maybe both? Actually {2,4} (either 4) works, also {2,4} from the other 4. Also {2,4} only. Also maybe {?} 2+4=6, yes. Also could there be {6}? No. So S can be {2,4}. Subset sums: 0,2,4,6. Also could S be something else? Could we use both 4s and 2? That would sum 10 >6. Could we use the two 4s? 8 >6. So only {2,4}. So answer {0,2,4,6}. But is there a way to get x= something else like 1? no. Now consider DP: coins 2,4,4. After processing all, will we get (2,4) and (4,2) etc. We'll get x=0,2,4,6. Good.
Now, also consider if there is another S: maybe S = {2,4} but we could also use the other 4? Actually the other 4 is separate coin. But the set of coins is {2,4a,4b}. If we pick S = {2,4a}, that's allowed; also S = {2,4b} also allowed. So DP should capture both. Our DP should be able to produce (2,4) and (4,2). So answer includes both 2 and 4. Also x=6? (6,0) from {2,4}? Actually B=6, C=0: we need a subset summing to 6: that's the entire S. So (6,0) should be possible. DP should have (6,0) (i.e., a=6,b=0). That would come from using both coins 2 and 4. So dp[0] should have bit6 set. Let's simulate quickly: after processing all, dp[0] bit6? Possibly yes.
Now, one more nuance: The DP we do does not enforce that we use exactly the coins that sum to k; it allows leftover coins. So (6,0) would be achieved by using only coins summing to 6 and none leftover? But we might also have leftover coins that are not used. That's fine because we only need a subset S that sums to k; leftover coins are not part of S. In DP, (a,b) represents B and C used, but there could be unused coins. So (6,0) can be achieved by using some coins to sum to 6 as B, and C empty, and not using the remaining coins. So it's fine.
Now, we should ensure that the DP can indeed produce (6,0) even if there is an extra coin (like the other 4) that we don't use. That's fine because we can choose not to use it.
Now, test with DP quickly: Coins [2,4,4] (c=2,4,4). Process:
Initialize: dp[0]=1.
c=2:
b=0: old=1 -> new=1|(1<<2)=bits0,2 -> dp0=bits0,2
b=2: old=0 -> new=0; b>=2 true -> OR dp[0] old=1 -> dp2=1
After first coin: dp0=bits0,2; dp2=bit0.
c=4 (first 4):
b=0: old bits0,2 -> new = bits0,2 | ((bits0,2)<<4) = bits0,2 | bits4,6 = bits0,2,4,6. dp0=bits0,2,4,6.
b=1: old 0 ->0; b>=4? false ->0.
b=2: old bit0 -> new = 1 | (1<<4)=bits0,4. b>=4? b=2<4 false -> dp2=bits0,4.
b=3: old0 ->0; b>=4 false ->0.
b=4: old0 ->0; b>=4 true -> OR dp[0] old? dp[0] old before this coin was bits0,2. So dp4 = bits0,2.
b=5: old0 ->0; b>=4 true -> OR dp[1] old =0 ->0.
b=6: old0 ->0; b>=4 true -> OR dp[2] old =1 -> dp6=1 (bit0)
b=7:...
b=8:... but k? Let's assume k=6, we only go up to 6. Actually we are building dp for k=6? But note: In this problem, k is given as the target total. In this scenario we set k=6. So DP dimensions are up to 6. So we only consider b from 0..6. So after c=4 (first), we have dp0=bits0,2,4,6; dp2=bits0,4; dp4=bits0,2; dp6=bit0.
Now c=4 (second 4):
Now old dp as above.
Compute new_dp:
b=0: old bits0,2,4,6 -> new = old | ((old<<4)&mask). old<<4: bits4,6,8,10 -> mask (0..6) gives bits4,6. So new = bits0,2,4,6 (original) OR bits4,6 = same. So dp0 stays bits0,2,4,6.
b=1: old 0 ->0.
b=2: old bits0,4 -> new = bits0,4 | ((bits0,4)<<4) = bits0,4 | bits4,8 -> bits0,4,? bits8 out; so bits0,4. b>=4? b=2<4 false -> dp2 = bits0,4.
b=3: old0 ->0.
b=4: old bits0,2 -> new = bits0,2 | ((bits0,2)<<4) = bits0,2 | bits4,6 = bits0,2,4,6. b>=4 true -> OR dp[0] old? dp[0] old (before this coin) is bits0,2,4,6. So OR gives same. So dp4 = bits0,2,4,6.
b=5: old0 ->0; b>=4 true -> OR dp[1] old=0 ->0.
b=6: old bit0 -> new = 1 | ((1<<4)&mask) = 1 | 16? but mask up to 6, (1<<4)=16, bit4 is 16, but mask allows bits 0..6, so 16 is bit4? Actually 1<<4 = 16, which corresponds to bit 4. So (1<<4) & mask = 16 (bit4). So new = 1 | 16 = 17 (bits0,4). b>=4 true -> OR dp[2] old = bits0,4 (which is 17). So new becomes 17 (bits0,4). So dp6 = bits0,4.
Now final dp:
dp0: bits0,2,4,6
dp2: bits0,4
dp4: bits0,2,4,6
dp6: bits0,4
Now ans for k=6:
x=0: dp[6] bit0=1 -> true
x=2: dp[4] bit2? dp4 has bits0,2,4,6 -> bit2=1 -> true
x=4: dp[2] bit4? dp2 bits0,4 -> bit4=1 -> true
x=6: dp[0] bit6? dp0 bits0,2,4,6 -> bit6=1 -> true
x=1: dp[5]? dp5 is 0 -> false
x=3: dp[3]? 0 -> false
x=5: dp[1]? 0 -> false
So answer {0,2,4,6}. That matches expected. Good.
Now, is there any possibility that the answer includes x that is not reachable? No.
Now, we should also consider that the DP might produce false positives? Let's double-check: The DP yields a pair (a,b) if there is assignment of some coins to B and C. This exactly corresponds to a choice of S = B ∪ C with sum a+b. Then x = a is a subset sum of S. So it's valid. So no false positives.
Now, regarding the guarantee that there is a subset sum to k, we didn't explicitly need it for the algorithm; DP will compute regardless. But the problem statement says it's guaranteed.
Now, what about duplicate values and zero? Already covered.
Now, we also need to be careful about the Python implementation: The list dp is list of ints. The line new = old | ((old << c) & mask) works. However, note that (old << c) might produce an integer that is larger than mask, but we mask it. That's fine.
Now, one more nuance: The DP state includes possibilities where a+b > k? Actually we limited a and b each ≤ k due to masking and also b only up to k. But a can be up to k (bits up to k). However, it's possible that a+b > k? For states that we keep, a and b individually ≤ k, but a+b could be > k. For instance, with coins [100,200] and k=150, but c_i may be > k? Actually if k=150, coins >150 are ignored. So a,b ≤150 individually, but a+b could be up to 300? Actually if a and b both ≤150, a+b can be up to 300. But would that happen? Because we only assign coins that are ≤k, but sum of assigned coins could exceed k. For example, consider k=150, coins [100,100]. We could assign both to B: a=200, but a is capped at k? In our DP we mask a to ≤k. So if a would exceed k, we discard it (mask). So effectively we are not considering states where a > k or b > k. But is it possible that a+b = k while a > k or b > k? Not if a+b = k and a,b≥0, then both ≤k. So it's fine. So we can safely ignore states where a or b > k because they cannot contribute to a+b = k. So the DP is correct.
Now, what about states where a+b < k? They are not used in answer but they may be used to generate future states that sum to k. However, because we cap a and b at k, any state that would have a > k is dropped. But could dropping such states prevent us from reaching a future state with a+b = k? Consider a scenario where we need to have a intermediate state with a > k to later add coins to C to bring b up such that a+b = k? That seems impossible because adding coins only increases either a or b, so if a > k already, a+b will always be > k and cannot later become k (since we cannot decrease). So such states are irrelevant for final a+b = k. Similarly for b > k. So safe.
Thus the DP is correct.
Now, we should implement.
Now, we must also consider the case where k is large (500). The mask (1 << (k+1)) - 1 may be computed as (1 << (k+1)) - 1, but careful with shift: 1 << 501 is a huge integer, but fine.
Alternatively, we could use bit_length operations but not needed.
Now, the algorithm is ready.
Now, is there any even simpler solution? Possibly using classic subset sum DP and then checking if there exists a subset sum x such that the complement sum k-x is also achievable with the remaining coins. That is like: compute all subset sums, and for each coin combination, can we split? But the DP we have is essentially that.
Now, I'll implement final solution.
Testing with sample inputs.
Sample 1:
Input:
6 18
5 6 1 10 12 2
Our code should output:
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Let's manually test with quick mental? Already did some test; should be correct.
Now, just to be extra safe, we could test with some random small cases and verify via brute force (but not needed here).
Now, final answer code.
But wait: The problem might be from Codeforces (maybe problem "500B" or similar). In Codeforces, the time limit is usually strict but our solution should be fine.
One more nuance: The DP we described uses O(nk^2) bit operations if we think in terms of bitset loops, but our implementation using Python ints with shift is effectively O(nk) because each row update is constant time (bitwise ops on 501-bit integers). However, we have an inner loop over b (k+1). That's O(k) per coin, not O(k^2) because each row operation is O(1) due to fixed size bitset (since Python int operations on numbers of limited size are not O(number of bits) but they are implemented in C and scale with number of machine words, but for 501 bits it's constant, about 8 words). So total time O(n*k) ~ 250k constant-time operations, very fast.
Thus fine.
Now, we need to output the values in ascending order, which we will by iterating from 0 to k.
Now, code.
Implementation details:
- Use sys.stdin.read().split() to parse quickly.
- map to ints.
Pseudo:
import sys
def main():
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
k = int(next(it))
coins = [int(next(it)) for _ in range(n)]
# filter coins <= k
coins = [c for c in coins if c <= k]
mask = (1 << (k+1)) - 1
dp = [0] * (k+1)
dp[0] = 1
for c in coins:
new_dp = [0] * (k+1)
for b in range(k+1):
old = dp[b]
# not use or use in B
new = old | ((old << c) & mask)
# use in C
if b >= c:
new |= dp[b-c]
new_dp[b] = new & mask
dp = new_dp
ans = []
for x in range(k+1):
if (dp[k-x] >> x) & 1:
ans.append(x)
print(len(ans))
print(' '.join(map(str, ans)))
if name == 'main':
main()
Now test with sample:
Sample 1:
n=6,k=18, coins = [5,6,1,10,12,2]. We'll simulate quickly? Already tested with simpler cases.
Let's run mental check for sample 1? Could trust.
Now, confirm that dp array indexing: dp[b] bits for a. For x, we check dp[k-x] bit x. That's correct: because we want a=x, b=k-x.
Now, corner case: k=0? Not allowed.
Now, note: The DP uses dp[b-c] which may be out-of-range if b-c negative, but we only access when b>=c. Good.
Now, about filtering coins: if we filter out coins > k, is it possible that the existence of a subset sum to k is affected? Since coins > k cannot be used in a subset summing to k (all positive), the subset sum to k must use only coins ≤ k. So filtering is safe.
Now, consider coin values that are zero? Not allowed.
Now, I think the solution is correct.
Now, we can produce final answer with explanation.
But the user asked to "Write a solution in python". So we will output the code and maybe explanation.
Now, before final, let's think about potential pitfalls: The DP as implemented does not consider the order of processing coins; but it's correct. However, one must be careful that the array dp is updated correctly. In the loop, when we compute new for b, we refer to dp[b-c] which is from the old dp. Since we are reading from the old dp throughout, it's consistent. And we write to new_dp.
Now, potential performance: We are creating a new list new_dp of length k+1 for each coin. That's 500 * 501 ~ 250k integers created. Each integer is small (maybe 28 bytes). So about 7 MB allocated throughout. Acceptable.
Alternatively, we could allocate two lists and swap, but fine.
Now, about mask: For k=500, 1 << 501 is huge but we compute it as (1 << (k+1)) - 1. That is fine.
Now, some may worry about Python's shift and large integers, but it's okay.
Now, final answer.
But wait, the problem statement: "Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x." This is exactly what we solved.
Now, let's consider if there is any subtlety: Could Arya use the coins in any order? She uses a subset of the received coins. That's subset sum.
Now, we have the solution.
Now, I'll prepare final answer with explanation and code.
We might also mention the DP recurrence and bitset optimization.
Thus final solution.
```python
import sys
def main():
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
k = int(next(it))
coins = [int(next(it)) for _ in range(n)]
# Coins larger than k can never be used in a sum of total k.
coins = [c for c in coins if c <= k]
# mask keeps only bits 0..k
mask = (1 << (k + 1)) - 1
# dp[b] is an integer whose a-th bit is 1 iff state (a, b) is reachable.
dp = [0] * (k + 1)
dp[0] = 1 # (0,0)
for c in coins:
new_dp = [0] * (k + 1)
for b in range(k + 1):
old = dp[b]
# not using the coin or putting it into B (C unchanged)
new = old | ((old << c) & mask)
# putting the coin into C (B unchanged)
if b >= c:
new |= dp[b - c]
new_dp[b] = new & mask # keep only bits 0..k
dp = new_dp
# Collect all x with 0 <= x <= k such that (x, k-x) is reachable.
ans = [x for x in range(k + 1) if (dp[k - x] >> x) & 1]
print(len(ans))
print(' '.join(map(str, ans)))
if name == 'main':
main()