Codeforces 688E The Values You Can Make

http://codeforces.com/contest/688/problem/E

E. The Values You Can Make
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. 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 xusing 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 c1, c2, ..., cn (1 ≤ ci ≤ 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

output

input

output

题意:给定n个钱及其面值,现在要用这些钱选出恰好总和为k。问在所有的方案中,用选中的钱,能组成哪些值。

思路:出一看,应该是个01背包。但是需要改进。改进为物品可以从两边同时装入。用dp[i][j]=1表示这个背包,左边装了i的物品,右边装了j的物品,这个状态可以达到。当枚举到一个价值为c的物品的时候,dp[i+c][j]=dp[i][j+c]=1(如果dp[i][j]=1)。那么,如果最后某个价i能被构成,一定是满足dp[i][k-i]=1。

 

PS:这是我写过的最短的E题,看tourist的代码看懂的他的思路。