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.

Examples
input
3 2
1 2 3
output
2 3 4 5 6
input
5 5
1 1 1 1 1
output
5
input
3 3
3 5 11
output

9 11 13 15 17 19 21 25 27 33

题意:给你n种物品以及每种的价值,每一种物品可以任意取多次,问恰好取k次物品能取到的所有可能价值。

思路:容易想到4维dp,用dp[i][j]表示取i次,价值为j是否存在,但是这样的复杂度为10^12爆了,所以要减少一维,先对n个数排序,然后n个数都减去第一个数(这样做的目的是恰好k次很难dp,n个数都减去最小的数后,第1个数就变为0,在这样的情况下,如果我们凑到价值为i的物品少于k件,如只用k-2件拼凑,那么另外两件可以看做都用第1个物品),然后用dp[i]表示最后取到价值为i的物品,dp就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
int dp[maxn*maxn],a[maxn]; int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
int t=a[1];
for(i=1;i<=n;i++){
a[i]-=t;
}
for(i=0;i<=1000000;i++)dp[i]=inf;
dp[0]=0; for(j=0;j<=1000000;j++){
for(i=1;i<=n;i++){
if(j>=a[i]){
dp[j]=min(dp[j],dp[j-a[i] ]+1);
}
}
}
int flag=1;
for(j=0;j<=1000000;j++){
if(dp[j]<=k){
printf("%d ",j+t*k);
}
}
printf("\n");
}
return 0;
}

最新文章

  1. Swift - 初始化Initialization
  2. MongoDB用户权限基本操作
  3. makefile文件编写
  4. Android开发优化
  5. 多线程和Boost::Asio
  6. ANDROID_MARS学习笔记_S01原始版_009_下载文件
  7. Shell编程速查手册
  8. rpath 与runpath
  9. 12-UIKit(View绘制、绘制曲线、绘制文字、贴图)
  10. mongodb中的排序和索引快速学习
  11. 【LeetCode】2.Add Two Numbers
  12. selenium之handle学习 多窗口、句柄
  13. CSAPP-过程调用,数据存储,缓冲区溢出
  14. 重磅!!!微软发布.NET Core 2.2
  15. 解决在使用gensim.models.word2vec.LineSentence加载语料库时报错 UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte......的问题
  16. IdentityServer4客户端如何获取自定义声明,了解一下?
  17. 项目Alpha冲刺(团队)-第一天冲刺
  18. Python学习笔记_1
  19. 【转】SEGGER Embedded Studio 新建stm32f103工程
  20. DOS 和 DDOS 攻击

热门文章

  1. Spring的自动装配与依赖注入
  2. spring ioc踏出第一步
  3. 【Linux】sudo配置文件讲解
  4. CS远控
  5. Pulsar vs Kafka,CTO 如何抉择?
  6. Springmvc中参数的绑定
  7. [系列] Go - 基于 GORM 获取当前请求所执行的 SQL 信息
  8. HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端
  9. uni-app开发经验分享一: 多页面传值的三种解决方法
  10. Atlas 2.1.0 实践(3)—— Atlas集成HIve