Round #5 Problem B

Problem Description

John Doe has recently found a "Free Market" in his city that is the place where you can exchange some of your possessions for other things for free.

John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set{a,b} for set {v,a}. However, you can always exchange setx for any sety, unless there is itemp, such thatp occurs inx andp occurs iny.

For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of itemsx for a set of itemsy, ifs(x)+d<s(y) (s(x) is the total price of items in the setx).

During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.


Input

The first line contains two space-separated integers n,d (1≤n≤50,1≤d≤104) the number of items on the market and John's sense of justice value, correspondingly. The second line containsn space-separated integersci (1≤ci≤104).

 

Output

Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.


Sample Input
3 2
1 3 10
3 5
3
1 2

Sample Output
4 3
6 2


先找出所有的组合可能,若存在则标记成1,每次最优的解法是当前最优的组合方式+d,找最接近的组合法后更新


#include<stdio.h>
#include<string.h>
char dp[51*10001];
int main(void){
	int n,d,sum,k,max,count;
	while(scanf("%d%d",&n,&d)!=EOF){
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		max=count=sum=0;
		while(n--){
			scanf("%d",&k);
			for(int i=sum+=k;i>=k;i--)
				if(dp[i-k]==1)
                    dp[i]=1;
		}	
		while(1){
			int j=max+d;       
			while(dp[j]==0&&j>max) //µ±ÕÒµ½Ò»ÖÖ×éºÏʱÌø³ö
				j--;
			if(j==max)	//µ±ÔÚmax+dµÄ·¶Î§ÄÚÕÒ²»µ½¸ü´óµÄ¼ÛֵʱÌø³ö 
				break;
			max=j;		//¸üÐÂ×î´ó¼ÛÖµ 
			count++;	//ÿ´ÎÕÒµ½max+dµÄ×î´óÖµºó´ÎÊý¼ÓÒ» 
		}
		printf("%d %d\n",max,count);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值