CodeForces 632E Thief in a Shop(DP)

来源:互联网 发布:成都大旗软件 编辑:程序博客网 时间:2024/05/18 02:09

题意:给n个数,从中选k个(可以重复选)加起来,问最后有多少种不同的价值

思路:看起来很像一个多重背包,然而要准确的选到k个是不简单的,比如1 2 3 4 5,k=3,那么dp[6]=2,可是显然6也是可以由1,2,3来组成,所以直接多重背包会丢失解,解决方法是每个元素都减去a[1],令dp[i]为选了dp[i]个数价值为i,那么如果只用了k-2就凑成i,那么剩下的2个可以由a[1]来补上去,那么就恰好符合了题目要求的选k个数了。


#include<bits/stdc++.h>using namespace std;const int maxn = 1e3+5;int n,k,a[maxn],dp[maxn*maxn];int main(){    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);    sort(a+1,a+1+n);    n=unique(a+1,a+1+n)-(a+1);    for(int i=2;i<=n;i++)        a[i]=a[i]-a[1];    for(int i=1;i<=k*a[n];i++)        dp[i]=k+1;    for(int i=2;i<=n;i++)        for(int j=a[i];j<=k*a[i];j++)            dp[j]=min(dp[j],dp[j-a[i]]+1);    for(int i=0;i<=k*a[n];i++)        if(dp[i]<=k)            printf("%d ",a[1]*k+i);    return 0;}

Description

A thief made his way to a shop.

As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.

The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).

Find all the possible total costs of products the thief can nick into his knapsack.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.

The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.

Output

Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.

Sample Input

Input
3 21 2 3
Output
2 3 4 5 6
Input
5 51 1 1 1 1
Output
5
Input
3 33 5 11
Output
9 11 13 15 17 19 21 25 27 33


0 0
原创粉丝点击