The Values You Can Make

Description(点击查看原题)

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 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 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

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 

题目分析

题意:一个人要用最多n个硬币凑出k元的价值,求用于凑成k元的硬币重新组合后,可以得到哪些价值,比如我们用价值为 1,4,5 的硬币凑成了k = 10 ,那么我们使用这三个硬币可以凑成价值有 0,1 ,4 ,5,6, 9,10 ,

解析:记dp[i][j] = 1 表示当前凑成了i 元,然后用凑成了 i 元的硬币可以凑成 j 元的价值,转移方程也很明显,dp[i][j] = max(dp[i-val][j] , dp[i-val][j-val]) ,其中我们将dp初始化为0,表示状态不存在,令dp[0][0] = 1 , 表示初始状态存在,因此状态转移方程也可以理解为只要dp[i-val][j] = 1 或者 dp[i-val][j-val] = 1 ,那么 dp[i][j] 就可以借助价值为val 的硬币从这两个状态转移而来了,所以dp[i][j] = 1。最后我们统计一下有多少 dp[k][j] = 1  并输出即可。

代码区

#include<iostream>
#include<cstdio>
#include<cstring>
//#define LOCAL = 1;
using namespace std;
typedef long long ll;int n, k;
int dp[510][510];   //dp[i][j] = 1 表示当前凑成了i 元,然后用凑成了 i 元的硬币可以凑成 j 元的价值
int val;int main()
{
#ifdef LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#endifwhile (scanf("%d%d", &n, &k) != EOF){memset(dp, 0, sizeof(dp));            //初始化为0,表示状态不存在dp[0][0] = 1;                        //最初的转移状态int tot = 0;                      //记录数量for (int i = 1; i <= n;i++){scanf("%d", &val);for(int j = k ; j >= val;j --){for(int t = k ; t>= 0 ; t --){if(dp[j-val][t] == 1)                 //前一个状态加上当前的硬币{if (dp[j][t] == 0 && j == k)     //统计数量,在第一次取得当前状态时统计tot++;dp[j][t] = 1;}if(t >= val && dp[j-val][t-val] == 1)   //前一个状态加上当前的硬币后,再用新硬币组成新的价值{if (dp[j][t] == 0 && j == k)     //统计数量,在第一次取得当前状态时统计tot++;dp[j][t] = 1;}}}}printf("%d\n", tot);for(int i = 0 ;i <= k ;i ++)  //输出满足的状态{if (dp[k][i] == 1)printf("%d ", i);}printf("\n");}return 0;
}

codeforces 687C (动态规划)The Values You Can Make相关推荐

  1. codeforces 688 E. The Values You Can Make(01背包+思维)

    题目链接:http://codeforces.com/contest/688/problem/E 题解:设dp[s1][s2]表示s1状态下出现s2是否合理.那么s1显然可以更具01背包来得到状态.首 ...

  2. Long Path CodeForces - 407B(动态规划+思维+公式推导)

    题意: 起点为1,终点为n+1,对应第i个各点,如果我奇数次到达i点,那么下一步走到a[i]的位子,如果是偶数次到达,那么下一步走到i+1的位子. 问从1走到n+1一共需要走多少步?结果对1e9+7取 ...

  3. By Elevator or Stairs? CodeForces - 1249E(动态规划)

    题意 n层楼,a[i] (0<i<n)表示从 i 楼到 i + 1 楼走楼梯的时间,b[i] (0<i<n)表示从 i 楼到 i + 1 楼乘电梯的时间,其中每一次乘电梯需要等 ...

  4. 动态规划训练10 [Coloring Brackets CodeForces - 149D]

    西安交大 软件53 蔡少斐 整理 Coloring Brackets CodeForces - 149D 题目大意: 给定合法的括号序列,让你给括弧上色,并且上色时一定要满足3个要求: (1)每个括号 ...

  5. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  6. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  7. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  8. codeforces gym-101741 Elevator 动态规划、单调队列

    题目 这里写链接内容 题解 注意:题目给出是按照时间给出的顺序. 我们考虑第iii个人要上的楼高h[i]" role="presentation" style=" ...

  9. codeforces gym-101745 D-Stamp Stamp Stamp动态规划

    题解 一道很不错的动态规划问题,首先这些印章一定是s的子串. 我们可以枚举s的子串然后进行check. 如何check,成了这道题的关键. 由于盖章的顺序不知道,所以我们可以使用动态规划的方法. 我们 ...

最新文章

  1. android.mk 里面内容介绍
  2. Error: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.8‘ not found (required by /usr/anaconda3/bin/)
  3. PyCharm 查找文件名
  4. 从事数据科学前必须知道的五件事儿
  5. 机器学习实验中的编程技术(part1)--numpy
  6. 阅读与html阅读的区别,w3c School读书笔记(一):HTML基础
  7. 庖丁解InnoDB之REDO LOG
  8. 一个通用的单元测试框架的思考和设计02-设计篇
  9. python矢量裁剪栅格代码_Python中使用面状矢量裁剪栅格影像,并依据Value值更改矢量属性...
  10. 【李宏毅2020 ML/DL】P43-44 More about Adversarial Attack | Images Audio
  11. Sun Virtualbox说明文件的Bug
  12. 单纯形法中大m法_线性规划 第五讲 单纯形法及进一步讲解(大M法).ppt
  13. 京东客小程序功能模块源码V6.0.2
  14. (Redis基础教程之八) 如何在Redis中管理Sets
  15. 这些优质的教育类公众号您知道么,非常实用!
  16. LOOP AT SCREEN ABAP
  17. Android心跳包(一)——心跳机制
  18. 副总经理(技术类)+技术总监都做些什么
  19. python attributeerror_Python 入门 错误显示 AttributeError: apos;moduleapos; object has no attribute apo...
  20. RetinaNet论文详解Focal Loss for Dense Object Detection

热门文章

  1. matlab将yuv文件的luma分量矩阵转换为图像显示
  2. realsense R200转成costmap_2d
  3. 小猫爪:PMSM之FOC控制00-专题简介
  4. 下拉词如何做,百度阿里抖音京东下拉框优化有什么用?
  5. 关于开展XXX的通知
  6. Mac上使用Royal TSX链接服务器
  7. 离线数仓(1):什么是数据仓库
  8. 【Minecraft】【ModPC】【我的世界】 我的世界电脑版如何进入网络游戏?
  9. 比pingpong,派安盈更好的第三方收款平台—APP store,steam,applovin,unity ads,大陆地区Google游戏社交等应用收款结汇
  10. ICLR 2022 不求甚解阅读笔记--强化学习类(1)