Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is c_{i}. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.

Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.

Formally, 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, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.


-----Input-----

The first line contains two integers n and k (1  ≤  n, k  ≤  500) — the number of coins and the price of the chocolate, respectively.

Next line will contain n integers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ 500) — the values of Pari's coins.

It's guaranteed that one can make value k using these coins.


-----Output-----

First line of the output must contain a single integer q— the number of suitable values x. Then print q integers in ascending order — the values that Arya can make for some subset of coins of Pari that pays for the chocolate.


-----Examples-----
Input
6 18
5 6 1 10 12 2

Output
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 

Input
3 50
25 25 50

Output
3
0 25 50

Difficulty: HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
A = map(int, input().split())
DP = [[False] * (k + 1) for _ in range(k + 1)]
DP[0][0] = True
for a in sorted(A):
	for i in range(k, a - 1, -1):
		for j in range(0, k + 1):
			DP[i][j] |= DP[i - a][j]
			if j >= a:
				DP[i][j] |= DP[i - a][j - a]
A = [i for i in range(k + 1) if DP[k][i]]
print(len(A))
print(*A)


(n, k) = map(int, input().split())
cs = map(int, input().split())
table = [[0 for _ in range(k + 1)] for _ in range(2)]
table[0][0] = 1
for (i, c) in enumerate(cs, 1):
	for s in range(k + 1):
		table[i % 2][s] |= table[(i - 1) % 2][s]
		if c <= s:
			table[i % 2][s] |= table[(i - 1) % 2][s - c]
			table[i % 2][s] |= table[(i - 1) % 2][s - c] << c
possible = [str(i) for i in range(k + 1) if table[n % 2][k] >> i & 1]
print(len(possible))
print(*possible)


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
cc = list(map(int, input().split()))
dp = [[0] * (k + 1) for i in range(2)]
dp[0][0] = 1
for (i, c) in enumerate(cc, 1):
	for j in range(k + 1):
		dp[i % 2][j] |= dp[(i - 1) % 2][j]
		if c <= j:
			dp[i % 2][j] |= dp[(i - 1) % 2][j - c]
			dp[i % 2][j] |= dp[(i - 1) % 2][j - c] << c
ans = [i for i in range(k + 1) if dp[n % 2][k] >> i & 1]
print(len(ans))
print(*ans)


(n, m) = list(map(int, input().split()))
s = [0] + list(map(int, input().split()))
dp = [[[False for i in range(m + 1)] for j in range(m + 1)] for k in range(2)]
dp[0][0][0] = True
cur = 0
for i in range(1, n + 1):
	cur += 1
	cur %= 2
	last = (cur + 1) % 2
	for j in range(m + 1):
		for k in range(j + 1):
			if j - s[i] > -1 and k - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k] or dp[last][j - s[i]][k - s[i]]
			elif j - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k]
			elif k - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k - s[i]]
			else:
				dp[cur][j][k] = dp[last][j][k]
ans = []
for i in range(k + 1):
	if dp[cur][k][i]:
		ans.append(i)
print(len(ans))
print(*ans)


cin = lambda : list(map(int, input().split()))
(n, k) = cin()
c = list(cin())
dp = [0] * (k + 1)
dp[0] = 1
for i in c:
	tmp = dp[:]
	for x in range(k, i - 1, -1):
		tmp[x] |= dp[x - i] | dp[x - i] << i
	dp = tmp
b = bin(dp[-1])
ans = [i for i in range(k + 1) if b[-i - 1] == '1']
print(len(ans))
print(*ans)


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, k) = map(int, input().split())
b = list(map(int, input().split()))
dp = [[False for j in range(k + 1)] for i in range(k + 1)]
dp[0][0] = True
for i in range(n):
	for j in range(k, -1, -1):
		for rem in range(j, -1, -1):
			if j - b[i] >= 0 and rem - b[i] >= 0:
				if dp[j - b[i]][rem]:
					dp[j][rem] = True
				if dp[j - b[i]][rem - b[i]]:
					dp[j][rem] = True
			elif j - b[i] >= 0:
				if dp[j - b[i]][rem]:
					dp[j][rem] = True
poss = []
for rem in range(0, k + 1):
	if dp[k][rem]:
		poss.append(rem)
print(len(poss))
print(*poss)


from collections import Counter
import sys
(n, k) = list(map(int, sys.stdin.readline().split()))
c = list(map(int, sys.stdin.readline().split()))
dp = [0] * (k + 1)
dp[0] = 1
for i in c:
	tmp = dp[:]
	for j in range(k, i - 1, -1):
		tmp[j] |= dp[j - i] | dp[j - i] << i
	dp = tmp
b = bin(dp[-1])
ans = [i for i in range(k + 1) if b[-i - 1] == '1']
sys.stdout.write('{0}\n{1}'.format(len(ans), str(ans)[1:-1].replace(',', '')))


