D - Free Market CodeForces - 365D 背包求状态数+贪心 好题!

时间:2021-07-04 18:43:24

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, if s(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.

Example
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 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).



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

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

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

每天你只能去兑换一次。

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



思路:
训练时傻逼的一直在暴力模拟去贪心... 结果怎么计算天数给我整的orz...看了题解半天也没看懂,某巨告诉我这叫背包枚举状态数,然后求解.真的很巧妙我们先进行一个预处理,枚举出所有可能组成出的价值和,然后记录它。在进行交换时,因为要获得更大的价值,所以我们从已有的能换的起的最大价值开始往前找,买最大的!每交换了一次最大值天数+1,直到j==w即不能再交换了为止.
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
int dp[55*maxn];
int main()
{
int n,d,x,cnt=0,w=0,i,j;
cin>>n>>d;
memset(dp,0,sizeof(dp));
dp[0]=1;
ll sum=0;
for(i=1;i<=n;i++)
{
cin>>x;
sum+=x;
for(j=sum;j>=x;j--)
if(dp[j-x])
dp[j]=1;
}
while(0==0)
{
j=w+d;
while(!dp[j]&&j>w)
j--;
if(j==w)
break;
w=j;
cnt++;
}
printf("%d %d",w,cnt);
return 0;
}