CodeForces 364 B. Free Market


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, 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.

Sample test(s)
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).
有没有相交的部分不重要,预处理出所有可能的组合重量,两两都是可以通过去掉相同的部分一次交换得到

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

bool dp[51*10001];

int main()
{
    int n,d,x,cnt=0,sum=0,w=0;
    cin>>n>>d;
    dp[0]=1;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        for(int j=(sum+=x);j>=x;j--)
        {
            if(dp[j-x]) dp[j]=true;
        }
    }
    while(true)
    {
        int j=w+d;
        while(!dp[j]&&j>w) j--;
        if(j==w) break;
        w=j;cnt++;
    }
    cout<<w<<" "<<cnt<<endl;
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值