(n, k) = map(int, input().split())
dp = []
k += 1
for j in range(k):
	dp.append([0] * k)
dp[0][0] = 1
c = list(map(int, input().split()))
for i in range(n):
	for j in range(k - 1, c[i] - 1, -1):
		for t in range(j + 1):
			if t >= c[i] and dp[j - c[i]][t - c[i]] > 0:
				dp[j][t] = 1
			if dp[j - c[i]][t] > 0:
				dp[j][t] = 1
print(sum(dp[k - 1]))
for i in range(k):
	if dp[k - 1][i]:
		print(i)


from sys import stdin, stdout
(n, m) = map(int, input().split())
s = [0] + list(map(int, stdin.readline().strip().split()))
dp = [[[False for i in range(m + 1)] for j in range(m + 1)] for k in range(2)]
dp[0][0][0] = True
cur = 0
for i in range(1, n + 1):
	cur += 1
	cur %= 2
	last = (cur + 1) % 2
	for j in range(m + 1):
		for k in range(j + 1):
			if j - s[i] > -1 and k - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k] or dp[last][j - s[i]][k - s[i]]
			elif j - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k]
			elif k - s[i] > -1:
				dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k - s[i]]
			else:
				dp[cur][j][k] = dp[last][j][k]
n1 = 0
ans = ''
for i in range(k + 1):
	if dp[cur][k][i]:
		ans += str(i) + ' '
		n1 += 1
stdout.write(str(n1) + chr(10))
stdout.write(ans)


import sys
input = sys.stdin.buffer.readline
(n, k) = map(int, input().split())
cost = list(map(int, input().split()))
dp = [set() for i in range(500 + 1)]
dp[0].add(0)
for i in range(n):
	for j in range(k, -1, -1):
		if j + cost[i] <= k and len(dp[j]) > 0:
			for l in dp[j]:
				dp[j + cost[i]].add(l)
				dp[j + cost[i]].add(l + cost[i])
			dp[j + cost[i]].add(cost[i])
	dp[cost[i]].add(cost[i])
arr = []
for i in dp[k]:
	arr.append(i)
arr.sort()
print(len(arr))
for i in arr:
	print(i, end=' ')
print()


(n, k) = map(int, input().split())
dp = []
k += 1
for i in range(2):
	a = []
	for j in range(k):
		a.append([0] * k)
	dp.append(a)
dp[1][0][0] = 1
c = list(map(int, input().split()))
c = sorted(c)
for i in range(n):
	for j in range(c[i], k):
		for t in range(j + 1):
			if t >= c[i] and dp[1][j - c[i]][t - c[i]] > 0:
				dp[0][j][t] = 1
			if dp[1][j - c[i]][t] > 0:
				dp[0][j][t] = 1
	for j in range(k):
		for t in range(j + 1):
			if dp[0][j][t]:
				dp[1][j][t] = 1
	if sum(dp[1][k - 1]) == k:
		break
print(sum(dp[1][k - 1]))
for i in range(k):
	if dp[1][k - 1][i]:
		print(i)


def check(arr, l, s):
	m = []
	for _ in range(l + 1):
		m.append([1] + [0] * s)
	m[0][0] = 1
	for (i, ei) in enumerate(arr, 1):
		for j in range(s + 1):
			m[i][j] |= m[i - 1][j]
			if j >= ei:
				m[i][j] |= m[i - 1][j - ei]
				m[i][j] |= m[i - 1][j - ei] << ei
	return [i for i in range(s + 1) if m[-1][s] >> i & 1]
(n, s) = map(int, input().split())
l = list(map(int, input().split()))
L = check(l, n, s)
print(len(L))
print(*L, sep=' ')


(n, k) = map(int, input().split())
cost = list(map(int, input().split()))
dp = [set() for i in range(500 + 1)]
dp[0].add(0)
for i in range(n):
	for j in range(k, -1, -1):
		if j + cost[i] <= k and len(dp[j]) > 0:
			for l in dp[j]:
				dp[j + cost[i]].add(l)
				dp[j + cost[i]].add(l + cost[i])
			dp[j + cost[i]].add(cost[i])
	dp[cost[i]].add(cost[i])
arr = []
for i in dp[k]:
	arr.append(i)
arr.sort()
print(len(arr))
for i in arr:
	print(i, end=' ')
print()


import sys
input = sys.stdin.readline
(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
DP = []
for _ in range(k + 2):
	DP.append([False] * (k + 2))
DP[0][0] = True
for a in sorted(A):
	for i in range(k, a - 1, -1):
		for j in range(0, k + 1):
			DP[i][j] |= DP[i - a][j]
			if j >= a:
				DP[i][j] |= DP[i - a][j - a]
print(sum(DP[k]))
for i in range(k + 1):
	if DP[k][i]:
		print(i)