The Values You Can Make

https://codeforces.com/problemset/problem/687/C

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 

C++版本一

1048ms

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG

using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a[N];
int dp[N][N];
bool b[N][N][N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<=k;i++){
        for(int j=0;j<=k;j++){
            dp[i][j]=-INF;
        }
    }
    dp[0][0]=0;
    for(int i=1;i<=n;i++){
        for(int j=k;j>=a[i];j--){
            for(int l=k-j;l>=0;l--){
                if(dp[j-a[i]][l]!=-INF&&!b[j-a[i]][l][i]&&dp[j][l]<dp[j-a[i]][l]+a[i]){
                    dp[j][l]=dp[j-a[i]][l]+a[i];
                    b[j][l][i]=1;
                }
                if(dp[l][j-a[i]]!=-INF&&!b[l][j-a[i]][i]&&dp[l][j]<dp[l][j-a[i]]+a[i]){
                    dp[l][j]=dp[l][j-a[i]]+a[i];
                    b[l][j][i]=1;
                }
            }
        }
    }
    priority_queue<int,vector<int>,greater<int> >ans;
    for(int i=0;i<=k;i++){
        if(k==dp[i][k-i]){
            ans.push(i);
        }
    }
    int len=ans.size();
    cout << len << endl;
    for(int i=0;i<len;i++){
        t=ans.top();
        ans.pop();
        cout << t << ((i!=len-1)?" ":"\n");
    }

    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG

using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a[N];
int dp[N][N];
bool b[N][N][N];
int ans[N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<=k;i++){
        for(int j=0;j<=k;j++){
            dp[i][j]=-INF;
        }
    }
    dp[0][0]=0;
    for(int i=1;i<=n;i++){
        for(int j=k;j>=a[i];j--){
            for(int l=k-j;l>=0;l--){
                if(dp[j-a[i]][l]!=-INF&&!b[j-a[i]][l][i]&&dp[j][l]<dp[j-a[i]][l]+a[i]){
                    dp[j][l]=dp[j-a[i]][l]+a[i];
                    b[j][l][i]=1;
                }
                if(dp[l][j-a[i]]!=-INF&&!b[l][j-a[i]][i]&&dp[l][j]<dp[l][j-a[i]]+a[i]){
                    dp[l][j]=dp[l][j-a[i]]+a[i];
                    b[l][j][i]=1;
                }
            }
        }
    }
    //priority_queue<int,vector<int>,greater<int> >ans;
    int cnt=0;
    for(int i=0;i<=k;i++){
        if(k==dp[i][k-i]){
            //ans.push(i);
            ans[++cnt]=i;
        }
    }
    //int len=ans.size();
    //cout << len << endl;
    cout << cnt << endl;
//    for(int i=0;i<len;i++){
//        t=ans.top();
//        ans.pop();
//        cout << t << ((i!=len-1)?" ":"\n");
//    }
    for(int i=1;i<=cnt;i++){
        cout << ans[i] << ((i!=cnt)?" ":"\n");
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本三

93ms

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG

using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a;
bool dp[N][N];
int ans[N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    dp[0][0]=1;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a);
        for(int j=k;j>=a;j--){
            for(int l=0;l<=k-a;l++){
                if(dp[j-a][l]){
                    dp[j][l]=dp[j][l+a]=1;
                }
            }
        }
    }
    int cnt=0;
    for(int i=0;i<=k;i++){
        if(dp[k][i]){
            ans[++cnt]=i;
        }
    }
    cout << cnt << endl;
    for(int i=1;i<=cnt;i++){
        cout << ans[i] << ((i!=cnt)?" ":"\n");
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本四

31ms的大佬代码

背包,DFS

全部评论

相关推荐

04-20 22:20
已编辑
门头沟学院 golang
27届,bg为四非本211硕,如题,导师不放实习,且每周至少一次线下组会(工作日),从研一上开始实习,然后我组在研一下引入了打卡机五段大厂分别是:美团到店、美团服务零售、快手电商、字节TikTok、字节CapCut。目前要结束我的第五段实习了(不会再刷第六段,好好搞学校的事,还有秋招)本来一直告诉自己的是“所有委屈到了终点再说”,过去告诉自己的终点自然还没到,但我觉得自己仿佛已经到了另一个终点,有感而发,写了这篇文章也许你会觉得为啥不尝试问问导师能不能实习,或者用其他让自己舒服的手段,我只能说,这很复杂,有导师的人自然会懂,这种一开始就把“利益冲突”摆明面上的招几乎就是不可能成功———————————————————我到底是怎么实习的?骗hr自己满勤,然后没有捷径,就是每周往返,第一段去的是北京美团,而学校在江苏,因此需要一周一次北京江苏往返,因为实习钱少,所以坐的基本是绿皮,难以入睡,下车后就是长达2小时的地铁去公司,地铁站上靠着人睡觉周末做什么?基本在做导师的科研or横向,学习的话很多时候就是尽力在晚上回到出租屋的时候学,这很难维持,但只能不断push自己如何破解打卡机?直接把打卡机偷了,或者使用指纹膜(当然我很早就做好了无法破解的准备,那就是找个长三角实习,每天早起去打卡完坐高铁去实习,从每周高铁往返变成每天)导师会压力吗?非常压力,实习的时候非常害怕微信弹出他的消息,PTSD了,有时候一周要往返两次学校,每次都跟要死了一样,之前真是情绪崩溃好几次,哈哈哈哈平时往返怎么平衡工作?我本来很晕车,为了不耽误公司和导师的进度,从车上一看电脑就头晕、吐,到后面可以随意在高铁、地铁、出租车上Coding,甚至不会再因为往返感到心累了,哈哈哈哈这一路已经淬炼出比较坚强的内心了,已经数不清多少次坐末班高铁从学校回公司,多少次凌晨6点爬起来赶车过去我会把这些当作是我人生的弯路,但现在,这些已经成为我宝贵的经验了。往后,我想我也能真正允许各种不好的情况出现了,因为我会真正把它当作我要解决的问题,而非抱怨,这又何尝不是终点呢?要照顾好身体,我不管怎么往返,一直非常在乎身体,会让自己睡够8小时,最近几星期培养早睡早起到公司健身后去工作的习惯,我觉得好身体很关键
gtgt..:很佩服,但是很恐怖,感觉已经从人类异化到高度运转的机器了
美团工作强度 2455人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务