<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

415. Necessary Coins
Time limit per test: 1.25 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



Vasya has been on vacation on Mars. He's a big fan of foreign coins, and thus has collected exactly one martian coin of each denomination, for a total of n coins: a1 martian dollars, a2 martian dollars, etc, an martian dollars. Unfortunately, he couldn't stand ordering the Pan Galactic Gargle Blaster at the Starport, and has to pay for it — it costs x martian dollars. Vasya is wondering which of his coins are absolutely necessary to do so (i.e., he is forced to abandon them). They don't offer change at the Starport Mars.

Input
The input file contains two integer numbers n and x (1 ≤ n ≤ 200, 1 ≤ x ≤ 104), followed by n distinct integer numbers ai (1 ≤ ai ≤ x).

Output
On the first line of output, print the amount of denominations of coins that appear in any subset that sums to x martian dollars. On the second line of output, print the denominations themselves, in any order, separated with single spaces. It is guaranteed that there exists at least one way to pay x martian dollars with the given coins.

Example(s)
sample input
sample output
5 18
1 2 3 5 10
2
5 10

<|response|>
1. Abridged problem statement  
Given n distinct coins with values a₁…aₙ and a target sum x, find all coins that must be used in every subset of these coins that sums exactly to x. Output the count of such “necessary” coins and list their values in any order.

2. Key observations  
- A coin c is non‐necessary if there exists at least one valid subset summing to x that does not include c. Otherwise c is necessary.  
- Naively, for each coin i we could run a subset‐sum DP on the other n−1 coins, but that would be O(n²·x) and too large (n up to 200, x up to 10⁴).  
- We can speed this up by precomputing two DP arrays:  
  • dp_pref[i][s] = true if sum s is achievable using coins a₁…aᵢ.  
  • dp_suf[i][s] = true if sum s is achievable using coins aᵢ…aₙ.  
- Then coin i can be skipped if there exists a split j + (x−j) = x so that dp_pref[i−1][j] and dp_suf[i+1][x−j] are both true. If no such j exists, coin i is necessary.  
- Implement these DP arrays efficiently with bitsets so that each transition is a bitwise shift+OR operation in O(x/word_size) time. Total running time is O(n·x/word_size + n·x) which is fast enough.

3. Full solution approach  
a. Read n, x and the array a of coin values.  
b. Let MAXX = x+1.  
c. Initialize two arrays of bitsets of length MAXX:  
   • dp_pref[0] with dp_pref[0][0] = 1, all others 0.  
   • dp_suf[n+1] with dp_suf[n+1][0] = 1, all others 0.  
d. Build the prefix DP for i = 1…n:  
     dp_pref[i] = dp_pref[i−1] | (dp_pref[i−1] << a[i])  
   This sets dp_pref[i][s] = true if we can make sum s using coins up to i.  
e. Build the suffix DP for i = n…1:  
     dp_suf[i] = dp_suf[i+1] | (dp_suf[i+1] << a[i])  
   This sets dp_suf[i][s] = true if we can make sum s using coins from i to n.  
f. For each coin i = 1…n, test if it is skippable:  
     for j = 0…x, if dp_pref[i−1][j] AND dp_suf[i+1][x−j] is true, then coin i is non‐necessary.  
   If no such j exists, coin i is necessary—add a[i] to the answer list.  
g. Print the size of the answer list and then the coin values.

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, x;
    cin >> n >> x;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    // We need bitsets of length x+1 to represent sums 0..x
    const int MAXX = 10000 + 5; 
    // Prefix DP: dp_pref[i][s] = can we form sum s using coins a[0..i-1]?
    vector< bitset<MAXX> > dp_pref(n + 2), dp_suf(n + 2);

    // Base cases: sum=0 is always achievable with no coins
    dp_pref[0].set(0);
    dp_suf[n+1].set(0);

    // Build prefix DP
    for (int i = 1; i <= n; i++) {
        // copy reachable sums without using the i-th coin
        dp_pref[i] = dp_pref[i-1];
        // OR in all sums that include a[i-1]
        dp_pref[i] |= (dp_pref[i-1] << a[i-1]);
    }

    // Build suffix DP
    for (int i = n; i >= 1; i--) {
        dp_suf[i] = dp_suf[i+1];
        dp_suf[i] |= (dp_suf[i+1] << a[i-1]);
    }

    vector<int> necessary;
    // Check each coin if it is necessary
    for (int i = 1; i <= n; i++) {
        bool can_skip = false;
        // Try splitting the target x into j + (x-j)
        for (int j = 0; j <= x; j++) {
            if (dp_pref[i-1].test(j) && dp_suf[i+1].test(x - j)) {
                // We can pay x without coin i
                can_skip = true;
                break;
            }
        }
        if (!can_skip) {
            // No way to pay x without a[i-1], so it is necessary
            necessary.push_back(a[i-1]);
        }
    }

    // Output the result
    cout << necessary.size() << "\n";
    for (int v : necessary) {
        cout << v << " ";
    }
    cout << "\n";

    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    n = int(next(it))
    x = int(next(it))
    a = [int(next(it)) for _ in range(n)]

    # We'll represent DP rows as integers, where the k-th bit means sum k is achievable
    full_mask = (1 << (x + 1)) - 1

    # dp_pref[i] = bitmask of sums achievable with coins a[0..i-1]
    dp_pref = [0] * (n + 2)
    # dp_suf[i] = bitmask of sums achievable with coins a[i-1..n-1]
    dp_suf  = [0] * (n + 2)

    # Base cases: sum=0 is always doable
    dp_pref[0] = 1 << 0
    dp_suf[n+1] = 1 << 0

    # Build prefix DP
    for i in range(1, n+1):
        prev = dp_pref[i-1]
        # shift the bits by a[i-1] to include that coin
        shifted = (prev << a[i-1]) & full_mask
        dp_pref[i] = prev | shifted

    # Build suffix DP
    for i in range(n, 0, -1):
        nxt = dp_suf[i+1]
        shifted = (nxt << a[i-1]) & full_mask
        dp_suf[i] = nxt | shifted

    necessary = []
    # Test each coin
    for i in range(1, n+1):
        pref = dp_pref[i-1]
        suf  = dp_suf[i+1]
        can_skip = False
        # try all splits j + (x-j)
        for j in range(x+1):
            if ((pref >> j) & 1) and ((suf >> (x-j)) & 1):
                can_skip = True
                break
        if not can_skip:
            necessary.append(a[i-1])

    # Print answer
    print(len(necessary))
    if necessary:
        print(" ".join(map(str, necessary)))

if __name__ == "__main__":
    main()
```