B. Free Market----暴力+dp

来源:互联网 发布:金庸哪派武功厉害知乎 编辑:程序博客网 时间:2024/06/08 14:32

B. Free Market
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.

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

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 nd (1 ≤ n ≤ 501 ≤ d ≤ 104) — the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (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.

Examples
input
3 21 3 10
output
4 3
input
3 51 2 3
output
6 2
input
10 1000010000 9999 1 10000 10000 10000 1 2 3 4
output
50010 6
Note

In the first sample John can act like this:

  • Take the first item (1 - 0 ≤ 2).
  • Exchange the first item for the second one (3 - 1 ≤ 2).
  • Take the first item (1 - 0 ≤ 2).

题目链接:http://codeforces.com/contest/364/problem/B

题意:有n个品种的物品,每个物品的价格为c[ i ],每次你可以从你拥有的物品中挑取部分去商店去兑换。兑换有如下限制:

(1)你拿去兑换的物品(不妨设拿去兑换的物品集合为s),都要换成集合s所没有的。比如(a,b)--->(v,a)就是不合法的

(2)你拿去兑换的物品集s1里的价值和cost1与你兑换回来的物品集s2里的价值和cost2需要满足:cost1+d>=cost2

每天你只能去兑换一次。

问通过兑换可以获得的物品集价值和的最大值以及所需的兑换天数


还是看了题解。。。思路很简单,暴力d,看看谁满足条件就可以了,题解一下子就看懂了,为什么我当初没有想到。。。

http://blog.csdn.net/u011645923/article/details/38797767


代码:

#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n,d;int dp[700000];int main(){scanf("%d%d",&n,&d);    int sum=0;    memset(dp,0,sizeof(dp));    dp[0]=1;    for(int i=1;i<=n;i++){        int x;        scanf("%d",&x);        sum+=x;        for(int j=sum;j>=x;j--){            if(dp[j-x]){                dp[j]=1;            }        }    }    int ans=0;    sum=0;    while(1){        sum+=d;//这一次的状态上限,暴力        int pos=-1;        for(int i=0;i<d;i++){            if(dp[sum-i]){//该次最大可到达的状态                pos=i;//记录位置                break;            }        }        if(pos==-1){            sum-=d;            break;        }        sum-=pos;        ans++;    }    printf("%d %d\n",sum,ans);return 0;}