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.

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 contains n space-separated integers ci (1 ≤ ci ≤ 104).

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.

NoteIn 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 <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
bool f[maxn];
int a[55];
int main() {
	int n, d;
	f[0] = 1;
	int mx = 5e5, sum = 0;
	scanf("%d%d", &n, &d);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		sum += a[i];
		for (int j = sum; j >= a[i]; j--) {
			if(f[j - a[i]])	f[j] = 1;
		}
	}
	int now = 0, tot = 0;
	while(1) {
		int lim = now + d;
		bool flag = 0;
		for (int i = 0; i < d; i++) {
			if(f[lim - i]) {
				now = lim - i;
				f[now] = 1;
				tot++;
				flag = 1;
				break;
			}
		}
		if(flag == 0) break;
	}
	printf("%d %d\n", now, tot);
}

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int n,d,c[55];
bool vis[55][500005];
vector<int> s;
void sol(int idx,int sum)
{
	if(idx == n)
		return;
	if(vis[idx][sum])
		return;
	vis[idx][sum] = 1;
	s.push_back(sum + c[idx]);
	sol(idx + 1,sum);
	sol(idx + 1,sum + c[idx]);
}

int main()
{
	cin >> n >> d;
	for(int i = 0; i < n; i++)
		cin >> c[i];
	sol(0,0);
	s.push_back(0);
	sort(s.begin(),s.end());
	int cur = 0,days = 0;
	while(1)
	{
		vector<int>::iterator it = lower_bound(s.begin(),s.end(),cur + d + 1);
		it--;
		if(*it <= cur)
			break;
		cur = *it;
		days++;
	}
	cout << cur << " " << days << endl;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1000005;
int dp[maxn];
vector<int>g;
int main()
{
    int i,j,n,d;
    cin>>n>>d;
    dp[0]=1;
    for(i=1;i<=n;i++){
        int m;
        scanf("%d",&m);
        for(j=maxn-1;j>=m;j--){
            if(dp[j-m]) dp[j]=1;
        }
    }
    for(i=maxn-1;i>=1;i--){
        if(dp[i]) g.push_back(i);
    }
    int s=0,w=0;
    while(g.size()){
        if(g[g.size()-1]-w>d) break;
        while(g.size()>1&&g[g.size()-2]<=w+d){
            g.pop_back();
        }
        w=g[g.size()-1];
        s++;
        g.pop_back();
    }
    cout<<w<<" "<<s<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// Useful constants
#define INF (int)1e6
#define INFL (long long)1e18
#define EPS 1e-9
// Useful hardware instructions
#define bitcount __builtin_popcount
#define gcd __gcd
// Useful container manipulation / traversal macros
#define all(a) a.begin(), a.end()
#define in(a,b) ( (b).find(a) != (b).end())
#define pb push_back
#define fill(a,v) memset(a, v, sizeof a)// fill originally
#define mp make_pair

// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define tr(container, it) for(aut(it,container.begin()); it != container.end(); it++)

#define llu long long unsigned
#define ld long

#define DEBUG 1
#define debug(x) {if (DEBUG)cout <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cout <<#x <<" = "; tr((x),it) cout <<*it <<", "; cout <<endl; }}

#define checkbit(n,b) ( (n >> b) & 1)
#define setbit(n,b) (n | ((1 << b)))
#define revbit(n,b) (n ^ ((1 << b)))
#define unsetbit(n,b) (n & (~(1 << b)))
#define rep(i,n) for(int i=0; i<(int)n;i++)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mod (1000000007L)

//typedef pair<int,pair<int,pair<int,int> > >  pp;

//freopen (fname"in","r",stdin);
//freopen (fname"out","w",stdout);

int n,d;
int data[10009],sum;

int dp[59][600001];

void solve()
{
    rep(i,n)rep(j,500009)
        {
        dp[i][j]=0;
        //indx[i][j]=-1;
        }

    for(int i=0;i<n;i++)
    {
        for(int j=1;j<=sum+d;j++){

            if(data[i]==j)
            {
                dp[i][j]=j;
                //indx[i][j]=i;

            }
            else if(data[i]>j)
            {
                if(i>0)
                {
                dp[i][j] =dp[i-1][j];
                //indx[i][j] =indx[i-1][j];
                }
                else
                {
                    dp[i][j]=0;//indx[i][j]=-1;
                }
            }else
            {
                if(i==0)
                {
                    dp[i][j]=data[i];
                }
                else if(dp[i-1][j-data[i]] + data[i] >= dp[i-1][j])
                {
                    dp[i][j]=dp[i-1][j-data[i]] + data[i];
                    //indx[i][j] = i;
                }
                else
                {
                    dp[i][j]=dp[i-1][j];
                    //indx[i][j]=indx[i-1][j];
                }
            }

        }
    }

    int itr=0;int val=0,cur;
    rep(i,10000000)
    {

        cur = val+d;

        int nval = dp[n-1][cur];

        if(nval<=val)break;
        itr++;

        val=nval;

    }

    cout<<val<<" "<<itr<<endl;

}

int main()
{
    s(n);
    s(d);
sum=0;
    rep(i,n)
    {
        s(data[i]);
sum += data[i];
    }

    solve();

}

#include<map>
#include<cmath>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 6e5+9;
bool dp[MAXN];
vector<int>v;
vector<int>::iterator cur, mx;
int main() {
    int n, c, d, i, j, sum;
    while(~scanf("%d%d", &n, &d)) {
        memset(dp, 0, sizeof(dp));
        sum = 0;
        dp[0]=1;
        for(i=0; i<n; ++i) {
            scanf("%d", &c);
            for(j=sum; j>=0; --j) {
                dp[j+c] |= dp[j];
            }
            sum += c;
        }
        for(i=0; i<MAXN; ++i) {
            if(dp[i]) v.push_back(i);
        }
        int day=0;
        mx = v.begin();
        while(true) {
            cur = upper_bound(v.begin(), v.end(), (*mx)+d);
            --cur;
            int i1=mx-v.begin(), i2=cur-v.begin();
            if(mx==cur) break;
            mx=cur;
            ++day;
        }
        printf("%d %d\n", *mx, day);
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int _n = 50 + 2;
const int _m = _n * 10000;
int a[_n], n;
bool vis[_m];

int main() {
    int d;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    std::sort(a, a + n);
    int sum = 0;
    for (int i = 0; i < n; ++i)
        if (a[i] - sum <= d)
            sum += a[i];
        else {
            n = i;
            break;
        }
    printf("%d ", sum);
    //
    vis[0] = true;
    for (int i = 0; i < n; ++i)
        for (int j = _m - 1; j >= 0; --j)
            if (vis[j])
                vis[j + a[i]] = true;
    int step = 0, cur = 0, nex;
    while (cur < sum) {
        nex = cur;
        for (int i = cur + 1; i <= sum && i - cur <= d; ++i)
            if (vis[i])
                nex = i;
        cur = nex;
        ++step;
    }
    printf("%d\n", step);
    return 0;
}

#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;
}

#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

bitset<555555>dp;
int N,D;
signed main(){
    cin>>N>>D;
    dp[0]=1;
    rep(i,N){
        int a;cin>>a;
        dp|=dp<<a;
    }

    vint lis;
    rep(i,555555)if(dp[i])lis.pb(i);

    int cur=0;
    int cnt=0;
    while(true){
        int nex=cur;
        while(nex+1<lis.size()&&lis[nex+1]-lis[cur]<=D)nex++;
        if(cur==nex)break;
        cnt++;
        cur=nex;
    }

    cout<<lis[cur]<<" "<<cnt<<endl;
    return 0;
}

#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>

#define REP(i,n) for(int i=0;i<(n);i++)
#define TR(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
#define ALL(x) x.begin(),x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define FILL(x,c) memset(x,c,sizeof(x))
#define SIZE(x) (int)(x).size()

#define MP make_pair
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int MAXV = 500010;

bool g[MAXV];
int c[60], f[MAXV];
int q[MAXV];

int main() {
    int n, d, m;
    while (cin >> n >> d) {
        m = 0;
        REP(i, n) cin >> c[i], m += c[i];
        CLEAR(g);
        g[0] = true;
        REP(i, n) {
            for (int j = m; j >= c[i]; --j) {
                if (g[j - c[i]]) g[j] = true;
            }
        }
        FILL(f, 0x7f);
        f[0] = 0;
        int h = 0, t = 0;
        q[0] = 0;
        for (int i = 1; i <= m; ++i) {
            if (!g[i]) continue;
            while (h <= t && i - q[h] > d) ++h;
            if (h > t) break;
            f[i] = f[q[h]] + 1;
            while (h <= t && f[i] <= f[q[t]]) --t;
            q[++t] = i;
        }
        int ans = 0;
        for (int i = 1; i <= m; ++i) if (f[i] < 0x7f7f7f7f) ans = i;
        cout << ans << " " << f[ans] << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second

typedef pair <int, int> pii;

const int maxn = 66;
const int MAX = 5e5 + 21;

int n, d, a[maxn], cnt[MAX], dp[MAX][2];

deque <pii> Q;

int main()
{
    ios::sync_with_stdio(0);    cin.tie(0);
    cin >> n >> d;

    for(int i = 0; i < n; i++)
        cin >> a[i];

    sort(a, a + n);

    dp[0][0] = 1;
    for(int i = 0; i < n; i++)
    {
        for(int j = a[i]; j < MAX; j++)
            if( dp[ j - a[i] ][0] )
                dp[j][1] = 1;

        for(int j = 0; j < MAX; j++)
            dp[j][0] |= dp[j][1];
    }

   // for(int j = 0; j < MAX; j++)
     //   if( dp[j][0] )
       // cerr << j << " " << dp[j][0] << "\n";

    Q.push_back( {0, 0} );
    for(int i = 1; i < MAX; i++)
        if( dp[i][0] )
        {
            dp[i][0] = 0;

            while( Q.size() && Q.front().Y < i - d )
                Q.pop_front();

            if( Q.size() )
            {
                dp[i][0] = 1;
                cnt[i] = Q.front().X + 1;

                while( Q.size() && Q.back().X > cnt[i] )
                    Q.pop_back();

                Q.push_back( {cnt[i], i} );
            }

        }

    for(int i = MAX - 1; i >= 0; i--)
        if( dp[i][0] )
        {
            cout << i << " " << cnt[i] << "\n";
            return 0;
        }
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int MAXN = 510000;
bool f[MAXN];

int main() {
    int n, d;
    scanf("%d%d", &n, &d);
    f[0] = 1;
    for(int i = 0, x; i < n; i ++) {
        scanf("%d", &x);
        for(int j = MAXN - x-1; j >= 0; --j) {
            f[j+x] |= f[j];
        }
    }

    f[0] = 0;
    int r = 0, sum = 0, ans = 0;
    while (r < MAXN) {
        int nxt = -1;
        while (r <= sum + d && r < MAXN) {
            if (f[r]) nxt = r;
            r ++;
        }
        if (nxt == -1) break;
        sum = nxt;
        ans++;
    }

    printf("%d %d\n", sum, ans);
    return 0;
}

#include <bits/stdc++.h>

#define INF 1e9
#define EPS 1e-8
#define pb push_back
#define pause system("pause");
#define exit exit(0)

using namespace std;
using ll = long long;

int n, d;
int c[50];
bool dp[55][500005];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> n >> d;
	for (int i = 0; i < n; ++i) {
		cin >> c[i];
	}

	dp[0][0] = true;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < 500005; ++j) {
			if (!dp[i][j]) {
				continue;
			}

			dp[i + 1][j + c[i]] = true;
			dp[i + 1][j] = true;
		}
	}

	vector<int> v;
	for (int j = 1; j < 500005; ++j) {
		if (dp[n][j]) {
			v.pb(j);
		}
	}

	int sz = (int)v.size();
	int i = -1, prev = 0, cnt = 0;
	while (i < sz) {
		while (i + 1 < sz && prev + d >= v[i + 1]) {
			++i;
		}

		if (i == -1 || i == sz || prev + d < v[i]) {
			break;
		}

		prev = v[i];
		++cnt, ++i;
	}

	cout << prev << ' ' << cnt << endl;
	//pause;
	return 0;
}

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <stack>
#include <cassert>
#include <ctime>
#include <cmath>
#include <queue>
#include <ctime>
#include <cassert>
#include <vector>
#include <cmath>
#include <stack>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int N = 500010;
const int M = 1e9 + 0.5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int pi = cos(-1.0);
const ll oo = 0x3f3f3f3f3f3f3f3fll;

#define pb push_back
#define sz(x) ((x).size())

int n, dp[N], d;

int main(){
	scanf("%d%d", &n, &d);
	dp[0] = 1;
	for(int i = 0; i < n; i++){
		int x;
		scanf("%d", &x);
		for(int i = 500000; i >= x; i--)
			dp[i] |= dp[i - x];
	}
	int ret = 0, cnt = 0;
	for(; 1;){
		int t = ret, x = ret;
		for(int j = x + 1; j <= x + d && j <= 500000; j++){
			if(dp[j]) t = j;
		}
		if(ret < t) ret = t, cnt++;
		else break;
	}
	cout << ret << " " << cnt << endl;
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) v.begin(),v.end()
#define sz size()
#define mp make_pair
#define pb push_back
#define rep(p,a,b) for(ll p=a ; p<b ; p++)
#define rap(p,a,b) for(ll p=a ; p<=b ; p++)
#define per(p,a,b) for(ll p=a ; p>=b ; p--)
#define F first
#define S second
using namespace std;

typedef vector <ll> vll;
typedef vector <ld> vld;
typedef pair< ll ,ll > pll;

bool dp[555556];

int main()
{
    ll t,i,j,k,ans,n,d,sum;

    //cin>>t;
    t=1;
    while(t--)
    {
        cin>>n>>d;
        vll v(n);
        sum=0;
        //memset(dp,0,sizeof(dp));

        dp[0]=1;
        rep(p,0,n)
        {
            cin>>v[p];
            per(q,555555,v[p])dp[q] |= dp[q-v[p]];
        }

        ll mx=0,nx,days=0;
        while(1)
        {
            nx = mx+d;
            while(!dp[nx])nx--;
            if(nx==mx)break;

            days++;mx = nx;
        }

        cout<<mx<<" "<<days;
    }

    return 0;
}

#include<iostream>
#include<cstring>
using namespace std;
int dp[550000];
int main()
{
    int n,d;
    while(cin>>n>>d)
    {
        int a=0,sum=0;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            sum+=a;
            for(int j=sum;j>=a;j--)
            {
                if(dp[j-a]!=0)dp[j]=1;
            }
        }
        int now=0,i;
        int day=0;
        for(;;)
        {
            now+=d;
            for(i=0;i<d;i++)
            {
                if(dp[now-i]!=0)break;
            }
            if(i==d)break;
            now-=i;day++;
        }
        cout<<now-d<<" "<<day<<endl;
    }
}

#include <cstdio>
#include <vector>
#include <algorithm>
#include <deque>
#include <iostream>
#include <cstring>
#include <set>
#include <map>
using namespace std;

int sum[500001];

int main()
{
    int n, d;
    scanf("%d%d", &n, &d);
    sum[0] = 1;
    for(int i = 0, c; i < n; ++i)
    {
        scanf("%d", &c);
        for(int s = 500000; s >= 0; --s)
            if(sum[s])
                sum[s + c] = 1;
    }
    vector<int> var;
    for(int i = 1; i < 500001; ++i)
        if(sum[i])
            var.push_back(i);
    int s = 0, c = 0;
    for(int i = 0; i < var.size(); ++i)
    {
        while(s + d >= var[i] && i < var.size())
            i++;
        if(s + d >= var[--i] && s != var[i])
        {
            s = var[i];
            c++;
        }
        else
            break;
    }
    printf("%d %d\n", s, c);
    return 0;
}

/*
By Tianyi Chen. All rights reserved.
Date: 2017-02-12
*/
#include<bits/stdc++.h>
using namespace std;
multiset<int>s;
char visit[500050];
int n,d,c,dis[500050],eit,ans;
int main() {
 memset(dis,0x33,sizeof dis);*dis=0;
 scanf("%d%d",&n,&d);*visit=1;
 while(n--) {
  scanf("%d",&c);
  for(int i=500000;i>=c;--i)visit[i]|=visit[i-c];
 }
 s.insert(0);
 for(int i=1;i<=500000;++i)if(visit[i]){
  for(;i-d>eit;++eit) {
   if(visit[eit])s.erase(s.find(dis[eit]));
  }
  if(s.empty())break;
  dis[i]=min(dis[i],*s.begin()+1);
  s.insert(dis[ans=i]);
 }
 printf("%d %d\n",ans,dis[ans]);
}

#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

const int N=50;
const int C=10000;
bool table[N*C+10];
int main() {
	int sum=0;
	int n,d;
	cin >> n >> d;
	table[0]=true;
	for(int i=0;i<n;i++){
		int c;
		cin >> c;
		for(int j=sum;j>=0;j--){
			table[j+c]|=table[j];
		}
		sum+=c;
	}
	int a=0;
	int p=0;
	int q=0;
	/*
	for(int i=0;i<=sum;i++){
		if(table[i]){
			cout << i << endl;
		}
	}*/
	for(int i=1;i<=sum;i++){
		if(i>p+d){
			if(p==q){
				break;
			}
			p=q;
			a++;
		}
		if(table[i]){
			q=i;
		}
	}
	if(p!=q){
		a++;
	}
	cout << q << " " << a << endl;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAXN=600000+100;

int n,d;
int dp[MAXN];
int main()
{
	while(~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 st=0,ans=0;
		while(1){
			st+=d;
			int pos=d;
			for(int i=0;i<d;i++) if(dp[st-i]){
				pos=i; break;
			}
			if(pos == d) {
				st-=d; break;
			}
			st-=pos;
			ans++;
		}
		printf("%d %d\n",st,ans);
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

bool dp[5*100000+100];
int c[55];

int main()
{
	int n,d;
	int sum=0;
	cin>>n>>d;
	for(int i=0;i<n;i++)
	{
		cin>>c[i];
		sum+=c[i];
	}
	dp[0]=1;
	for(int i=0;i<n;++i)
		for(int j=sum;j>=c[i];--j)
			dp[j] |= dp[j-c[i]];
	int val=0,cnt=0;
	while(1)
	{
		int i,tmp=val;
		for(i=val+d;i>val;--i)
			if(i<=sum && dp[i])
			{
				val=i;
				++cnt;
				break;
			}
		if(i==tmp) break;
	}
	cout<<val<<" "<<cnt<<endl;
}

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int n,d;
int c[51],dp[500001];
bool v[500001];
int main()
{
    cin>>n>>d;
    for(int i=1;i<=n;i++)
    cin>>c[i];
    sort(c+1,c+1+n);
    int sum=0,cnt;
    for(int i=1;i<=n;i++)
    {
        if(sum+d>=c[i])
        {
            sum+=c[i];
        }
        else
        {
            cnt=i-1;
            break;
        }
    }
    v[0]=1;
    for(int i=1;i<=n;i++)
    for(int j=sum;j-c[i]>=0;j--)
    {
        v[j]|=v[j-c[i]];
    }
    int t=0;
    for(int i=0;;t++)
    {
        if(i==sum)break;
        for(int j=i+d;j>i;j--)
        {
            if(v[j])
            {
                i=j;
            }
        }
    }
    cout<<sum<< " "<<t;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define mp make_pair
const int INF=(int)1e18;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);

    ///freopen("minions.in","r",stdin);
    ///freopen("minions.out","w",stdout);
    ///freopen("input.txt","r",stdin);
    ///freopen("output.txt","w",stdout);

    int n,d;
    cin >> n >> d;

    int m[n];
    bitset<(int)6e5> B;
    for(int i=0;i<n;i++)
    {
        cin >> m[i];
        B[i]=0;
    }
    B[0]=1;
    for(int i = 0 ; i < n ; i++)
    {
        B |= (B<<m[i]);
    }

    int ans=0;
    int days=0;

    ///cout << B[20] << "\n";

    while(true)
    {
        bool upd=0;
        for(int i=ans+d;i>ans;i--)
        {
            ///cout << i << " " << B[i] << "\n";
            if(B[i])
            {
                ///cout << ans << "\n";
                upd=1;
                ans=i;
                break;
            }
        }
        if(!upd)
            break;
        days++;
    }

    cout << ans << " " << days << "\n";

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int dp[550001],c[10005];
int main(){
    int n,d;
    cin>>n>>d;
    for(register int i=1;i<=n;i++){
        cin>>c[i];
    }
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(register int i=1;i<=n;i++){
        for(register int j=550000;j>=c[i];j--){
            if(dp[j-c[i]]==1)dp[j]=1;
        }
    }
    register int now=0,t=0;
    for(;t<=550000;t++){
        //cout<<now<<endl;
        int e=now+d;
        for(register int i=e;i>=now;i--){
            if(dp[i]==1){
                now=i;
                break;
            }
        }
        if(e==now+d)break;
    }
    //cout<<dp[6]<<endl;
    cout<<now<<' '<<t<<endl;
    return 0;
}

#include<bits/stdc++.h>

#define x first

#define y second

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define sq(x) ((x)*(x))

#define bit(x, y) (((x)>>(y))&1)

#define bctz(x) (__builtin_ctz(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzl(x))

#define bctzl(x) (__builtin_ctzl(x))

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}

template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}

template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}

template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}

template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}

template <class T> inline void println(T x) {print(x); putchar('\n');}

template <class T> inline void printsp(T x) {print(x); putchar(' ');}

template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}

template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}

template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}

template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 55

#define MM 500100

int c[NN];

int dp[MM];

int s[MM];

int main(){

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n,d;

	gn(n,d);

	for(int i=1;i<=n;i++) gn(c[i]);

	dp[0]=1;

	for(int i=1;i<=n;i++){

		for(int j=MM-1;j>=0;j--) if(dp[j]) dp[j+c[i]]=1;

	}

	int nn=0;

	for(int i=1;i<MM;i++) if(dp[i]) {

		s[nn++]=i;

	}

	int sum=0;

	int cnt=0;

	while(1){

		int pos=upper_bound(s,s+nn,sum+d)-s;

		pos--;

		if(s[pos]!=sum) {

			sum=s[pos];

			cnt++;

		}

		else break;

	}

	print(sum,cnt);

}

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<(k);i++)
#define mp make_pair
#define sz(a) (int)(a).size()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
//----head----
int val[500005]={1};
vector<int> V;
int main()
{
	int n,d;
	scanf("%d%d",&n,&d);
	rep(i,0,n)
	{
		int a;
		scanf("%d",&a);
		for(int i=500004;i>=a;i--)if(val[i-a])val[i]=1;
	}
	rep(i,0,500005)if(val[i])V.pb(i);
	int p=0,ans=0;
	while(1)
	{
		int _p=upper_bound(V.begin(),V.end(),V[p]+d)-V.begin()-1;
		if(_p==p)break;
		p=_p;ans++;
	}
	printf("%d %d\n",V[p],ans);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <ctime>

using namespace std;

#define F first
#define S second
#define mp(x, y) make_pair(x, y)

typedef long long LL;
typedef pair <int, int> pii;

const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
const int maxn = 2000000;

int n, m;
LL a[maxn], sum[maxn], cur, d, ans, can[maxn];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> d;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + a[i];

	for (int i = 1; i <= n;) {
		cur = sum[i - 1];
		int j = i - 1;
		while (j + 1 <= n && cur + d >= sum[j + 1] - cur)
			j++;
		if (j == i - 1) {
			can[0] = true;
			for (int k = 1; k <= j; k++)
				for (int l = sum[j]; l >= 0; l--)
					can[l + a[k]] |= can[l];

			cur = 0;
			while (cur != sum[j]) {
				int mx = 0;
				for (int k = cur; k <= cur + d; k++)
					if (can[k])
						mx = k;

				cur = mx;
				++ans;
			}

			cout << sum[j] << ' ' << ans;
			return 0;
		}
		i = j + 1;
	}

	can[0] = true;
	for (int k = 1; k <= n; k++)
		for (int l = sum[n]; l >= 0; l--)
			can[l + a[k]] |= can[l];

	cur = 0;
	while (cur != sum[n]) {
		int mx = 0;
		for (int k = cur; k <= cur + d; k++)
			if (can[k])
				mx = k;
		cur = mx;
		++ans;
	}

	cout << sum[n] << ' ' << ans;

	return 0;
}

#pragma comment(linker, "/STACK:268435456,268435456")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define MAXN 510001

int a[50];
bool dp[MAXN];

int main(){
    int n,d;
    scanf("%d %d", &n, &d),dp[0]=1;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i]);
        for(int j=MAXN-1;j>=a[i];j--)
            dp[j]|=dp[j-a[i]];
    }
    //for(int i=0;i<30;i++)
        //printf("%d %d\n", i, dp[i]);
    int curr=0,ans=0,ans2=0;
    while(true){
        int temp=curr;
        for(int i=d;i>=1;i--)
            if(dp[temp+i]){
                //printf("c%d %d\n",temp,i);
                temp+=i,ans++,ans2=max(ans2,temp);
                break;
            }
        if(temp==curr) break;
        curr=temp;
    }
    printf("%d %d\n", ans2, ans);
    //system("pause");
    return 0;
}

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool f[550005];
int n, d;
int main(){
	scanf("%d%d", &n, &d);
	int sum = 0;
	f[0] = 1;
	for (int c, i = 1; i <= n; ++i){
		scanf("%d", &c);
		sum += c;
		for (int j = sum; j >= c; --j)
			f[j] |= f[j - c];
	}
	int ret = 0, cur = 0;
	while (1){
		int j;
		for (j = cur + d; !f[j]; --j);
		if (j == cur){
			printf("%d %d\n", cur, ret);
			break;
		}
		cur = j; ++ret;
	}
	return 0;
}
/*
题意:
	交换市场上有n个物品, 每个物品有一个价值c[i]. 一开始你两手空空(也就是说拥有的物品集合为空集).
	可以用物品集合X交换物品集合Y, 当且仅当满足两个条件(X可以为空集):
		1. X交Y为空
		2. sum(X) >= sum(Y) - d
	想问两个问题:
		1. 最终获得的物品总价值最大是多少
		2. 获得最大总价值的最少交换次数是多少
考虑第一问, 可以发现两个性质:
	1. 价值越小越容易换到
	2. 如果换到了第k大那么就可以换到前k大
	所以可以O(n)搞定
考虑第二问, 可以发现两个性质:
	1. 换最大的肯定比换较小的好, 因为选择更多.(类似跳石子)
	2. 我们总是可以用所有物品去换, 因为有交集的情况可以把交集取掉, 是等价的.
	所以可以背包预处理, 每次换最大的, 看多少步到达最大值即可
*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)

int n, d;
int dp[1130000];

int main() {
    scanf("%d %d", &n, &d);

    int s = 0;
    dp[0]=1;

    for (int i = 0; i < n; i++) {
        int ci;
        scanf("%d", &ci);
        s += ci;

        for (int v = s; v >= ci; v--) {
            dp[v] |= dp[v-ci];
        }
    }

    int lla = -1e9;
    int la = 0;
    int days = 0;

    for (int i = 1; i <= s; i++) {
        if (dp[i] && i - la <= d) {
            if (i - lla > d) {
                days++;
                lla = la;
            }
            la = i;
        }
    }
    printf("%d %d\n", la, days);
}

#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define pi acos(-1.0)
#define MAX 1000001
bool a[6000001];
int x[51];
int main()
{
    int n,d,i,j;
    cin>>n>>d;
    for(j=0;j<n;j++)
        cin>>x[j];
    int sum=0;
    a[0]=true;
    for(i=0;i<n;i++)
    {
        sum+=x[i];
        for(j=sum;j>=x[i];j--)
        {
            a[j]|=a[j-x[i]];
        }

    }
    int cnt=0,res=0;
    for(;;cnt++)
    {
       for(j=d;j;j--)
       {
           if(a[res+j])
           {res+=j;break;}
       }
       if(!j)break;

    }
        cout<<res<<" "<<cnt;
return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int N,D;
int val[60];
int dp[10010*60];

int main()
{
    //freopen("input.txt","r",stdin);
    int i,j;
    cin>>N>>D;
    int sum=0;
    dp[0]=1;
    for(i=0;i<N;i++)
    {
        cin>>val[i];
        sum+=val[i];
        for(j=sum;j>=val[i];j--)
        {
            if(dp[j-val[i]])
            {
                dp[j]=1;
            }
        }
    }
    int day,ans;
    day=ans=0;
    while(true)
    {
        for(i=D;i>0;i--)
        {
            if(dp[ans+i])
            {
                ans+=i;
                day++;
                break;
            }
        }
        if(i==0)
        {
            break;
        }
    }
    cout<<ans<<" "<<day<<endl;
}

// In the name of Allah.
// We're nothing and you're everything.
// Ya Ali!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 6e5 + 14;
int n, d;
bitset<maxn> knapsack;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> d;
    knapsack[0] = 1;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        knapsack |= knapsack << x;
    }
    int ans = 0, step = 0;
    while(true){
        int nxt = ans + d;
        while(!knapsack[nxt])
            nxt--;
        if(nxt == ans)
            break;
        ans = nxt;
        step++;
    }
    cout << ans << ' ' << step << '\n';
}

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[600005];
int gett[600005];
int a[55];
int main() {
    int n, m;
    int i, j;
    scanf("%d%d", &n, &m);
    memset(dp, 0, sizeof(dp));
    for (i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    dp[0] = 1;
    for (i = 0; i < n; ++i) {
        for (j = 600000; j - a[i] >= 0; --j)
            if (dp[j - a[i]])
                dp[j] = 1;
    }
    gett[1] = m;
    j = 0;
    while (1) {
        j++;
        for (i = gett[j]; i >= 0; --i)
            if (dp[i])
                break;
        gett[j + 1] = i + m;
        if (gett[j + 1] == gett[j])
            break;
    }
    printf("%d %d\n", gett[j + 1] - m, j - 1);
}

#include <stdio.h>
int dp[51*10010];
int main()
{
	int n,d,i,x,sum,ans,ans1,j;
	scanf("%d%d",&n,&d);
		sum=0;
		dp[0]=1;
		for(i=0;i<n;i++){
			scanf("%d",&x);
			sum+=x;
			for(j=sum;j>=x;j--){
				if(dp[j-x])
					dp[j]=1;
			}
		}
		ans=ans1=0;
		while(1)
		{
			j=ans+d;
			while(j>ans&&!dp[j])
				j--;
			if(j==ans)
				break;
			ans=j;
			ans1++;
		}
		printf("%d %d\n",ans,ans1);

	return 0;
}

#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define MaxN 55

using namespace std;

int N, D, C[MaxN], g[MaxN], lastpos, q[MaxN * 10000], qh, sum[MaxN], f[MaxN * 10000], qt;
bool exist[MaxN * 10000];

int Min(int a, int b) {
    return a < b ? a : b;
}

int main() {
    int i, j, k, l;
    scanf("%d%d", &N, &D);
    for(i = 1; i <= N; ++i)
        scanf("%d", &C[i]);
    sort(C + 1, C + N + 1);
    for(i = 1; i <= N; ++i)
        sum[i] = sum[i - 1] + C[i];
    for(i = 1; i <= N; ++i) {
        if(C[i] <= sum[i - 1] + D) {
            lastpos = i;
        }
        else {
            break;
        }
    }
    exist[0] = 1;
    for(i = 1; i <= N; ++i)
        for(j = sum[lastpos] - C[i]; j >= 0; --j)
            exist[j + C[i]] |= exist[j];
    for(i = 1; i <= sum[lastpos]; ++i)
        if(exist[i])
        {
            for( ; q[qh] < i - D; ++qh);
            f[i] = f[q[qh]] + 1;
            q[++qt] = i;
        }
    printf("%d %d\n", sum[lastpos], f[sum[lastpos]]);
    return 0;
}

/*
 * Author:  DreamHigh
 * Created Time:  2013/12/1 19:59:56
 * File Name: T.cpp
 * solve: T.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 511010;
int a[maxn];
int dp[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,d;
    while(cin>>n>>d)
    {
        repf(i,1,n) scanf("%d",&a[i]);
        clr(dp);
        dp[0] = 1;
        repf(i,1,n)
        {
            repd(j,50*10000,0)
            {
                if(dp[j] && j + a[i] <= 50*10000)
                    dp[j + a[i]] = 1;
            }
        }
        int sum = 0,num = 0;
        while(1)
        {
            int flag = 0;
            repd(i,sum + d,sum + 1)
            {
                if(i > 50*10000) continue;
                if(dp[i])
                {
                    sum = i;
                    num++;
                    flag = 1;
                    break;
                }
            }
            if(flag == 0)
                break;
        }
        cout<<sum<<" "<<num<<endl;
    }
    return 0;
}

#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;
}

#include <iostream>
#include <set>

using namespace std;

int N, D;
bool poss[1000050];

int main()
{
    for(int i=0;i<1000050;i++) poss[i] = false;
    cin >> N >> D;
    poss[0] = true;
    for(int k=0;k<N;k++)
    {
        int c;
        cin >> c;
        for(int i=1000049; i>=c; i--) poss[i] |= poss[i-c];
    }
    //for(int i=0;i<20;i++) cout << poss[i];  cout << endl;
    int p = 0, d = 0;
    while(1)
    {
        int latest = -1;
        for(int i = p+1; i <= p+D; i++)
        {
            if(poss[i]) latest = i;
        }
        if(latest == -1) break;
        p = latest;
        d++;
    }
    cout << p << ' ' << d;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 51;
const int M = 10001 * N;
int dp[N][M];

int main()
{
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
  int n, d;
  cin >> n >> d;
  int a[n];
  for(int i = 0;i < n;i++)
    cin >> a[i];
  sort(a, a + n);

  dp[0][0] = 1;
  for(int i = 1;i <= n;i++){
    for(int j = 0;j < M;j++){
      dp[i][j] = dp[i - 1][j];
      if(j >= a[i - 1] && dp[i - 1][j - a[i - 1]])
        dp[i][j] = 1;
    }
  }
  int cur = 0;
  int ds = 0;
  while(true){
    int in = -1;
    for(int i = cur + 1;i < M && i <= cur + d;i++)
      if(dp[n][i])
        in = i;
    if(in == -1)break;
    cur = in;
    ds++;
  }
  cout << cur <<  ' ' << ds;
}

#include <iostream>
#include <cstring>
using namespace std;

const int MAXN=55;
const int MAXC=10005;
int c[MAXN];
int n,d;
const int MAXW=MAXC*MAXN;
bool p[MAXW];

void fill_p(void)
{
    memset(p, false, MAXW*sizeof(bool));
    p[0]=true;
    for(int j=0; j<n; j++)
        for(int i=MAXW-1; i>=c[j]; i--) p[i]|=p[i-c[j]];
}

int main()
{
    cin >> n >> d;
    for(int i=0; i<n; i++) cin >> c[i];
    fill_p();

    int price=0;
    int days=0;
    int r=0+0;
    int best=-1;

    //for(int i=0; i<20; i++) cout << i << ' ' << p[i] << endl;

    while(true)
    {
        best=-1;
        //cout << price << endl;

        for(int i=max(price+1, r); i<=price+d && i<MAXW; i++)
        {
            if(p[i]) best=i;
            r=i;
        }

        if(best==-1) break;
        price=best;
        days++;
    }

    cout << price << ' ' << days;
    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int read(){
	int x=0;
	char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return x;
}

const int N = 55;

int n,d,c[N];
int f[N*10000];

int main()
{
	int w=0;
	n=read(); d=read();
	for(int i=1;i<=n;i++) c[i]=read(),w+=c[i];

	f[0]=1;
	for(int i=1;i<=n;i++)
		for(int j=w;j>=c[i];j--)
			if(f[j-c[i]]) f[j]=1;

	int t=0,cur=0;
	for(;;){
		int flag=0;
		for(int i=min(cur+d,w);i>cur;i--)
			if(f[i]) { flag=1; cur=i; t++; break; }
		if(!flag) break;
	}
	printf("%d %d\n",cur,t);

	return 0;
}//hello

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 500000;
bool ok[N+10];
int dist[N+10];
int main(){
	int n, d;
	cin >> n >> d;
	vector<int> c(n);
	rep(i, n) cin >> c[i];
	ok[0] = 1;
	rep(i, n){
		for(int j = N; j >= c[i]; j--){
			if(ok[j-c[i]]) ok[j] = 1;
		}
	}
	vector<int> v;
	rep(i, N+10){
		if(ok[i]) v.push_back(i);
	}

	memset(dist, -1, sizeof(dist));
	dist[0] = 0;
	rep(i, v.size()){
		if(dist[i] < 0) continue;
		int pos = upper_bound(ALL(v), v[i]+d)-v.begin()-1;
		if(pos < 0) continue;
		if(dist[pos] < 0 || dist[pos] > dist[i]+1){
			dist[pos] = dist[i]+1;
		}
	}
	int ans = 0, days = 0;
	for(int i = v.size()-1; i >= 0; i--){
		if(dist[i] > 0){
			ans = v[i], days = dist[i];
			break;
		}
	}
	cout << ans << ' ' << days << endl;

	return 0;
}

#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define MOD 1000000007LL
#define endl "\n"
#define fst first
#define snd second
const ll UNDEF = -1;
const ll INF=1e18;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define DEBUG_CAT
#ifdef DEBUG_CAT

#define dbg(...)   printf( __VA_ARGS__ )
#else
#define dbg(...)   /****nothing****/
#endif
static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof (stdinBuffer);
static const char* stdinPos = stdinDataEnd;

void readAhead(size_t amount)
{
    size_t remaining = stdinDataEnd - stdinPos;
    if (remaining < amount) {
       memmove(stdinBuffer, stdinPos, remaining);
       size_t sz = fread(stdinBuffer + remaining, 1, sizeof (stdinBuffer) - remaining, stdin);
       stdinPos = stdinBuffer;
       stdinDataEnd = stdinBuffer + remaining + sz;
       if (stdinDataEnd != stdinBuffer + sizeof (stdinBuffer))
         *stdinDataEnd = 0;
    }
}

int readInt()
{
    readAhead(16);

    int x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}
char readCh()
{
    readAhead(16);
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    char ans=*stdinPos;
    ++stdinPos;
    return ans;
}
const int mn=50*10000+1;
bool f[mn];
int c[51];
vector<int> v;
int bins(int start, int key) {
  int imin=0,imax=v.size();
  while(imin<imax) {
    int imid=(imin+imax)>>1;
    if (v[imid]<=key) imin=imid+1;
    else imax=imid;
  }
  return imin-1;
}
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n=readInt(),d=readInt();
  for (int i=0;i<n;i++) c[i]=readInt();
  f[0]=1;
  int cur=0,nxt=1;
  for (int i=0;i<n;i++) {
    int now=c[i];
    for (int x=mn-1;x>=now;x--) {
      f[x]|=f[x-now];
    }
  }
  for (int x=0;x<mn;x++) {
    if (f[x]) {
      //printf("x:%d\n",x);
      v.PB(x);
    }
  }
  int step=0;
  int now=0;
  while(1) {
    int aft=bins(now,v[now]+d);
    if (aft==now) break;
    now=aft;
    step++;
  }
  printf("%d %d\n",v[now],step);
}

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

#define MP make_pair
#define PB push_back
#define FF first
#define SS second

#define FORN(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)(n) - 1; i >= 0; i--)

#define MOD 1000000007
#define INF 2000000000

const int MAXN = 55;
const int MAXD = 10010;

int c[MAXN], good[MAXN*MAXD];

int n, d;

int main() {
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++) scanf("%d", &c[i]);

    memset(good, 0, sizeof good); good[0] = 1;

    for (int i = 0; i < n; i++) {
        for (int j = MAXN * MAXD - 1; j >= 0; j--) {
            if (j + c[i] < MAXN * MAXD && good[j]) good[j + c[i]] = 1;
        }
    }

    int res = 0; int days = 0; bool ok = true;

    while (ok) {
        ok = false;

        for (int i = min(MAXN * MAXD - 1, res + d); i > res; i--) {
            if (good[i]) {
                res = i; days++; ok = true; break;
            }
        }
    }

    printf("%d %d\n", res, days);
    return 0;
}

#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cfloat>
#include <numeric>
#include <cassert>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int oo = 0x3f3f3f3f;
const double eps = 1e-9;

#define sz(c) int((c).size())
#define all(c) (c).begin(), (c).end()
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--)
#define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)

const int MAXP = 64*10000;
bool sums[MAXP];
int N, D;
int main() {
	memset(sums,0,sizeof(sums));
	sums[0] = 1;
	cin >> N >> D;
	int MAXI = 1;
	while(N--){
		int P;
		cin >> P;
		MAXI += P;
		FORD(i,P,MAXI)sums[i] |= sums[i-P];
	}
	int can = 0;
	int days = 0;
	int to = 0;
	int last = 0;
	while(true){
		while(to < can + D){
			++to;
			if(sums[to])last = to;
		}
		if(can == last)break;
		can = last;
		days++;
	}
	cout << can <<  " " << days << endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

int main()
{
	int n, d;
	cin>>n>>d;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin>>a[i];

	int su = accumulate(a.begin(), a.end(), 0);
	sort(a.begin(), a.end());
	vector<int> s(su+1, 0);
	s[0] = 1;
	for (int i = 0, S = 0; i < n; ++i )
	{
		int t = a[i];
		S += a[i];
		for (int j = S; j >= t; --j)
			s[j] |= s[j-t];
	}

	vector<int> maxLess(su+1, 0);
	int ml = 0;
	for (int i = 1; i <= su; ++i)
	{
		if (s[i])
			ml = i;
		maxLess[i] = ml;
	}

	int M = 0, moves = 0;
	while (M < su)
	{
		int t = M + d;
		if (t >= su)
		{
			M = su;
			++moves;
		}
		else
		{
			if (maxLess[t] == M)
				break;
			M = maxLess[t];
			++moves;
		}
	}

	cout<<M<<" "<<moves;
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
bool dp[500050];
int trace[500050];
int n,d;
int a[55];

int main()
{
    //freopen("FreeMarket.INP","r",stdin);
    //freopen("FreeMarket.OUT","w",stdout);
    scanf("%ld%ld",&n,&d);
    for (int i=1; i<=n; i++)
        scanf("%ld",&a[i]);
    memset(dp,false,sizeof(dp));

    //day con co tong = S
    dp[0]=true;
    for (int i=1; i<=n; i++)
    for (int j=500000; j>=0; j--)
    if ((dp[j]==true) && (j+a[i]<=500000))
        dp[j+a[i]]=true;

     // tinh san ham trace
    int pos;
    for (int i=0; i<=500000; i++)
    {
        if (dp[i]==true) pos=i;
        trace[i]=pos;
    }
    // tim so ngay + kq = "GREEDY"
    int now=0, day=0;
    while (now <=500000)
    {
        if (trace[min(now+d,500000)]==now) break;
        now=trace[min(now+d,500000)];
        day++;
    }
    printf("%ld %ld",now,day);
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <ctime>

using namespace std;

long long gcd(long long a, long long b) {
    return (b == 0 ? a : gcd(b, a % b));
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, d;
    cin >> n >> d;
    vector<int> c(n);
    for (int i = 0; i < n; i++)
        cin >> c[i];
    vector<int> a(n * 10000 + 1000);
    a[0] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = (n - 1) * 10000; j >= 0; j--)
            if (a[j] == 1)
                a[j + c[i]] = 1;
    }
    set<int> s;
    for (int i = 1; i <= d; i++)
        if (a[i] == 1)
            s.insert(i);
    int k = d + 1;
    int ans1 = 0, ans2 = 0;

    while (!s.empty()) {
        int x = *(--s.end());
        ans1 = x;
        ans2++;
        int y = *(s.lower_bound(-1));
        while (!s.empty() && y <= ans1) {
            s.erase(y);
            y = *(s.lower_bound(-1));
        }
        while (k <= n * 10000 && k <= ans1 + d) {
            if (a[k] == 1)
                s.insert(k);
            k++;
        }
    }
    cout << ans1 << " " << ans2 << endl;
    return 0;
}

#include <stdio.h>
const int N=550000;
int n,d,a[N];
int main(){
  scanf("%d%d",&n,&d);
  for(int i=0,x;i<n&&1==scanf("%d",&x);++i)
    for(int j=N;j-->x;)if(a[j-x]==j-x)a[j]=j;
  for(int i=1;i<N;++i)if(a[i]<1)a[i]=a[i-1];
  for(int i=0,x=0;i<N;++i)
    if(x==a[x+d]){
      printf("%d %d\n",x,i);
      break;
    }else x=a[x+d];
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std ;
const int maxn = 511111 ;

int num[maxn];
int q[maxn] ;
int main()
{
	int i , j , n , m , k;
	scanf("%d%d" , &n , &m ) ;
	memset( num , 0 , sizeof(num)) ;
	num[0] = 1 ;
	for( i = 1 ; i <= n ; i++ )
	{
		scanf("%d" ,&k ) ;
		for( j = 500000 ; j >= k ; j-- )
		{
			if( num[j-k] ) num[j] =  1 ;
		}
	}
/*	for( i = 0 ; i <= 500000 ; i++ )
		if( num[i] )
			printf("i = %d\n" , i ) ;*/
	int tot = 0 ;
	for( i =  0  ; i <= 500000 ; i++ )
		if( num[i] )
			q[++tot] = i ;

	q[0] = 0 ;
	int tt = 0 ;
	int ans = 0 ;
	q[++tot] = 1111111111 ;
	for( i = 1 ; i <= tot ;)
	{
		if( q[i] - ans <= m )
		{
			i++ ;
		}
		else
		{
			i-- ;
			if( ans == q[i] ) break ;
			ans = q[i] ;
			tt++ ;
		}
	}
	printf("%d %d\n" , ans , tt ) ;
	return 0 ;
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=52;
int n,d;
int c[N],sum[N];
int dp[N][N*10000];
vector<int> v;

int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++)scanf("%d",&c[i]);
    dp[0][0]=1;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<=sum;j++){
            dp[i+1][j]|=dp[i][j];
            dp[i+1][j+c[i+1]]|=dp[i][j];
        }
        sum+=c[i+1];
    }
    for(int i=0;i<=sum;i++)if(dp[n][i])v.push_back(i);
    int now=0,ans=0;
    while(1){
        int nex=v[upper_bound(v.begin(),v.end(),now+d)-v.begin()-1];
        if(now==nex)break;
        now=nex;ans++;
    }
    cout<<now<<" "<<ans<<endl;
}

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <string.h>
#include <sstream>
#include <cmath>
#include <math.h>
#include <queue>
#include <deque>
#include <cassert>
#include <time.h>

#define forn(i,n) for (int i = 0; i < (int)n; i++)
#define fornd(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define forab(i,a,b) for (int i = (int)a; i <= (int)b; i++)
#define forabd(i, b, a) for (int i = (int)(b); i >= (int)(a); i--)
#define forit(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define _(a, val) memset (a, val, sizeof (a))
#define sz(a) (int)(a).size()
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

#ifdef _DEBUG
#define dbg(...) {fprintf(stderr, __VA_ARGS__); fflush(stderr);}
#define dbgx(x) {cerr << #x << " = " << x << endl;}
#define dbgv(v) {cerr << #v << " = {"; for (typeof(v.begin()) it = v.begin(); it != v.end(); it ++) cerr << *it << ", "; cerr << "}"; cerr << endl;}
#else
#define dbg(...) { }
#define dbgx(x) { }
#define dbgv(v) { }
#endif

typedef long long lint;
typedef unsigned long long ull;
typedef long double ld;

const lint LINF = 1000000000000000000LL;
const int INF = 1000000000;
const long double eps = 1e-9;
const long double PI = 3.1415926535897932384626433832795l;

using namespace std;

void prepare (string s)
{
#ifdef _DEBUG
    freopen ("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
#else
    if (s.length() != 0)
    {
        freopen ((s + ".in").c_str(), "r", stdin);
        freopen ((s + ".out").c_str(), "w", stdout);
    }
#endif
}

const int NMAX = 55, MAXW = 615555;
int n, d;
int c[NMAX];
int dp[MAXW];

void read()
{
    scanf("%d %d", &n, &d);
    forn(i, n) scanf("%d", &c[i]);
}

void solve()
{
    forn(i, MAXW) dp[i] = INF;
    dp[0] = 0;

    int last = 0;
    forn(i, n)
    {
        for(int j = last; j >= 0; j--)
        {
            dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
        }
        last += c[i];
    }

    int ans = 0;
    int id = 0;
    for(; ;)
    {
        int c = 1;
        int f = -1;
        while(c <= d)
        {
            if (dp[id + c] < INF)
                f = id + c;
            c ++;
        }
        if (f == -1)
            break;
        id = f;
        ans ++;
    }

    printf("%d %d\n", id, ans);
}

int main ()
{
    prepare ("");

    read();
    solve();

    return 0;
}

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;

bool dp[50 * 10001];
int main() {
  int n, d;
  scanf("%d %d", &n, &d);
  vector<int> value(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &value[i]);
  }
  sort(value.begin(), value.end());
  set<int> comb;
  comb.insert(0);
  for(const auto& i : value) {
    const auto iter = comb.lower_bound(i - d);
    if (iter == comb.end() || *iter + d < i) continue;
    for (auto j = comb.rbegin(); j != comb.rend(); ++j) {
      comb.insert(*j + i);
    }
  }
  int assert_value = *comb.rbegin(), total = 0;
  dp[0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = total + value[i]; j >= value[i]; j--) {
      dp[j] |= dp[j - value[i]];
    }
    total += value[i];
  }
  int counter = 0, max_value = 0;
  while (true) {
    int upper = max_value + d;
    while(upper > max_value && (upper > total || !dp[upper])) upper--;
    if(upper == max_value) break;
    counter++;
    max_value = upper;
  }
  assert(max_value == assert_value);
  printf("%d %d\n", max_value, counter);
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=55;
const int maxm=1e7;
int n,d;
int c[maxn],dp[maxm];
int sum;
int main()
{
	scanf("%d%d",&n,&d);
	for(register int i=1;i<=n;++i) scanf("%d",&c[i]),sum+=c[i];

	sum+=d;
	dp[0]=1;
	for(register int i=1;i<=n;++i)
	{
		for(register int j=sum-c[i]+1;j>=0;--j)
		{
			if(dp[j]) dp[j+c[i]]=1;
		}
	}

	int now=0,step=0;
	while(1)
	{
		int nxt=now+d;
		for(register int i=nxt;i>now;--i)
		{
			if(dp[i]&&i<=sum)
			{
				now=i;
				break;
			}
		}
		if(nxt==now+d) break;
		step++;
	 }

	printf("%d %d\n",now,step);
	return 0;
}

#include<stdio.h>
int bag[550000];
int main()
{
	int i,j,n,d,c;
    int sum,value,days;
	while(~scanf("%d%d",&n,&d))
	{
		sum=value=days=0;
		bag[0]=1;
		for(i=0;i<n;i++)
		{
			scanf("%d",&c);
			for(j=(sum+=c);j>=c;j--)
				bag[j]|=bag[j-c];
		}
		while(true)
		{
			for(j=value+d;j>value;j--)
				if(j<=sum&&bag[j]!=0) break;
				if(j==value) break;
				value=j;
				days++;
		}
		printf("%d %d\n",value,days);
	}
	return 0;
}

#include<cstdio>
#include<iostream>
using namespace std;
const int N=6e5+5;
int n,d,c[51];
int last_maxn,maxn;
bool f[N];
int find(int l,int r)
{
	for(int i=r;i>=l;i--)
	if(f[i]) return i;
	return l;
}
int main()
{
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;i++)
	scanf("%d",&c[i]);
	f[0]=1;
	for(int i=1;i<=n;i++)
	for(int j=N;j>=c[i];j--)
	if(f[ j-c[i] ]) f[j]=1;
	for(int i=1;;i++)
	{
		last_maxn=maxn;
		maxn=find(maxn,d+last_maxn);
		if(last_maxn!=maxn) continue;
		printf("%d %d\n",maxn,i-1);
		return 0;
	}
}

#include <algorithm>

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <string>
#include <vector>
#include <iostream>

using namespace std;

#define TRACE(x) cout << #x << " = " << x << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)

typedef long long llint;

const int MAXS = 500010;

bool f[MAXS];

int main(void) {
  int n, d;
  scanf("%d %d", &n, &d);

  f[0] = 1;
  REP(i, n) {
    int x;
    scanf("%d", &x);
    for (int j = MAXS-1-x; j >= 0; --j)
      f[j+x] |= f[j];
  }

  int r = 1, sum = 0, ans = 0;
  while (r < MAXS) {
    int next = -1;
    while (r <= sum+d && r < MAXS) {
      if (f[r]) next = r;
      r++;
    }

    if (next == -1) break;
    sum = next, ans++;
  }

  printf("%d %d\n", sum, ans);
  return 0;
}

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<ctype.h>
using namespace std;

#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> Pi;

int n,inp[100],d;
int s[550010],tmp[550010];

int main()
{
    scanf("%d%d",&n,&d);
    int i,j;
    for(i=1;i<=n;i++)scanf("%d",inp+i);
    tmp[0]=1;
    for(i=1;i<=n;i++){
        for(j=1;j<=10000*i;j++){
            if(j-inp[i]>=0)s[j]=tmp[j]+tmp[j-inp[i]];
            else s[j]=tmp[j];
            if(s[j]>0)s[j]=1;
        }
        for(j=1;j<=10000*i;j++)tmp[j]=s[j];
    }
    int cnt=0;
    for(i=0;i<=10000*n;){
        int tmp=i;
        for(j=i+d;j>i;j--){
            if(s[j]){i=j;cnt++;break;}
        }
        if(tmp==i){printf("%d %d\n",i,cnt);break;}
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int MAX = 54;
int n, d;
int a[MAX];
int dp[1000000];

int main() {
	int i, j;
	int s = 0;

	scanf("%d%d", &n, &d);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		s += a[i];
	}

	dp[0] = 1;
	for (i = 0; i < n; i++) {
		for (j = s; j >=a[i]; j--) {
			dp[j] |= dp[j - a[i]];
		}
	}

	int cur = 0;
	int lvl = 0;

	while (1) {
		for (i = cur + d; i != cur; i--) {
			if (dp[i]) break;
		}

		if (i == cur) break;

		cur = i;
		lvl ++;
	}

	printf ("%d %d\n", cur, lvl);

	return 0;
}

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define N 52
#define C 10005

bool pack[N * C];
int items[N];
int sum;;

void Read(int n, int d)
{
	sum = 0;
	for (int i=0; i<n; ++i) {
		scanf("%d", &items[i]);
		sum += items[i];
	}
}

void Solve(int n, int d)
{
	memset(pack, 0, sizeof(pack));
	pack[0] = true;
	for (int i=0; i<n; ++i) {
		for (int j=sum; j>=items[i]; --j) {
			if (pack[j-items[i]]) { pack[j] = true; }
		}
	}

	int last = - N * C;
	int buy = 0;
	int step = 0;
	for (int i=1; i<=sum; ++i) {
		if (pack[i]) {
			if (last + d >= i) {
				buy = i;
			} else if (buy + d >= i) {
				last = buy;
				buy = i;
				++step;
			} else {
				break;
			}
		}
	}

	printf("%d %d\n", buy, step);
}

int main()
{
	int n, d;

	while (EOF != scanf("%d %d", &n, &d)) {
		Read(n, d);
		Solve(n, d);
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define vec vector
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rdv(type, v, n) vector<type> v(n); rep(i, 0, n) cin >> v[i]
#define prv(v) rep(i, 0, v.size()) {if (i) cout << ' '; cout << v[i];} cout << '\n'
#define rdm(type, v, h, w) vector<vector<type>>(h, vector<type>(w)) v; rep(i, 0, h) rep(j, 0, w) cin >> v[i][j]
#define prm(v) rep(i, 0, v.size()) {rep(j, 0, v[i].size()) {if (j) {cout << ' ';} cout << v[i][j];} cout << '\n';}
typedef long long ll;
void solve();
int main() {ios::sync_with_stdio(false); cin.tie(0); cout << fixed; cout.precision(12); solve(); return 0;}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void pr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void pr(const Head& head, const Tail&... tail) {cout << head << ' '; pr(tail...);}

const int MAX_SUM = 5e5 + 10;

bool vis[MAX_SUM];

void solve() {
  fill(vis, vis + MAX_SUM, false);
  rd(int, n, d);
  vec<int> oks;
  oks.pb(0);
  rep(i, 0, n) {
    rd(int, x);
    per(j, sz(oks) - 1, 0) {
      if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) {
        oks.pb(x + oks[j]);
        vis[x + oks[j]] = true;
      }
    }
  }
  sort(oks.begin(), oks.end());
  int ans = 0, cur = 0;
  while (true) {
    int idx = upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1;
    if (idx >= sz(oks) || idx == cur) {
      break;
    }
    ans++;
    cur = idx;
  }
  pr(oks[cur], ans);
}//hello

#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstdio>

#define MP 510000
#define MN 55
using namespace std;

int a[MN];
int T[MP], V[MP];
int E[MP], EN;

int main() {

	int i, k, n, d;

	cin >> n >> d;
	for (i = 0; i < n; i ++) scanf("%d", a + i);
	T[0] = 1;
	int S = 0;

	for (i = 0; i < n; i ++) {
		for (k = S; k >= 0; k --) if (T[k]) {
			if (k + a[i] >= MP) continue;
			T[k + a[i]] = 1;
		}
		S += a[i];
	}
	for (i = 0; i <= S; i ++) if (T[i]) {
		E[EN ++] = i;
	}
	memset(V, -1, sizeof(V));
	V[0] = 0;

	for (i = 0; i <= S; i ++) if (V[i] != -1) {
		int e = i + d;
		int st = upper_bound(E, E + EN, i) - E;
		int en = upper_bound(E, E + EN, e) - E - 1;
		k = en;
		if (V[E[k]] == -1) V[E[k]] = V[i] + 1;
		if (V[E[k]] > V[i] + 1) V[E[k]] = V[i] + 1;

/*		for (k = st; k <= en; k ++) {
			if (V[E[k]] == -1) V[E[k]] = V[i] + 1;
			if (V[E[k]] > V[i] + 1) V[E[k]] = V[i] + 1;
		}*/
	}
	for (i = MP - 1; i >= 0; i --) if (V[i] != -1) break;
	printf("%d %d\n", i, V[i]);
}

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <stdio.h>
#include <bits/stdc++.h>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

inline void setmin(int &x, int y) { if (y < x) x = y; }
inline void setmax(int &x, int y) { if (y > x) x = y; }
inline void setmin(ll &x, ll y) { if (y < x) x = y; }
inline void setmax(ll &x, ll y) { if (y > x) x = y; }

const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

bitset<50 * 10000 + 1> dp;
int ans[50 * 10000 + 1];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 0.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, d;
    cin >> n >> d;
    int a[n];
    rep(i, 0, n)
        cin >> a[i];
    sort(a, a + n);
    int pos = 0, sum = 0;
    while (pos < n && sum + d >= a[pos])
        sum += a[pos++];
    n = pos;
    dp.set(0);
    rep(i, 0, n)
        dp |= (dp << a[i]);
    fill(ans, ans + 50 * 10000 + 1, inf);
    ans[0] = 0;
    multiset<int> vals;
    vals.insert(0);
    pos = 0;
    rep(i, 1, 50 * 10000 + 1)
    {
        while (pos + d < i)
            vals.erase(vals.find(ans[pos++]));
        if (dp.test(i))
            setmin(ans[i], (*vals.begin()) + 1);
        vals.insert(ans[i]);
    }
    repb(i, 50 * 10000, -1)
        if (ans[i] != inf)
        {
            cout << i << " " << ans[i] << "\n";
            break;
        }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second

typedef pair <int, int> pii;

const int maxn = 66;
const int MAX = 5e5 + 21;

int n, d, a[maxn], cnt[MAX], dp[MAX][2];

deque <pii> Q;

int main()
{
    ios::sync_with_stdio(0);    cin.tie(0);
    cin >> n >> d;

    for(int i = 0; i < n; i++)
        cin >> a[i];

    sort(a, a + n);

    dp[0][0] = 1;
    for(int i = 0; i < n; i++)
    {
        for(int j = a[i]; j < MAX; j++)
            if( dp[ j - a[i] ][0] )
                dp[j][1] = 1;

        for(int j = 0; j < MAX; j++)
            dp[j][0] |= dp[j][1];
    }

   // for(int j = 0; j < MAX; j++)
     //   if( dp[j][0] )
       // cerr << j << " " << dp[j][0] << "\n";

    Q.push_back( {0, 0} );
    for(int i = 1; i < MAX; i++)
        if( dp[i][0] )
        {
            dp[i][0] = 0;

            while( Q.size() && Q.front().Y < i - d )
                Q.pop_front();

            if( Q.size() )
            {
                dp[i][0] = 1;
                cnt[i] = Q.front().X + 1;

                while( Q.size() && Q.back().X > cnt[i] )
                    Q.pop_back();

                Q.push_back( {cnt[i], i} );
            }

        }

    for(int i = MAX - 1; i >= 0; i--)
        if( dp[i][0] )
        {
            cout << i << " " << cnt[i] << "\n";
            return 0;
        }
}

/*#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
using namespace std;
#define INF 99999999
#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b ? a : b)
const int nMax = 1e5;
int a[nMax];
int mark[11];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++)
        scanf("%d",&a[i]);
    int ans = 0;
    for(int i = 0;i < n;i++){
        memset(mark,0,sizeof(mark));
        while(a[i]){
            int temp = a[i]%10;
            a[i]/=10;
            mark[temp]++;
        }
        int countnum = 0;
        for(int j = 0;j <= k;j++)
            if( mark[j] != 0 )
                countnum++;
        if( countnum == k+1 )
            ans++;
    }
    cout<<ans<<endl;
}
*//*
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const nMax = 1e5+100;
int a[nMax];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;i++)
        scanf("%d",&a[i]);
    if( n <= 2 ){
        cout<<n<<endl;
        return 0;
    }
    int ans = 2;
    int countnum = 2;
    for(int i = 2;i < n;i++){
        if( a[i] == a[i-1]+a[i-2] ){
            countnum++;
        }
        else{
            ans = max(countnum,ans);
            countnum = 2;
        }
    }
    ans = max(countnum,ans);
    cout<<ans<<endl;
}
*//*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const int nMax = 50000;
const int INF = 1e9+1;
string ss;
long long ans,x,n,a[nMax],s[nMax],h[nMax];
int main()
{
    cin>>x>>ss;
    n = ss.length();
    for(int i = 0;i < n;i++){
        a[i+1] = ss[i]-'0';
        s[i+1] = s[i] + a[i+1];
    }
    for(int i = 1;i <= n;i++)
        for(int j = 0;j < i;j++)
            h[s[i]-s[j]]++;
    if( x==0 )
        for(int i = 0;i <= s[n];i++)
            ans+=h[0]*h[i];
    for(int i = 1;i <= s[n];i++)
        if( (x%i == 0) && (x/i <= s[n]) )
        ans+=h[i]*h[x/i];
    cout<< ans <<endl;
    return 0;
}*/

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[500010];
int c[100];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,d;
    scanf("%d%d",&n,&d);
    memset(dp,0,sizeof(dp));
    for(int i = 1;i <= n;i++)
        scanf("%d",&c[i]);
    dp[0] = 1;
    for(int i = 1;i <= n;i++){
        for(int j = 500000-c[i];j >= 0;j--){
            if( dp[j] )
                dp[j+c[i]] = 1;
        }
    }
    int mas = 0;
    int day = 0;
    while(1){
        int x = mas+d;
        if( x >= 500000 )
            x = 500000;
        while( x > mas && !dp[x] ) x--;
        if( x == mas ){
            printf("%d %d\n",mas,day);
            break;
        }
        mas = x;
        day++;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(){

	int n, d;
	cin >> n >> d;

	set<int> s = {0};
	for(int i = 0; i < n; i++){
		int c;
		cin >> c;
		vector<int> a;
		for(auto it = s.begin(); it != s.end(); ++it)
			a.push_back(*it+c);
		for(size_t j = 0; j < a.size(); j++)
			s.insert(a[j]);
	}

	int sum = 0, time = 0;
	while(true){
		auto it = s.upper_bound(sum + d);
		--it;
		if(*it == sum) break;
		sum = *it;
		time++;
	}

	cout << sum << " " << time;

	return 0;
}

#include<bits/stdc++.h>
#define forn(I,N) for(int I=0;I<(N);I++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
typedef long long ll;
using namespace std;
int knap[51][500001];
int ans1[500001];
int main()
{
    int n,d,x;
    cin>>n>>d;
    vector<int>a;
    forn(i,n)
    {
        cin>>x;
        a.pb(x);
    }
    sort(a.begin(),a.end());
    vector<int>nums;
    int sum=0;
    forn(i,n)
    {
        if(sum+d>=a[i])
        {
            sum+=a[i];
            nums.pb(a[i]);
        }
        else break;
    }
    cout<<sum<<' ';
    for(int i=0;i<nums.size();i++)
    {
        if(i==0){knap[i][nums[i]]=1;continue;}
        for(int j=1;j<=sum;j++)
        {
            if(j==nums[i])knap[i][j]=1;
            knap[i][j]=knap[i][j]|knap[i-1][j];
            if(j>nums[i])knap[i][j]=knap[i][j]|knap[i-1][j-nums[i]];
        }
    }
    int h=nums.size()-1;
    deque<int>t;
    for(int i=1;i<=sum;i++)
    {
        if(i<=d && knap[h][i]==1)
        {
            ans1[i]=1;
            t.push_back(1);
        }
        else
        {
            if(knap[h][i]==1)
            {
                ans1[i]=t.front()+1;
                int g=t.front()+1;
                while(t.back()>g)t.pop_back();
                t.push_back(g);
            }
                if(i>d && ans1[i-d]>0 && ans1[i-d]==t.front())
                {
                    t.pop_front();
                }
        }
        //cout<<ans1[i]<<';'<<i<<endl;
    }
    cout<<ans1[sum];
    return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 5e5;

int main() {
    int n, D; cin >> n >> D;
    vector<int> c(n);
    for (auto &ci: c) cin >> ci;
    vector<int> dp(MAX + 1);
    dp[0] = true;
    for (auto ci: c) {
        for (int j = MAX; j >= ci; j--) dp[j] |= dp[j - ci];
    }
    int cur = 0, cnt = 0;
    bool update = true;
    while (update) {
        update = false;
        for (int i = min(MAX, cur + D); i > cur; i--) if (dp[i]) {
            cur = i, cnt += update = true;
            break;
        }
    }
    cout << cur << " " << cnt << endl;
    return 0;
}

#include <cstdio>
#include <vector>
#include <iostream>
typedef unsigned int uint;

main()
{
	uint N,D;
	scanf("%u%u",&N,&D);
	uint c[50];
	for(uint i=0; i<N; ++i)
		scanf("%u",c+i);
	std::vector<bool> A(510001,0);
	A[0]=1;
	for(uint n=0; n<N; ++n)
	{
		std::vector<bool> B(A);
		for(uint i=0; i+c[n]<B.size(); ++i)
			if(A[i])
				B[i+c[n]]=1;
		std::swap(A,B);
	}
	uint s=0,n=0;
	while(1)
	{
		uint prev=s;
		for(uint i=0; i<=D; ++i)
			if(A[prev+i])
				s=prev+i;
		if(s==prev)
			break;
		++n;
	}
	printf("%u %u\n",s,n);
}

#include<cstdio>
#include<iostream>
using namespace std;
int n,k,cost,nowcost,tmp;
bool dp[600005],flag;
int main(){
	scanf("%d%d",&n,&k);
	dp[0]=true;
	for(register int i=1;i<=n;i++){
		scanf("%d",&cost);
		for(register int j=600000;j>=cost;j--){
			dp[j]=(dp[j]||dp[j-cost]);
		}
	}
	for(register int i=0;i<=600000;i++){
		tmp=nowcost+k;
		for(register int j=tmp;j>=nowcost;j--){
			if(dp[j]==true){
				nowcost=j;
				break;
			}
		}
		if(tmp==nowcost+k){
			printf("%d %d\n",nowcost,i);
			return 0;
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

int n,d,x,f[6000005],p,ans,s;

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>d;
	f[0]=1;
	for(int i=1;i<=n;++i){
		cin>>x;
		s+=x;
		for(int j=s;j>=x;--j)f[j]|=f[j-x];
	}
	while(1){
		int i;
		for(i=d;i>0;--i)if(f[p+i]){
			p+=i;
			break;
		}
		if(i==0)break;
		ans++;
	}
	cout<<p<<" "<<ans<<"\n";
	return 0;
}

//
// Created by Allen Li on 6/8/18.
//

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

#define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define long ll
#define finish(x) return cout << x << '\n', 0
#define F0R(i, a) for(int i=0;i<a;i++)
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define F0Rd(i, a) for(int i=a;i>=0;i--)
#define FORd(i, a, b) for(int i=a;i>=b;i--)
#define all(x) x.begin(), x.end()

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
const int INF = 1061109567;
const int MAXN = 510005;

int dp[MAXN];

int main() {
    FAST_IO;
    int n, d; cin >> n >> d;
    memset(dp, 0, sizeof dp);
    dp[0] = 1;
    int items[n];
    F0R(i, n)
        cin >> items[i];
    F0R(j, n) {
        F0Rd(i, MAXN - items[j])
            if(dp[i])
                dp[i + items[j]] = 1;
    }
    int itCount = 0;
    int i = 0;
    while(i < 500000) {
        bool flag = false;
        FORd(j, i + d, i + 1) {
            if(dp[j]) {
                itCount++;
                i=j;
                flag = true;
                break;
            }
        }
        if(!flag)
            break;
    }
    cout << i << " " << itCount << endl;
}

#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
//#define fill(x,y) memset(x,y,sizeof(x))

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

#define filename ""

const double pi = 2 * asin (1.0);
const int INF = 1e9;
const int mod = 1e9 + 9;

int m;
int n;
int d;

int a[500010];

int main()
{
//  freopen (filename".in", "r", stdin);
//  freopen (filename".out", "w", stdout);
    scanf ("%d %d", &n, &d);
    a[0] = 1;
    for (int i = 0; i < n; i++) {
        int x;
        scanf ("%d", &x);
        for (int j = m; j >= 0; j--)
            if (a[j] == 1) a[x + j] = 1;
        m += x;
    }
    int s = 0, f = -1, t = 0;
    for (int i = 1; i <= m; i++) {
        if (a[i] == 0) continue;
        if (s + d >= i) { f = i; }
        else if (f != -1) { s = f; t ++; f = -1; i--; }
    }
    if (f != -1) { s = f; t ++; f = -1; }
    cout << s << " " << t << endl;
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<iostream>
#include<set>
#include<string>
using namespace std ;
#define M 500010

int a[100], vis[100], sum[100];
int dp[M], f[1000];
int lft[M];

int main()
{
	int n, d;
	cin>>n>>d;
	for( int i = 0; i < n; ++i ) scanf( "%d", a+i );
	int j = 0, m = 0;
	sort( a, a+n );
	while( j < n && a[j] <= d ) vis[j++] = 1;
	sum[0] = a[0];
	for( int i = 1; i < n; ++i ) sum[i] = a[i] + sum[i-1];

	dp[0] = 1;
	int ans = 0, cc = 0;
	for( int i = 0; i < n; ++i ) if( vis[i] ){
		ans += a[i];
		for( int k = m; k >= 0; --k ) if( dp[k] ){
			dp[k+a[i]] |= dp[k];
			while( j < n && k+a[i] + d >= a[j] ) vis[j++] = 1;
		}
		m += a[i];
	}
	for( int i = 1; i <= m; ++i ){
		lft[i] = lft[i-1];
		if( dp[i] ) lft[i] = i;
	}
	cc = 0;
	int i = 0;
	while( i < ans ){
		i = lft[i];
		++cc;
		i += d;
	}
	cout<<ans<<" "<<cc<<endl;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1e9+10;
const ll MOD=1e9+7;

int n,d;
int a[maxn];
int dp[maxn];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	while(cin>>n>>d){
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		int sum=0;
		for(int i=1;i<=n;i++){
			sum+=a[i];
			for(int j=sum;j>=0;j--){
				if(j>=a[i]&&dp[j-a[i]]) dp[j]=1;
			}
		}
		int cnt=0;
		int now=0;
		while(1){
			int x=now;
			for(int i=now+d;i>=now+1;i--){
				if(dp[i]){
					x=i;break;
				}
			}
			if(x==now) break;
			now=x;cnt++;
		}
		cout<<now<<" "<<cnt<<endl;
	}
	return 0;
}

#include <cstdio>
int n,d,x,max,i,j,ans;
bool f[5100000];
int main() {
    scanf("%d%d",&n,&d);
    f[0]=true;
    for (i=1;i<=n;i++) {
        scanf("%d",&x);
        for (j=(max+=x);j>=x;j--)
            if (f[j-x]) f[j]=true;
    }
    x=0;
    while (1) {
        j=ans+d;
        while (!f[j]&&(j>ans)) j--;
        if (j==ans) break;
        ans=j;x++;
    }
    printf("%d %d\n",ans,x);
    return 0;
}

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define CLEAR(a, val) memset(a, val, sizeof(a))
#define UP(i, s, t) for (int i = (s); i < (t); i += 1)
#define DOWN(i, s, t) for (int i = (s); i > (t); i -= 1)

typedef long long ll;
using namespace std;
const int MAXINT = 1 << 31 - 1;
const int MAXN = 55 * 1e4 + 5;

int dp[MAXN];
int arr[60];
int n, d;

int main () {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	while (cin >> n >> d) {
		int big = 0;
		UP(i, 0, n) {
			scanf("%d", &arr[i]);
			big += arr[i];
		}

		fill(dp, dp + big + d + 1, MAXINT);
		dp[0] = 0;
		UP(i, 0, n) {
			DOWN(j, big, 0) {
				int prev = j - arr[i];
				if (prev < 0) {
					break;
				}
				if (dp[prev] == MAXINT) {
					continue;
				}
				dp[j] = min(dp[j], dp[prev] + 1);
			}
		}

		int times = 0;
		int cur = 0, next = 0;
		while (true) {
			next = cur;
			UP(i, 1, d + 1) {
				if (dp[cur + i] == MAXINT) {
					continue;
				}
				next = cur + i;
			}
			if (next == cur) {
				break;
			}
			cur = next;
			times ++;
		}

		printf("%d %d\n", next, times);
	}
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rint register int
#define io_f std::ios::sync_with_stdio(false)
#define ci const int
using namespace std;

int n,d;
int maxi;
bool book[5000001];

int main(){
	io_f;
	int a;
	cin>>n>>d;
	book[0]=1;
	for(rint i=1;i<=n;i++){
		cin>>a;
		for(rint j=5000000;j>=a;j--)
		    book[j]|=book[j-a];
	}
	int ans=0,tmp,x=0;
	bool st=0;
	while(1){
		st=0;
		for(tmp=x+d;tmp>x;tmp--){
			if(book[tmp]){
				st=1;
				ans++;
				x=tmp;
				break;
			}
		}
		if(!st)break;
	}
	cout<<tmp<<' '<<ans;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
const int MAXN = 700010;
int N,D;
int dp[MAXN];
int main()
{
    while (scanf("%d%d",&N,&D) != EOF)
    {
        memset(dp,0,sizeof(dp));
        dp[0] = 1;
        int sum = 0;
        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 day = 0,ret = 0;
        while (true)
        {
            ret += D;
            int pos = D;
            for (int i = 0 ; i < D ; i++) if (dp[ret - i])
            {
                pos = i;
                break;
            }
            if (pos == D)
            {
                ret -= D;
                break;
            }
            day++;
            ret -= pos;
        }
        printf("%d %d\n",ret,day);
    }
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,sum,i,j,now,ans,f[1000001],a[101];
int main(){
	scanf("%d%d",&n,&m);
	f[0]=1;
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum+=a[i];
		for(j=sum;j>=a[i];j--)f[j]|=f[j-a[i]];
	}
	while(1){
		for(i=now+m;i>=now+1;i--)if(f[i])break;
		if(i==now)break;
		now=i;ans++;
	}
	printf("%d %d",now,ans);
}

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)

const int MAX = 1e6;

using namespace std;

int main()
{
  int N,K, soma=0;
  bool dp[MAX];
  cin>>N>>K;
  dp[0] = 1;
  FOR(i,0,N)
  {
    int a;
    cin>>a;
    soma+=a;
    for(int j = soma;j>=a;j--)
      if(dp[j-a]) dp[j]=1;
  }
  int dia=0, now=0;
  while(true)
  {
    int i;
    now+=K;
    for(i = 0;i<K;i++)
      if(dp[now-i])
        break;
    if (i==K)
      break;
    dia++;
    now -=i;
  }
  cout<<now-K<<" "<<dia<<endl;

}

#include<map>
#include<cmath>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 6e5+9;
bool dp[MAXN];
vector<int>v;
vector<int>::iterator cur, mx;
int main() {
    int n, c, d, i, j, sum;
    while(~scanf("%d%d", &n, &d)) {
        memset(dp, 0, sizeof(dp));
        sum = 0;
        dp[0]=1;
        for(i=0; i<n; ++i) {
            scanf("%d", &c);
            for(j=sum; j>=0; --j) {
                dp[j+c] |= dp[j];
            }
            sum += c;
        }
        for(i=0; i<MAXN; ++i) {
            if(dp[i]) v.push_back(i);
        }
        int day=0;
        mx = v.begin();
        while(true) {
            cur = upper_bound(v.begin(), v.end(), (*mx)+d);
            --cur;
            if(mx==cur) break;
            mx=cur;
            ++day;
        }
        printf("%d %d\n", *mx, day);
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

int main()
{
	int n, d;
	cin>>n>>d;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin>>a[i];

	int su = accumulate(a.begin(), a.end(), 0);
	sort(a.begin(), a.end());
	vector<int> s(su+1, 0);
	s[0] = 1;
	for (int i = 0, S = 0; i < n; ++i )
	{
		int t = a[i];
		S += a[i];
		for (int j = S; j >= t; --j)
			s[j] |= s[j-t];
	}

	vector<int> maxLess(su+1, 0);
	int ml = 0;
	for (int i = 1; i <= su; ++i)
	{
		if (s[i])
			ml = i;
		maxLess[i] = ml;
	}

	int M = 0, moves = 0;
	while (M < su)
	{
		int t = M + d;
		if (t >= su)
		{
			M = su;
			++moves;
		}
		else
		{
			if (maxLess[t] == M)
				break;
			M = maxLess[t];
			++moves;
		}
	}

	cout<<M<<" "<<moves;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 10010;
int n, d, a[55]; bool dp[55][55 * MAXN];

int main()
{
    cin >> n >> d;
    for(int i=0; i<n; i++)  cin >> a[i+1];
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i=1; i<=n; i++)
        for(int j = 0; j <= MAXN * 50; j++)
            if(dp[i-1][j] || (j >= a[i] && dp[i-1][j-a[i]]))
                dp[i][j] = 1;
    int now = 0, nxt = -1, days = 0, flag = 1;
    while(flag) {
        flag = 0;
        for(int i = now+1; i <= now + d; i++)
            if(dp[n][i])
                nxt = i, flag = 1;
        if(flag)
            now = nxt, days++;
    }
    cout << now << " " << days << "\n";
}

//============================================================================
// Author      : TAPAN SAHNI
//============================================================================
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#include<deque>
#include<map>
#include<set>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<functional>
using namespace std;
#define LL long long
#define si(x) scanf("%d",&x)
#define sc(x) scanf("%c",&x)
#define sl(x) scanf("%I64d",&x)
#define ss(x) scanf("%s",x)
#define all(x) x.begin(),x.end()
#define compress(x) {sort(all(x));(x).resize(unique(all(x))-(x).begin());}
template<class T> inline void umax(T &a,T b){if(a<b) a = b ;}
typedef pair<int, int> ii;
typedef pair<LL, LL> PII;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
#define vl vector<>
#define vi vector<int>
#define vii vector<ii>
#define vvl vector< vl >
#define vvi vector< vi >
#define vvii vector< vii >
#define sz size()
#define pb push_back
#define F first
#define S second
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,a,b) for(int  i=(a);i<(b);i++)
#define repl(i,a,b) for(LL i=(a);i<(b);i++)
#define repv(i,b,a) for(int i=(b);i>=(a);i--)
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PDD pair<LL, LL>
#define LLD long double

const int maxn =  50 + 10;
const int INF  = 1e9 + 7;
//const LL INF = 0x0123456789ABCDEFLL;
int n,d,ct[maxn],a[maxn];
int dp[60][5*200001];
inline void solve(void){
       cin>>n>>d;
       rep(i,0,n)cin>>ct[i];
       int Max = 0, s=d,k=0;
       sort(ct,ct+n);
       rep(i,0,n){
        if(ct[i] <= s){
            Max+=ct[i];
            a[k] = ct[i];
            k++;
        }
        s+=a[i];
       }
       if(Max==0){
        cout<<"0 0";
        return;
       }
       n=k;
       dp[0][0]=1;
       for(int i=0;i<=n-1;i++){
        for(int cst=0;cst<=5*100000;cst++){
            if(dp[i][cst]==0) continue;
            dp[i+1][cst+a[i]]=1;
            dp[i+1][cst]=1;
        }
       }
       int maxi=0,cnt=0;
       while(1){
            if(maxi==Max){
                cout<<Max<<" "<<cnt<<endl;
                return;
            }

            for(int i=maxi+d;i>maxi;i--){
                if(dp[n][i]==1){
                    maxi=i;
                    break;
                }

            }
            cnt++;
       }

       return;

}
void init() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}
int main(int argc, const char * argv[]){
    // freopen("game.in","r",stdin);
    //freopen("game.out","w",stdout);
    init();
    solve();
    return 0;
}

#include<stdio.h>
int bag[550000];
int main()
{
	int i,j,n,d,c;
    int sum,value,days;
	while(~scanf("%d%d",&n,&d))
	{
		sum=value=days=0;
		bag[0]=1;
		for(i=0;i<n;i++)
		{
			scanf("%d",&c);
			for(j=(sum+=c);j>=c;j--)
				bag[j]|=bag[j-c];
		}
		while(true)
		{
			for(j=value+d;j>value;j--)
				if(j<=sum&&bag[j]!=0) break;
				if(j==value) break;
				value=j;
				days++;
		}
		printf("%d %d\n",value,days);
	}
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
bool dp[500050];
int trace[500050];
int n,d;
int a[55];

int main()
{
    //freopen("FreeMarket.INP","r",stdin);
    //freopen("FreeMarket.OUT","w",stdout);
    scanf("%ld%ld",&n,&d);
    for (int i=1; i<=n; i++)
        scanf("%ld",&a[i]);
    memset(dp,false,sizeof(dp));

    //day con co tong = S
    dp[0]=true;
    for (int i=1; i<=n; i++)
    for (int j=500000; j>=0; j--)
    if ((dp[j]==true) && (j+a[i]<=500000))
        dp[j+a[i]]=true;

     // tinh san ham trace
    int pos;
    for (int i=0; i<=500000; i++)
    {
        if (dp[i]==true) pos=i;
        trace[i]=pos;
    }
    // tim so ngay + kq = "GREEDY"
    int now=0, day=0;
    while (now <=500000)
    {
        if (trace[min(now+d,500000)]==now) break;
        now=trace[min(now+d,500000)];
        day++;
    }
    printf("%ld %ld",now,day);
    return 0;
}

/*Author - Dhananjay Singh (@Dhananjay_049)*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;
#define forn(i,n) for(int i=0;i<n;i++)
#define rforn(i,n) for(int i=n-1;i>=0;i--)
#define forne(i,n) for(int i=1;i<=n;i++)
#define forse(i,s,e) for(int i=s;i<e;i++)
#define rforse(i,s,e) for(int i=e-1;i>=s;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define fill(a,x) memset(a,x,sizeof a);
#define trav(a,x) for(auto &a:x)
#define INF	ll(1e18)
#ifdef DHANANJAY_049
#include "../code-pieces/trace.h"
#else
#define trace(args...)
#endif
//ordered_set T; T.insert(val);*T.find_by_order(index);T.order_of_key(val);
//find_by_order(k): It returns to an iterator to the kth element (counting from zero)
//order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
//Thus we can now solve the above problem easily i.e. count of elements between l and r can be found by:
//o_set.order_of_key(r+1) – o_set.order_of_key(l)
ll modpow(ll a, ll b, ll m = MOD) {
	a %= m; ll res = 1; while (b) {if (b & 1)res = res * a % m; a = a * a % m; b >>= 1;}
	return res;
}
ll bpow(ll a, ll b) {ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll modinv(ll a) {return modpow(a, MOD - 2, MOD);}
void graph(vvi &adj, int m) {int x, y; forn(i, m) {cin >> x >> y; adj[x - 1].pb(y - 1); adj[y - 1].pb(x - 1);}}
 struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
const int mxN = 2e5 + 5;

ll subarrays(int arr[],ll x,int n){
    unordered_map<ll,int> mp;
    ll sum_so_far = 0;
    mp[0]=1;
    ll count1 = 0;
    forn(i,n){
        sum_so_far+=arr[i];
        if(mp.find(sum_so_far-x)!=mp.end()){
            count1+=mp[sum_so_far-x];
        }
        mp[sum_so_far]++;
    }
    return count1;
}
int dp[510005];
void solve(){
  dp[0]=1;
  int n,d;
  cin>>n>>d;
  forn(i,n){
    int c;
    cin>>c;
    rforse(j,c,500001){
        dp[j]|=dp[j-c];
    }
  }
  int x=0;
  int days=0;
  while(true){
    bool poss=false;
    rforse(i,x+1,x+d+1){
        if(dp[i]){
            x=i;
            days++;
            poss=true;
            break;
        }
    }
    if(!poss) break;
  }
  cout<<x<<' '<<days<<'\n';
}
int main() {
	ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
#ifdef DHANANJAY_049
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
#endif
	int t = 1;
	//cin >> t;
	while (t--) {
        solve();
	}
}

#include <cstdio>
#include <cstring>
#include <algorithm>

const int maxn = 10000 + 10 ;
bool used[ 50 * 10000 + 1000 ];
int data[ maxn ],n,d ;
int main (){
    while( 2 == scanf ("%d%d",&n,&d) ){
        for(int i=1; i <= n ; ++i)
            scanf ( "%d" , data + i);
        memset( used, 0 , sizeof( used ) ) ;
        used[ 0 ]= true ;

        for(int i=1; i <= n ; ++i){
            for(int j=50*maxn-1; j>=0 ; --j){
                if(!used[j]) continue ;
                used[ j + data[ i ] ] = true;
            }
        }

        int ans = 0 ,cnt = 0 ;
        while( true ){
            int to = std::min( 50 * maxn - 1 , ans + d ) ;
            while( !used[to] ) --to;
            if(ans == to ) break ;
            ++cnt;
            ans = to;
        }
        printf ("%d %d\n", ans, cnt ) ;
     }
     return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 51;
const int M = 10005;

bool dp[N * M];
int c[N],d,n;

int main(){
    // freopen("input.txt","r",stdin);
    scanf("%d%d",&n,&d);
    for(int i = 0;i < n;i ++)
        scanf("%d",&c[i]);
    int sum = 0;
    dp[0] = true;
    for(int i = 0;i < n;i ++){
        sum += c[i];
        for(int j = sum;j >= c[i];j --)
            dp[j] |= dp[j - c[i]];
    }
    int maxVal,days;
    maxVal = days = 0;
    while(true){
        bool found = false;
        for(int i = d;i >= 1 && !found;i --)
            if(dp[maxVal + i]){
                maxVal += i;
                days ++;
                found = true;
            }
        if(!found) break;
    }
    printf("%d %d\n",maxVal,days);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#include <complex>
using namespace std;
#define pb push_back
#define mp make_pair
#define point complex <double>
#define xx real()
#define yy imag()
typedef vector<int> vi;

const int MAXN = 550005, INF = 1e9;

int dp[MAXN], v[55];

void solve(){
    int n, i, j, d, ind, ind1;
    scanf("%d%d", &n, &d);
    for(i = 0; i < n; i++){
        scanf("%d", &v[i]);
    }
    for(i = 0; i < MAXN; i++){
        dp[i] = dp[i] = INF;
    }
    dp[0] = 0;
    for(i = 0; i < n; i++){
        for(j = MAXN - 1; j >= 0; j--){
            if (dp[j] == INF){
                continue;
            }
            if (j + v[i] < MAXN){
                dp[j + v[i]] = 0;
            }
        }
    }
    int ans, ans_cnt, tmp;
    ind = 0;
    while(1){
        ind1 = 0;
        for(i = ind + 1; i <= min(ind + d, MAXN - 1); i++){
            if (dp[i] < INF){
                dp[i] = dp[ind] + 1;
                ind1 = i;
            }
        }
        if (ind1 == 0){
            break;
        }
        ind = ind1;
    }
    ans = ind;
    ans_cnt = dp[ind];
    printf("%d %d", ans, ans_cnt);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    t = 1;
    while(t){
        t--;
        solve();
    }
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <utility>
#include <set>
#include <stack>
#include <deque>
#include <ctime>
#include <random>
#include <cassert>
#include <cmath>
#include <climits>
#include <queue>
#include <cstring>
#include <bitset>
#include <iomanip>
#include <chrono>

/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#pragma GCC target("avx2")
*/

#ifdef LOCAL
	#define dbg(x) cout << #x << " : " << x << endl;
#else
	#define dbg(x)
#endif

#define int long long
#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a,b) for (int i=a;i<b;i++)
#define fj(a,b) for (int j=a;j<b;j++)
#define fk(a,b) for (int k=a;k<b;k++)
#define fi1(a,b) for (int i=a-1;i>=b;i--)
#define fj1(a,b) for (int j=a-1;j>=b;j--)
#define fk1(a,b) for (int k=a-1;k>=b;k--)
#define fx(x,a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

template<typename T1, typename T2>inline void mine(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, T2 y) { if (x < y) x = y; }

ostream& operator << (ostream &a, const vector<int> &b) {
    for (auto k : b) cout << k << ' ';
    return a;
}

typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;

const int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 500010;
const double EPS = 1e-9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

   	int n, d;
   	cin >> n >> d;
   	bitset<MX> can;
   	can[0] = 1;
   	fi(0, n) {
   		int x;
   		cin >> x;
   		can |= can << x;
   	}
   	int day = 0;
   	int mx = 0;
   	fi(1, MX) {
   		bool done = false;
   		fj1(mx + d + 1, mx + 1) {
   			if (can[j]) {
   				mx = j;
   				day++;
   				done = true;
   				break;
   			}
   		}
   		if (!done)
   			break;
   	}
   	cout << mx << ' ' << day << '\n';
}

#include<bits/stdc++.h>

using namespace std;

const int maxn = 500010;

int can[maxn], cat[maxn], num;

int a[55];

int main() {

	int n, d;

	cin >> n >> d;

	for( int i = 0; i < n; i ++ ) scanf("%d", a+i);

	can[0] = 1;

	for( int i = 0; i < n; i ++ ) {

		for( int itr = maxn - a[i] - 1; itr >= 0; itr-- ) {

			if( can[itr] ) can[itr+a[i]] = 1;

		}

	}

	for( int i = 0; i < maxn; i ++ ) if( can[i] ) cat[num++] = i;

	int mn = 0, rid = 0;

	while( 1 ) {

		int pos = lower_bound( cat, cat + num, mn + d + 1 ) - cat;

		int temp = cat[pos-1];

		if( temp == mn ) break;

		rid++;

		mn = temp;

	}

	cout << mn << ' ' << rid << endl;

	return 0;

}

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool vis[1000010];
int b[1000010],a[55];
int main()
{
    int i,j,n,d;
    scanf("%d%d",&n,&d);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+n+1);
    vis[0]=true;
    int pre=0;
    for(i=1;i<=n;i++)
    {
        bool flag=false;
        for(j=pre;j>=0;j--)
            if(vis[j]&&a[i]-j<=d)
            {
            flag=true;
            break;
        }
        if(!flag) break;
        for(j=pre;j>=0;j--)
            if(vis[j])
            vis[j+a[i]]=true;
        pre+=a[i];
    }
    int tot=0,days=0;
    for(i=0;i<=pre;i++)
        if(vis[i])
        b[tot++]=i;
    if(tot==1)
        return puts("0 0"),0;
    pre=0;
    for(i=0;i<tot;i++)
    {
        if(pre+d<b[i])
        {
            days++;
            pre=b[i-1];
        }
    }
    days++;
    printf("%d %d\n", b[tot-1], days);
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
#include<map>
#include<string>
#include<vector>
#include<set>

using namespace std;
#define For(i,l,r) for (int i = l; i <= r; ++i)
#define Cor(i,l,r) for (int i = l; i >= r; --i)
#define Fill(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define MP make_pair
#define PII pair<int,int>
#define flt double
#define INF (0x3f3f3f3f)
#define MaxN 1020304
#define MaxNode 1020304
#define MD 1000000007

int n,d,s[MaxN],a[MaxN],f[MaxN],dis[MaxN];
int main() {
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    cin >> n >> d;
    int Sum = 0;
    For(i,1,n) scanf("%d",&a[i]), Sum += a[i];
    Fill(f,0); f[0] = 1;
    For(i,1,n) Cor(j,Sum,a[i]) f[j] |= f[j - a[i]];
    int last = 0;
    Fill(dis,-1); dis[0] = 0;
    For(i,1,Sum) if (f[i]) {
        int newLast = -1;
        For(j,last,i - 1) if (f[j] && i - j <= d) {
            newLast = j;
            break ;
        }
        last = newLast;
        if (last != -1) dis[i] = dis[last] + 1;
        if (last == -1) {
            break ;
        }
    }
    Cor(i,Sum,0) if (dis[i] != -1) {
        cout << i << ' ' << dis[i] << endl;
        break ;
    }
    /*For(i,0,Sum + 100) f[i] = INF; f[0] = 0;
    memcpy(g,f,sizeof(f));
    sort(a + 1,a + n + 1);
    int ans1 = 0, ans2 = 0;
    For(i,1,n) {
        if (getProf(a[i]) == INF) break;
        int days = getProf(a[i]) + 1;
        ans1 += a[i]; ans2 += days;
        Cor(j,Sum,a[i]) f[j] = min(f[j],f[j - a[i]] + days);
        Cor(j,Sum - 1,0) g[j] = min(g[j + 1],f[j]);
    }
    cout << ans1 << ' ' << ans2 << endl;*/

    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<queue>
#include<map>
#include<vector>
using namespace std;

int can[500010];
int dp[500010];

int cost[110];

int c[500010];

void upd(int n,int k)
{
    n=500000-n+1;

    while(n<=500000)
	c[n]=min(c[n],k),n+=n&(-n);
}
int get(int n)
{
    n=500000-n+1;

    int ans=2100000000;

    while(n)
	ans=min(ans,c[n]),n-=n&(-n);
    return ans;
}
int main()
{
    int n,d,i,j,k;

    cin>>n;

    cin>>d;

    for(i=1;i<=n;i++)
	cin>>cost[i];

    can[0]=1;

    for(i=1;i<=n;i++)
    {
	for(j=i*10000-10000;j>=0;j--)if(can[j])
	    can[j+cost[i]]=1;
    }

    for(i=0;i<=500000;i++)
	c[i]=2100000000;

    upd(0,0);

    for(i=1;i<=500000;i++)if(can[i])
    {
	j=i-d;
	j=max(j,0);
	dp[i]=get(j)+1;
	upd(i,dp[i]);
    }
    else
	dp[i]=210000000;
    int ans=0,num=0;

    for(i=500000;i>=1;i--)if(dp[i]<1000000)
    {
	ans=i;
	num=dp[i];
	break;
    }
    cout<<ans<<" "<<num<<endl;
}

#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
using ll = long long;
using ld = long double;

const int mod = 1e9 + 7;
const int inf = INT_MAX;
const int N = 50 + 5;

int n, d, a[N];
bitset<10000*N> dp;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> d;
    for (int i = 1; i <= n; i++) cin >> a[i];

    dp[0] = true;
    for (int i = 1; i <= n; i++)
        dp |= dp << a[i];

    int x = 0, ans = 0;
    while (true) {
        int j = ans + d;
        while (!dp[j] and j > ans) j--;
        if (j == ans)
            break;
        ans = j;
        x++;
    }

    cout << ans << " " << x << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int a[60];
long long dp[50];

int csum(int l,int r)
{
    int ans=0;
    for (int i=l;i<=r;i++)
        ans+=a[i];
    return ans;
}

void ck(long long &x,long long y)
{
    if (x==-1||x>y) x=y;
}

int main()
{
    int n,d;
    scanf("%d%d",&n,&d);
    for (int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    int sum=0,id=-1;
    for (int i=0;i<n;i++)
    {
        if (sum+d<a[i]) break;
        sum+=a[i];
        id=i;
    }
    if (id==-1) puts("0 0");
    else
    {
        memset(dp,-1,sizeof(dp));
        dp[0]=1;
        for (int i=1;i<=id;i++)
        {
            for (int j=0;j<=i;j++)
            {
                if (csum(j,i)<=d)
                {
                    if (j==0) ck(dp[i],1);
                    else ck(dp[i],dp[j-1]+1);
                }
                else
                {
                    for (int k=0;k<j;k++)
                    {
                        if (csum(0,k)+d>=csum(j,i))
                        {
                            ck(dp[i],dp[j-1]+dp[k]+1);
                        }
                    }
                }
            }
        }
        printf("%d %I64d\n",sum,dp[id]);
    }
    return 0;
}

#include <iostream>
#include <set>

using namespace std;

int N, D;
bool poss[500050];

int main()
{
    for(int i=0;i<500050;i++) poss[i] = false;
    cin >> N >> D;
    poss[0] = true;
    for(int k=0;k<N;k++)
    {
        int c;
        cin >> c;
        for(int i=500049; i>=0; i--) poss[i] |= poss[i-c];
    }
    //for(int i=0;i<20;i++) cout << poss[i];  cout << endl;
    int p = 0, d = 0;
    while(1)
    {
        int latest = -1;
        for(int i = p+1; i <= p+D; i++)
        {
            if(poss[i]) latest = i;
        }
        if(latest == -1) break;
        p = latest;
        d++;
    }
    cout << p << ' ' << d << endl;
    return 0;
}

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int maxix = 60;
const int maxnum = 1000*1000*1000+10;
const int maxd =50*10000 + 10000;
int dp[maxd], mark[maxd];
int arr[maxix];
int main()
{
    int n, d;
    cin >> n >> d;
    for (int i=1 ; i<=n ; i++)
    {
        cin >> arr[i];
        mark[arr[i]] = 1;
    }
    sort(arr+1,arr+n+1);
    int sum = 0;
    int all = 0;
    dp[0] = 1;
    for (int i=1 ; i<=n ; i++)
    {
        if (sum+d >= arr[i])
        {
            sum += arr[i];
        }
        for (int j=0 ; j<=all ; j++)
        {
            if (dp[j] == 1)
                dp[j+arr[i]] = 1;
        }
        all += arr[i];
    }
    cout << sum << " ";
    //"until here we cout max number of price";
    all = 0;
    int day = 0;
    while (all != sum)
    {
        int index;
        int now = all + d;
        for (int i=now ; i>=all ; i--)
        {
            if (dp[i] == 1)
            {
                index = i;
                break;
            }
        }
        all = index;
        day++;
    }
    cout << day;
}

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<utility>
#include<string>
#define LL long long
#define x first
#define y second
#define pb push_back
#define mp make_pair
using namespace std;

int n,d;
int c[60],sum[60] = {0};
int dp[60][500010];
vector<int> C;
bool ss(int a, int b){return a > b;}

int main(){
    scanf("%d%d", &n,&d);
    for(int i=1; i<=n;i++) scanf("%d", &c[i]);
    sort(c+1,c+n+1, ss);
    for(int i=1; i<=n ;i++){
        if(c[i] == 1<<28) continue;
        if(c[i] >= d) C.pb(c[i]);
        else{
            int sum = c[i];
            for(int j=i+1; j<=n;j++)
                if(sum + c[j] <= d) {
                    sum += c[j];
                    c[j] = 1<<28;
                }
            C.pb(sum);
        }
    }
    C.pb(0);
    sort(C.begin(), C.end());
    n = C.size() - 1;

    for(int i=1; i<=n;i++) sum[i] = sum[i-1] + C[i];

    for(int i=0; i<=n;i++) for(int j=0; j<=sum[n]; j++) { dp[i][j] = 1<<28; dp[i][0] = 0;}

    int day = 1<<28, val = 0;
    for(int i=1; i<=n;i++)
        for(int j=0; j<=sum[i]; j++){
            if(j - C[i] >= 0){
                for(int x = max(0,C[i]-d); x <= C[i]; x++){
                    if(dp[i-1][x] != 1<<28){
                        dp[i][j] = min(dp[i][j], dp[i-1][x] + 1 + dp[i-1][j-C[i]]);
                        val = j; day = dp[i][j];
                        break;
                    }

                }
               // printf("%d %d %d\n", i, j, dp[i][j]);
            }
            else dp[i][j] = dp[i-1][j];
        }

    if(val == 0) day = 0;

    printf("%d %d\n", val, day);

    return 0;
}

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define M 89
#define inf 0x3f3f3f3f
#define MP make_pair
#define X first
#define Y second

char str[50000];
int cnt[1000000];
int sum[50000];
int main()
{
	long long a;
	cin>>a>>str;
	int n = strlen( str );
	for( int i = 1; i <= n; ++i ){
		sum[i] = str[i-1] - '0';
		sum[i] += sum[i-1];
	}
	for( int i = 1; i <= n; ++i ){
		for( int j = 1; j <= i; ++j ){
			cnt[sum[i]-sum[j-1]]++;
		}
	}
	long long ans = 0;
	for( long long i = 1; i * i <= a; ++i ) if( a % i == 0 ) {
		if( a / i >= 1000000 ) continue;
		if( i * i == a )
			ans += 1LL * cnt[i] * cnt[a/i];
		else ans += 1LL * cnt[i] * cnt[a/i] * 2;
	}
	if( a == 0 ){
		ans = 1LL * cnt[0] * n * (n+1) - cnt[0] * cnt[0];
	}
	cout<<ans<<endl;
}

#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define FOR(i, n) for(lo i = 0; i < (n); i++)
#define pi 3.14159265358979323
#define eps 1e-3
#define MN 1000000
#define SM 26
using namespace std;
const li INF = 2e9;

lo f[MN];
lo s[MN];
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#else
  //  freopen("redblack.in", "r", stdin);
  //  freopen("redblack.out", "w", stdout);
#endif
    lo n, d;
    //cin >> n >> d;
    n = 50;
    d = 10000;
    vector<lo> good;
    lo maxval = 0;
    FOR(i, n)
    {
        lo a;
        //cin >> a;
        a = 10000;
        good.push_back(a);
        maxval += a;
    }
    maxval++;
    FOR(i, maxval)
    {
        s[i] = INF;
        f[i] = INF;
    }
    s[0] = 0;
    sort(good.begin(), good.end());
    FOR(i, n)
    {
        lo with = good[i];
        if(with <= d)
        {
            f[with] = 1;
        }
        for(lo i = max(0, with - d); i <= with; i++)
        {
            if(s[i] == INF)
                continue;
            f[with] = min(f[with], s[i] + 1);
        }
        for(lo i = maxval; i - with >= 0; i--)
        {
            if(s[i - with] == INF)
                continue;
            if(i <= d)
                s[i] = 1;
            else
                 s[i] = min(s[i], f[with] + s[i - with]);
        }
        for(lo i = with; i <= with + d; i++)
        {
            f[i] = min(f[i], f[with] + 1);
            s[i] = min(s[i], f[with] + 1);
        }
    }
    lo maxi = 0, cnt = 0;
    FOR(i, maxval)
    {
        if(s[i] == INF)
            continue;
        maxi = i;
        cnt = s[i];
    }
    cout << maxi << " " << cnt;
}

//bayemirov
#include <bits/stdc++.h>

#define NAME ""

using namespace std;

typedef long long ll;

#define SZ(x) ((int) (x).size())
#define pb push_back

void tachyon() {ios_base::sync_with_stdio(0); cin.tie(nullptr);}

const int N = 50;
const int MAXE = 1e4 + 1;

int n, d, c[N];
bool was[N * MAXE + 1];
vector<int> a;

void solve() {
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> c[i];
    was[0] = 1;
    for (int i = 0; i < n; i++)
        for (int j = N * MAXE; j >= 0; j--)
            if (was[j])
                was[j + c[i]] = 1;
    for (int i = 1; i <= N * MAXE; i++)
        if (was[i])
            a.pb(i);
    pair<int, int> ans = {0, 0};
    for (int i = 0; i < SZ(a); i++) {
        if (ans.first + d < a[i]) {
            ans.first = a[i - 1];
            if (ans.first + d < a[i])
                cout << ans.first << ' ' << ans.second + 1, exit(0);
            ans.second++;
            i--;
        }
        else {
            if (i == SZ(a) - 1) {
                ans.first = a.back();
                ans.second++;
            }
        }
    }
    cout << ans.first << ' ' << ans.second;
}

int main() {
    tachyon();
    #ifndef ONLINE_JUDGE
        freopen(NAME".in", "r", stdin);
        freopen(NAME".out", "w", stdout);
    #endif
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

bitset<500005> dp;
int N, D, ans, i, j, x, d, s=1;

int main(){
	for(scanf("%d%d",&N,&D),i=0;i<N;i++){
		scanf("%d",&x);
		for(j=500000;j>=x;j--)
			dp[j]=(dp[j]||dp[j-x]);
	}
	while(s){
		for(s=0,i=ans+D;i>ans;i--){
			if(dp[i]){
				s=i-ans; break;
			}
		}
		ans += s; d++;
	}
	printf("%d %d\n",ans,d-1);
	getchar(); getchar();
}

#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

int n,d;
bool dp[50*1010+5];
int c[55];

int main(){
    ios::sync_with_stdio(0);
    cin>>n>>d;
    for(int i=0 ; i<n ; i++){
        cin>>c[i];
    }
    dp[0] = 1;
    for(int j=0 ; j<n ; j++){
        for(int i=50*1010 ; i>=0 ; i--){
            if(i+c[j] <= 1010*50)
            dp[i+c[j]] |= dp[i];
        }
    }

    set<int>st;
    set<int>::iterator it;

    for(int i=1; i<=50*1010 ; i++){
        if(dp[i])
            st.insert(i);
    }

    int r=0;
    int r1=0;
    while(1){
        it = st.upper_bound(r+d);
        it--;

        if(*it <= r){
            break;
        }
        r1++;
        r = *it;
    }
    cout<<r<<" "<<r1;
    return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <functional>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair

using namespace std;

int n, d, a[300];
int64 inf = 10000000000000000ll, dp[2][500001], dp1[2][500001];

int main()
{
    cin >> n >> d;
    rep(i,1,n) cin >> a[i];
    sort(a + 1, a + n + 1);
    rep(i,0,50)
        rep(j,0,500000)
        {
            dp[0][j] = inf;
            dp1[0][j] = inf;
        }
    dp[0][0] = 0;
    dp1[0][0] = 0;
    if (a[1] <= d)
    {
        dp[0][a[1]] = 1;
        repd(i,a[1],1) dp1[0][i] = 1;
    }
    rep(i,2,n)
    {
        repd(j,500000,0)
        {
            if (j <= d)
            {
                if (j >= a[i]) dp[1][j] = min(dp[0][j], dp[0][j - a[i]]);
                else dp[1][j] = dp[0][j];
                if (j != 0) dp[1][j] = max(dp[1][j], 1ll);
            }
            else
            {
                if (j >= a[i]) dp[1][j] = min(dp[0][j], dp1[0][max(0, a[i] - d)] + dp[0][j - a[i]] + 1);
                else dp[1][j] = dp[0][j];
            }
            if (j == 500000) dp1[1][j] = dp[1][j];
            else dp1[1][j] = min(dp1[1][j + 1], dp[1][j]);
        }
        rep(j,0,500000)
            {
                dp[0][j] = dp[1][j];
                dp1[0][j] = dp1[1][j];
            }
    }
    int ans1 = 0;
    int64 ans2 = 0;
    rep(i,0,500000)
        if (dp[1][i] != inf && i >= ans1)
        {
            if (i == ans1) ans2 = min(ans2, dp[1][i]);
            else
            {
                ans1 = i;
                ans2 = dp[1][i];
            }
        }
    cout << ans1 << " " << ans2;
    return 0;
}

#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

#pragma comment(linker,"/STACK:33554432")

#define eps 1e-9
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sz(v) ((int)(v).size())
#define all(v) v.begin(),v.end()
#define same(a,b) (fabs((a)-(b))<eps)
#define set(arr,with) memset(arr,with,sizeof(arr))
#define add(target,value,mod) target = (target+value)%(mod)
#define put_min(target,value) target = min(target,value)
#define put_max(target,value) target = max(target,value)
typedef pair<int,int> pii;
typedef long long lld;

int N,J,K,A[51],S[51],D[2501];
vector <int> V;

int main()
{
	int i,j,k;
	scanf("%d%d",&N,&J);
	for (i=1;i<=N;i++) scanf("%d",A+i);
	sort(A+1,A+N+1);
	for (i=1;i<=N;i++) S[i] = S[i-1]+A[i];
	for (i=1;i<=N;i++) for (j=i;j<=N;j++) V.pb(S[j]-S[i-1]);
	sort(all(V)); V.erase(unique(all(V)),V.end());
	for (i=0;i<sz(V);i++){
		D[i] = 1e9;
		if (V[i] <= J) D[i] = 1;
		for (j=0;j<i;j++) if (V[i]-V[j] <= J) put_min(D[i],D[j]+1);
	}
	for (i=sz(V);i--;){
		if (D[i] < 1e9){ printf("%d %d\n",V[i],D[i]); return 0; }
	}
	puts("0 0");
}

#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<string>
#include<sstream>
#include<fstream>
#include<deque>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<stack>
#include<set>
#include<bitset>
#include<iomanip>
#include<utility>
#include<functional>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cwchar>
#include<cwctype>
#include<exception>
#include<locale>
#include<numeric>
#include<new>
#include<stdexcept>
#include<limits>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pii pair<int,int>

int readint()
{
	char c;
	while(c=getchar(),(c<'0'||c>'9')&&c!='-');
	bool flag=(c=='-');
	if(flag)c=getchar();
	int x=0;
	while(c>='0'&&c<='9')
	{
		x=x*10+c-48;
		c=getchar();
	}
	return flag?-x:x;
}

const int maxn=55;
const int maxm=10005;

int n,d;
bitset<maxn*maxm>q;
int f[maxn*maxm],mask[maxn];

int main(){
	scanf("%d%d",&n,&d);
	rep(i,n)scanf("%d",&mask[i]);
	q.set(0);
	rep(i,n)q|=q<<mask[i];
	for(int i=0;i<maxn*maxm;i++)f[i]=INF;
	int a=0;
	f[0]=0;
	rep(i,500000){
		if(!q.test(i))continue;
		while(a+d<i)a++;
		while(a<i&&f[a]>=INF)a++;
		if(a<i)f[i]=f[a]+1;
	}
	for(int i=500000;i>=0;i--)
	if(f[i]<INF){
		printf("%d %d",i,f[i]);
		return 0;
	}
}

//			be name khoda

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 51 * 52;
const int maxE = 51 * 10000;

vector <int> G2;
int a[maxN], dp2[51][maxE], n, d, tul, ans1, ans2;

void makeGraph2()
{
	dp2[0][0] = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 0; j < maxE; j++)
		{
			if(dp2[i - 1][j - a[i]])
				dp2[i][j] = 1;
			if(dp2[i - 1][j])
				dp2[i][j] = 1;
		}
	for(int i = 0; i < maxE; i++)
		if(dp2[n][i])
			G2.push_back(i);
}

int greedy()
{
	long long now = 0, day = 0, a2 = 0;
	for(;now < ans1 && day < G2.size(); day++)
	{
		while(day < G2.size() && now + d >= G2[day])
			day++;
		day--;
		now = G2[day];
		a2++;
	}
	return a2;
}

int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++)
		if(ans1 + d >= a[i])
			ans1 += a[i];
		else
			break;
	makeGraph2();
	ans2 = greedy();
	cout << ans1 << " " << ans2 << endl;
}

#include <bits/stdc++.h>

typedef long long LL;
typedef double DB;

#define sf scanf
#define pf printf
#define mp make_pair
#define nl printf("\n")

#define FOR(i,a,b) for(i = a; i <= b; ++i)
#define FORD(i,a,b) for(i = a; i >= b; --i)
#define FORS(i,n) for(i = 0; i < n; ++i)
#define FORM(i,n) for(i = n - 1; i >= 0; --i)
#define reset(i,n) memset(i, n, sizeof(i))

using namespace std;

const LL mod = 1e9 + 7;
const LL INF = 4e18;
const int inf = 2e9;

int gcd(int a, int b) { return b? gcd(b, a%b): a; }
int lcm(int a, int b) { return a/ gcd(a, b)*b; }

int n, d, a[55], ans;
bool done[55];
LL day;

int main(void)
{
    int i, j, will = 1;
    sf("%d %d", &n, &d);
    FORS(i, n) sf("%d", a + i);
    sort(a, a + n);
    while(will) {
        will = 0;
        FORS(i, n) {
            if(will + a[i] <= d && !done[i]) {
                done[i] = 1;
                will += a[i];
                ans += a[i];
            }
        }
        if(!will) {
            FORS(i, n)
                if(ans + d >= a[i] && !done[i]) {
                    done[i] = 1;
                    will += a[i];
                    ans += a[i];
                    day *= 2LL;
                    break;
                }
        }
        if(will) day++;
    }
    pf("%d %I64d\n", ans, day);
    return 0;
}

#include <iostream>
#include <set>

using namespace std;

int N, D;
bool poss[500050];

int main()
{
    for(int i=0;i<500050;i++) poss[i] = false;
    cin >> N >> D;
    poss[0] = true;
    for(int k=0;k<N;k++)
    {
        int c;
        cin >> c;
        for(int i=500049; i>=0; i--) poss[i] |= poss[i-c];
    }
    //for(int i=0;i<20;i++) cout << poss[i];  cout << endl;
    int p = 0, d = 0;
    while(1)
    {
        int latest = -1;
        for(int i = p+1; i <= p+D; i++)
        {
            if(poss[i]) latest = i;
        }
        if(latest == -1) break;
        p = latest;
        d++;
    }
    cout << p << ' ' << d;
    return 0;
}

//			be name khoda

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 51;
const int maxE = maxN * 10000;

vector <int> G2;
int a[maxN], dp2[51][maxE], n, d, ans1, ans2;

void makeGraph2()
{
	dp2[0][0] = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 0; j < maxE; j++)
		{
			if(dp2[i - 1][j - a[i]])
				dp2[i][j] = 1;
			if(dp2[i - 1][j])
				dp2[i][j] = 1;
		}
	for(int i = 0; i < maxE; i++)
		if(dp2[n][i])
			G2.push_back(i);
}

int greedy()
{
	long long now = 0, day = 0, a2 = 0;
	for(;day < G2.size(); day++)
	{
		while(day < G2.size() && now + d >= G2[day])
			day++;
		day--;
		if(now == G2[day])
			break;
		now = G2[day];
		a2++;
	}
	ans1 = now;
	return a2;
}

int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	sort(a + 1, a + n + 1);
	makeGraph2();
	ans2 = greedy();
	cout << ans1 << " " << ans2 << endl;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
	return x*f;
}
const int N=5e5+5;
int n,d,c[N];
int ans,last;
int f[N];
inline int check()
{
	last=ans;
	for(int i=last+d;i>=last;i--)
	{
		if(f[i])
		{
			ans=i;
			break;
		}
	}
	return ans==last;
}
int main()
{
	n=read(),d=read();
	int s=0;
	for(int i=1;i<=n;i++) c[i]=read(),s+=c[i];
	f[0]=1;
	for(int i=1;i<=n;i++)
		for(int j=s;j>=c[i];j--)
			f[j]|=f[j-c[i]];
	int t=0;
	while(++t) if(check()) return printf("%d %d\n",ans,t-1),0;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 57;
const int K = 500007;

int n, d;
int c[N];

int tim[K];
int qtd[K];
multiset<int> s;

int main () {
    scanf("%d %d", &n, &d);

    for (int i = 0; i < n; i++)
        scanf("%d", c+i);

    qtd[0] = -1;
    tim[0] = 0;
    s.insert(0);

    for (int i = 1; i < K; i++) {
        if (i-d-1 >= 0 && tim[i-d-1] != -1)
            s.erase(s.find(tim[i-d-1]));
        if (s.empty()) {
            printf("%d %d\n", i-d-1, tim[i-d-1]);
            return 0;
        }

        qtd[i] = n;
        for (int j = n-1; j >= 0; j--) {
            if (i >= c[j] && qtd[i-c[j]] < j)
                qtd[i] = j;
        }

        if (qtd[i] == n) {
            tim[i] = -1;
        } else {
            tim[i] = *(s.begin()) + 1;
            s.insert(tim[i]);
        }
    }
}

#include <bits/stdc++.h>
using namespace std;

bitset<500005> dp;
int N, D, ans, i, j, x, d, s=1;

int main(){
	dp[0] = 1;
	for(scanf("%d%d",&N,&D),i=0;i<N;i++){
		scanf("%d",&x);
		for(j=500000;j>=x;j--)
			dp[j]=(dp[j]||dp[j-x]);
	}
	while(s){
		for(s=0,i=ans+D;i>ans;i--){
			if(dp[i]){
				s=i-ans; break;
			}
		}
		ans += s; d++;
	}
	printf("%d %d\n",ans,d-1);
	getchar(); getchar();
	return 0;
}

//bayemirov
#include <bits/stdc++.h>

#define NAME ""

using namespace std;

typedef long long ll;

#define SZ(x) ((int) (x).size())
#define pb push_back

void tachyon() {ios_base::sync_with_stdio(0); cin.tie(nullptr);}

const int N = 50;
const int MAXE = 1e4 + 1;

int n, d, c[N];
bool was[N * MAXE + 1];
vector<int> a;

void solve() {
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> c[i];
    was[0] = 1;
    for (int i = 0; i < n; i++)
        for (int j = N * MAXE; j >= 0; j--)
            if (was[j])
                was[j + c[i]] = 1;
    for (int i = 1; i <= N * MAXE; i++)
        if (was[i])
            a.pb(i);
    pair<int, int> ans = {0, 0};
    for (int i = 0; i < SZ(a); i++) {
        if (ans.first + d < a[i]) {
            if (i)
                ans.first = a[i - 1];
            if (ans.first + d < a[i])
                cout << ans.first << ' ' << ans.second, exit(0);
            ans.second++;
            i--;
        }
        else {
            if (i == SZ(a) - 1) {
                ans.first = a.back();
                ans.second++;
            }
        }
    }
    cout << ans.first << ' ' << ans.second;
}

int main() {
    tachyon();
    #ifndef ONLINE_JUDGE
        freopen(NAME".in", "r", stdin);
        freopen(NAME".out", "w", stdout);
    #endif
    solve();
    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
typedef long long ll;
#define MAX 10105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m, k;
//int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

const int MAXN = MAX * 10;
bitset<MAXN> bs;
int d,c[MAX];
int main(){
    scanf("%d%d", &n, &d);
    bs[0]=1;
    REP(i,n) {
        scanf("%d", c+i);
        bs |= bs<<c[i];
    }
    vector<int> v;
    REP(i,MAXN) if (bs[i] && i) v.push_back(i);
    int day = 0,dis,j;
    for(dis=0,j=0;j<v.size();) {
        int tj = j;
        while(j<v.size() && v[j] <= dis + d) j++;
        if (tj==j) break;
        dis = v[j-1];
        day++;
    }
    printf("%d %d\n", dis, day);
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1000005;
int dp[maxn];
vector<int>g;
int main()
{
    int i,j,n,d;
    cin>>n>>d;
    dp[0]=1;
    for(i=1;i<=n;i++){
        int m;
        scanf("%d",&m);
        for(j=maxn-1;j>=m;j--){
            if(dp[j-m]) dp[j]=1;
        }
    }
    for(i=maxn-1;i>=0;i--){
        if(dp[i]) g.push_back(i);
    }
    int s=0,w=0;
    while(g.size()){
        if(g[g.size()-1]-w>d) break;
        while(g.size()>1&&g[g.size()-2]<=w+d){
            g.pop_back();
        }
        w=g[g.size()-1];
        s++;
        g.pop_back();
    }
    cout<<w<<" "<<s<<endl;
}

#include<iostream>
#include<algorithm>
using namespace std;
long c[10004];
int n,d;
long  x[500005];int z[600004];
int main(){

cin>>n>>d;long s=0;
for(int i=1;i<=n;i++){cin>>c[i];s=s+c[i];}
sort(c+1,c+n+1);
long h=0;long p=d;
    x[c[1]]=1;x[0]=1;
//cout<<s<<endl;
for(long i=2;i<=n;i++){
    for(long j=s;j>=c[i];j--){
        x[j]=x[j-c[i]];
    }}
    z[0]=1;
    for(long i=1;i<=s;i++){
        if(x[i]==1){z[i]=i;}
        else z[i]=z[i-1];
    }
    for(long i=s+1;i<=s+2*d;i++)z[i]=z[s];
int e=0;long l=0;
while(e==0){
    long f=z[p];
  //  cout<<f<<" "<<h<<" "<<p<<endl;
    if(f==h){e=1;}
    else{h=f;p=h+d;l++;}
}
cout<<h<<" "<<l<<endl;}

#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int n,d,a[60],t;
bool best[500100];
vector<int> s;

int main()
{
    //freopen("input.in","r",stdin);
    cin>>n>>d;
    for(int i=1;i<=n;++i)
    {
        cin>>a[i];
        t+=a[i];
    }

    best[0] = 1;
    for(int i=1;i<=n;++i)
        for(int j=t-a[i];j>=0;--j)
        {
            best[j+a[i]] = max(best[j],best[j+a[i]]);
        }

    for(int i=0;i<=t;++i)
        if(best[i]==1)
        {
            //cout<<i<<" ";
            s.push_back(i);
        }

    int x = 0,y = 0,rez = 0;

    while(s[x]+d >= s[x+1] && x<s.size()-1)
    {
        ++x;
        y += s[x+1] - s[x];
        if(y >= d)
        {
            ++rez;
            y = 0;
        }
    }

    cout<<s[x]<<" "<<rez+1;

    return 0;
}

#include<bits/stdc++.h>
#define forn(I,N) for(int I=0;I<(N);I++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
typedef long long ll;
using namespace std;
int knap[51][500001];
int ans1[500001];
int main()
{
    int n,d,x;
    cin>>n>>d;
    vector<int>a;
    forn(i,n)
    {
        cin>>x;
        a.pb(x);
    }
    sort(a.begin(),a.end());
    vector<int>nums;
    int sum=0;
    forn(i,n)
    {
        if(sum+d>=a[i])
        {
            sum+=a[i];
            nums.pb(a[i]);
        }
        else break;
    }
    cout<<sum<<' ';
    for(int i=0;i<nums.size();i++)
    {
        if(i==0){knap[i][nums[i]]=1;continue;}
        for(int j=1;j<=sum;j++)
        {
            if(j==nums[i])knap[i][j]=1;
            knap[i][j]=knap[i][j]|knap[i-1][j];
            if(j>nums[i])knap[i][j]=knap[i][j]|knap[i-1][j-nums[i]];
        }
    }
    int h=nums.size()-1;
    deque<int>t;
    for(int i=1;i<=sum;i++)
    {
        if(i<=d && knap[h][i]==1)
        {
            ans1[i]=1;
            t.push_back(1);
        }
        else
        {
            if(knap[h][i]==1)
            {
                ans1[i]=t.front()+1;
                int g=t.front()+1;
                while(t.back()>g)t.pop_back();
                t.push_back(g);
                if(ans1[i-d]>0 && ans1[i-d]==t.front())
                {
                    t.pop_front();
                }
            }
        }
    }
    cout<<ans1[sum];
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int MAX = 54;
int n, d;
int a[MAX];
int dp[1000000];

int main() {
	int i, j;
	int s = 0;

	scanf("%d%d", &n, &d);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		s += a[i];
	}

	dp[0] = 1;
	for (i = 0; i < n; i++) {
		for (j = a[i]; j <=s; j++) {
			dp[j] |= dp[j - a[i]];
		}
	}

	int cur = 0;
	int lvl = 0;

	while (1) {
		for (i = cur + d; i != cur; i--) {
			if (dp[i]) break;
		}

		if (i == cur) break;

		cur = i;
		lvl ++;
	}

	printf ("%d %d\n", cur, lvl);

	return 0;
}

#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define FOR(i, n) for(lo i = 0; i < (n); i++)
#define pi 3.14159265358979323
#define eps 1e-3
#define MN 1000000
#define SM 26
using namespace std;
const li INF = 2e15;

li f[MN];
li s[MN];
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#else
  //  freopen("redblack.in", "r", stdin);
  //  freopen("redblack.out", "w", stdout);
#endif
    lo n, d;
    cin >> n >> d;
    //n = 50;
    //d = 10000;
    vector<lo> good;
    lo maxval = 0;
    FOR(i, n)
    {
        lo a;
        cin >> a;
        //a = 10000;
        good.push_back(a);
        maxval += a;
    }
    maxval++;
    FOR(i, maxval)
    {
        s[i] = INF;
        f[i] = INF;
    }
    s[0] = 0;
    sort(good.begin(), good.end());
    FOR(i, n)
    {
        lo with = good[i];
        if(with <= d)
        {
            f[with] = 1;
        }
        for(lo i = max(0, with - d); i <= maxval; i++)
        {
            if(s[i] == INF)
                continue;
            f[with] = min(f[with], s[i] + 1);
        }
        for(lo i = maxval; i - with >= 0; i--)
        {
            if(s[i - with] == INF)
                continue;
            if(i <= d)
                s[i] = 1;
            else
                 s[i] = min(s[i], f[with] + s[i - with]);
        }
        for(lo i = with; i <= with + d; i++)
        {
            f[i] = min(f[i], f[with] + 1);
           // s[i] = min(s[i], f[with] + 1);
        }
    }
    lo maxi = 0, cnt = 0;
    FOR(i, maxval)
    {
        if(s[i] == INF)
            continue;
        maxi = i;
        cnt = s[i];
    }
    cout << maxi << " " << cnt;
}

#include "bits/stdc++.h"
#define MAXN 500009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
bool can[MAXN];
int dp[MAXN];
int arr[55];
int main(){
	#ifndef ONLINE_JUDGE
    freopen("file.in", "r", stdin);
	#endif
	memset(dp,127,sizeof dp);
	int a,d;
	scanf("%d%d",&a,&d);
	dp[0]=0;
	for(int i=1;i<=a;i++)
		scanf("%d",arr+i);
	can[0]=1;
	sort(arr+1,arr+a+1);
	for(int i=1;i<=a;i++)
		for(int j=MAXN-9;j>=0;j--)
			if(j+d>=arr[i] and can[j]){
				umin(dp[j+arr[i]],(j+arr[i])/d+((j+arr[i])%d!=0));
				umin(dp[j+arr[i]],dp[j]*2+1);
				can[j+arr[i]]=1;
			}
	for(int i=MAXN-9;i>=0;i--)
		if(can[i]){
			printf("%d %d\n",i,dp[i]);
			return 0;
		}
	return 0;
}

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<ctype.h>
using namespace std;

#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> Pi;

int n,inp[100],d;
int s[550010],tmp[550010];

int main()
{
    scanf("%d%d",&n,&d);
    int i,j;
    for(i=1;i<=n;i++)scanf("%d",inp+i);
    tmp[0]=1;
    for(i=1;i<=n;i++){
        for(j=1;j<=10000*i;j++){
            if(j-inp[i]>=0)s[j]=tmp[j]+tmp[j-inp[i]];
            else s[j]=tmp[j];
        }
        for(j=1;j<=10000*i;j++)tmp[j]=s[j];
    }
    int cnt=0;
    for(i=0;i<=10000*n;){
        int tmp=i;
        for(j=i+d;j>i;j--){
            if(s[j]){i=j;cnt++;break;}
        }
        if(tmp==i){printf("%d %d\n",i,cnt);break;}
    }
    return 0;
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;

int n;
int d;

#define MAX 50

vector<int> v;

#define MAXX 500002
bool flag[MAXX];
vector<int> vv;

int main(){
	cin >> n >> d;
	for (int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		v.push_back(a);
	}
	sort(v.begin(), v.end());
	if (d<v[0]){
		puts("0 0");
		return 0;
	}
	flag[0] = true;
	int mx = 0;
	for (int i = 0; i < v.size(); i++){
		for (int j = mx; j >= 0; j--){
			flag[j + v[i]] = true;
		}
		mx += v[i];
	}
	for (int i = 0; i <= mx; i++){
		if (flag[i]){
			vv.push_back(i);
		}
	}
	int cur = 0;
	int cnt = 0;
	while (1){
		int id = upper_bound(vv.begin(), vv.end(), cur + d) - vv.begin();
		id--;
		if (id>=0&&cur < vv[id]){
			cur = vv[id];
			cnt++;
		}
		else{
			break;
		}
	}
	cout <<cur<<" "<< cnt << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define out(x) cout << #x"=" << x << endl

const int N = 60;
const int L = 5e5 + 10;
const int INF = 1e9 + 10;

int n, d;
int c[N];
set<int> s[N];
vector<int> v;
vector<int> dp;

int main() {
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; ++i) {
    scanf("%d", c + i);
  }
  sort(c, c + n);
  s[0].insert(0);
  s[0].insert(c[0]);
  for (int i = 1; i < n; ++i) {
    for (auto t : s[i - 1]) {
      s[i].insert(t + c[i]);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (auto t : s[i]) {
      v.push_back(t);
    }
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  dp.resize(v.size());
  fill(dp.begin(), dp.end(), INF);
  dp[0] = 0;
  int j = 0;
  int who = v.size() - 1;
  for (int i = 0; i < v.size(); ++i) {
    if (dp[i] >= INF) {
      who = i - 1;
      break;
    }
    for (; v[j] - v[i] <= d && j < v.size(); ++j) {
      dp[j] = min(dp[j], dp[i] + 1);
    }
  }
  printf("%d %d\n", v[who], dp[who]);
  return 0;
}

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 500010;

bool f[MAXN];

int main() {
    int n, d;
    scanf("%d %d", &n, &d);

    f[0] = 1;
    for(int i = 0 ; i < n; i ++) {
        int x;
        scanf("%d", &x);
        for (int j = MAXN - 1 - x; j >= 0; --j)
            f[j+x] |= f[j];
    }

    int r = 0, sum = 0, ans = 0;
    while (r < MAXN) {
        int next = -1;
        while (r <= sum+d && r < MAXN) {
            if (f[r]) next = r;
            r++;
        }
        if (next == -1) break;
        sum = next;
        ans++;
    }

    printf("%d %d\n", sum, ans);
    return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cctype>
#define REP(i,n) for( int (i)=0;(i)<(int)(n); ++(i))
#define REPR(i,n) for( int (i) = n; (i)>=0; --(i))
#define REPN(i,x,y) for( int i = x; (i) < (int)(y); (i)++ )
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define SZ(x) (int)(x).size()
#define RI(n) scanf("%d",&(n))
#define RII(x,y) scanf("%d %d",&(x),&(y))
#define RS(x) scanf("%s",x)
#define OI(x) printf("%d\n",(x))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define MP(x,y) make_pair((x),(y))
#define FT first
#define SD second
#define PB push_back
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
LL LLMAX = 9223372036854775807LL;
const int MOD = 1000000007;
const int maxn = 4000+10;
int n,d,c[55];
vector<int> s;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
	//freopen("D-large.in","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
    RII(n,d);
    REP(i,n)RI(c[i]);
    bitset<500001> dp;
    dp.set(0);
    for(int i=0;i<n;++i)
        for(int j=500001;j-c[i]>=0;--j)if(dp[j-c[i]]&&dp[j]==0){
            dp.set(j);
            s.PB(j);
        }
    sort(s.begin(),s.end());
    int ans=0,now=0;
    for(int i=0;i<s.size();++ans){
        for(;i<s.size() && now+d >= s[i];++i);
        if(s[i-1]!=now)now = s[i-1];
        else break;
        //OI(now);
    }
    OII(now,ans);
}

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <unordered_map>
#include <set>
#include <string>
#include <cmath>

using namespace std;

// Knapsack

int dp[55][500009],a[55];
vector<int> fin;

int main() {
	int i,j,t1,t2,t3,t4,n,d,curr=0,ans=0;
	scanf("%d %d",&n,&d);
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<=500000;i++){
		dp[0][i]=0;
	}
	dp[0][0]=1;
	for(i=1;i<=n;i++){
		for(j=0;j<=500000;j++){
			dp[i][j]=dp[i-1][j];
		}
		for(j=0;j<=500000;j++){
			if(a[i]<=j){
				dp[i][j]=(dp[i-1][j-a[i]]==1)?1:0;
			}
		}
	}
	for(i=0;i<=500000;i++){
		if(dp[n][i])
			fin.push_back(i);
	}
	while(1){
		t1=upper_bound(fin.begin(),fin.end(),curr+d)-fin.begin();
		t1--;
		t1=fin[t1];
		if(t1>curr){
			ans++;
			curr=t1;
		}
		else{
			break;
		}
	}
	printf("%d %d\n",curr,ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int M=6e5;
int n,d,f[M];
int main(){
	cin>>n>>d;
	f[0]=1;
	int now=0,times=0;

	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		for(int j=M-1;j>=0;j--)
		f[j]|=f[j-x];
	}
	while(1){
		int old=now;
		for(int i=1;i<=d;i++)
		if(f[i+old])now=old+i;
		if(now==old)break;
		times++;
	}
	cout<<now<<" "<<times<<endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <functional>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair

using namespace std;

int a[300], b[300], m, n, d;
bool used[51][500001], used1[500001];

void solve(int i, int s)
{
    used1[s] = true;
    if (i > n) return;
    if (used[i][s]) return;
    used[i][s] = true;
    solve(i + 1, s);
    solve(i + 1, s + a[i]);
}

int main()
{
    cin >> n >> d;
    rep(i,1,n) cin >> a[i];
    memset(used, false, sizeof(used));
    memset(used1, false, sizeof(used1));
    solve(1, 0);
    m = 0;
    rep(i,0,500000)
        if (used1[i]) b[++m] = i;
    int ans1 = 0, ans2 = 0;
    int i = 1;
    while (i <= m)
    {
        if (i == m || b[i + 1] > ans1 + d)
        {
            if (b[i] == ans1) break;
            ans1 = b[i];
            ans2++;
        }
        else i++;
    }
    cout << ans1 << " " << ans2;
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

bool used[50];
int n;
int ss[500001], v[50], sums[50];

int subsum(int val) {
	int i, j, ans, curr;
	memset (ss, -1, sizeof ss);
	ss[0] = -2;
	for (i = 0; i < n; i++) {
		if (used[i])	continue;
		for (j = 0; j <= val-v[i]; j++) {
			if (ss[j] != -1 && ss[j] != i && ss[j+v[i]] == -1)
				ss[j+v[i]] = i;
		}
	}
	for (ans = val; ss[ans] == -1; ans--);
	curr = ans;
	while (ss[curr] != -2) {
		used[ss[curr]] = true;
		curr -= v[ss[curr]];
	}
	return ans;
}

int main (void) {
	int d;
	int i, ans = 0, x;
	int size = 0;
	scanf ("%d%d", &n, &d);
	for (i = 0; i < n; i++)
		scanf ("%d", &v[i]);
	sort(v, v+n, greater<int>());
	int tot = 0;
	while (x = subsum(tot+d)) {
		tot += x;
		sums[size] = x;
		i = size++;
		while (i > 0 && sums[i-1] >= sums[i]) {
			swap(sums[i-1], sums[i]);
			i--;
		}
//		printf ("%d %d\n", x, i);
		ans += i+1;
	}
	tot = 0;
	for (i = 0; i < n; i++)	if(used[i])	tot += v[i];
	printf ("%d %d\n", tot, ans);
}

#include <bits/stdc++.h>

typedef long long LL;
typedef double DB;

#define sf scanf
#define pf printf
#define mp make_pair
#define nl printf("\n")

#define FOR(i,a,b) for(i = a; i <= b; ++i)
#define FORD(i,a,b) for(i = a; i >= b; --i)
#define FORS(i,n) for(i = 0; i < n; ++i)
#define FORM(i,n) for(i = n - 1; i >= 0; --i)
#define reset(i,n) memset(i, n, sizeof(i))

using namespace std;

const LL mod = 1e9 + 7;
const LL INF = 4e18;
const int inf = 2e9;

int gcd(int a, int b) { return b? gcd(b, a%b): a; }
int lcm(int a, int b) { return a/ gcd(a, b)*b; }

int n, d, a[55], ans;
bool done[55];
LL day;

int main(void)
{
    int i, j, will = 1;
    sf("%d %d", &n, &d);
    FORS(i, n) sf("%d", a + i);
    sort(a, a + n);
    reverse(a, a + n);
    while(will) {
        will = 0;
        FORS(i, n) {
            if(will + a[i] <= d && !done[i]) {
                done[i] = 1;
                will += a[i];
                ans += a[i];
            }
        }
        if(!will) {
            FORS(i, n) {
                if(will + a[i] <= ans + d && !done[i]) {
                    done[i] = 1;
                    will += a[i];
                    ans += a[i];
                }
            }
            if(will) day *= 2LL;
        }
        if(will) day++;
    }
    pf("%d %I64d\n", ans, day);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int sign=1,sum=0;
	char c=getchar();
	while(!isdigit(c))
	{
		if (c=='-') sign=-1;
		c=getchar();
	}
	while(isdigit(c))
	{
		sum=(sum<<1)+(sum<<3)+c-'0';
		c=getchar();
	}
	return sign*sum;
}
#define read(x) x=read()
const int N=5e6+50,M=1e5+50;
int n,d;
int f[N],c[M];
int main()
{
	read(n),read(d);
	for (int i=1;i<=n;++i)
	  read(c[i]);
	  memset(f,0,sizeof(f));
	f[0]=1;
	for (int i=1;i<=n;++i)
	  for (int j=M-1;j>=c[i];--j)
	    f[j]|=f[j-c[i]];
	int ans=0;
	int point=0;
	for (;point<M;++point)
	{
		int cost=ans+d;
		for (int i=cost;i>=ans;--i)
		{
			if (f[i])
			  {
			  	ans=i;
			  	break;
			  }
		}
		if (cost==ans+d) break;
	}
	printf("%d %d\n",ans,point);
}

#include <cstdio>
const	int	maxn = 55;
const	int	maxd = 550000;
int	n, d, sum, ans, res;
int	c[maxn], dp[maxd];
int	main()
{
	scanf("%d%d", &n, &d); sum = 0;
	for (int i=1; i<=n; i++) scanf("%d", &c[i]), sum += c[i];
	dp[0] = 1;
	for (int i=1; i<=n; i++)
		for (int j=sum-c[i]; j>=0; j--) if (dp[j] == j) dp[j+c[i]] = j+c[i];
	for (int i=1; i<=sum; i++) if (!dp[i]) dp[i] = dp[i-1];
	for (ans=res=0; dp[ans+d] != ans; ans = dp[ans + d]) res++;
	printf("%d %d\n", res, ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
long long fora = 0;
vector<long long> a;
long long acul[51];
int main(){
    long long n,m;
    cin >> n >> m;
    a.push_back(0);
    a.push_back(1000000);
    for(int i = 0;i < n;i++){
        long long k;
        cin >> k;
        a.push_back(k);
    }
    bool vet[n+1];
    memset(vet,0,sizeof vet);
    sort(a.begin(),a.end());
    acul[0] =a[0];
    for(int i = 0;i < n;i++){
        acul[i] = acul[i-1]+a[i];
    }
    fora = acul[n];
    for(int i = 1;i <= n+1;i++){
        if(a[i] > acul[i-1]+m){
            cout << acul[i-1] << " " << ceil(acul[i-1]/m);
            break;
            //fora = acul[i-1];
        }
    }

}

#include <bits/stdc++.h>
using namespace std;
long long fora = 0;
vector<long long> a;
long long acul[51];
int main(){
    long long n,m;
    cin >> n >> m;
    a.push_back(0);
    a.push_back(1000000);
    for(int i = 0;i < n;i++){
        long long k;
        cin >> k;
        a.push_back(k);
    }
    bool vet[n+1];
    memset(vet,0,sizeof vet);
    sort(a.begin(),a.end());
    acul[0] =a[0];
    for(int i = 0;i < n;i++){
        acul[i] = acul[i-1]+a[i];
    }
    fora = acul[n];
    for(int i = 1;i <= n+1;i++){
        if(a[i] > acul[i-1]+m){
            cout << acul[i-1] << " " << acul[i-1]/m + 1;
            break;
            //fora = acul[i-1];
        }
    }

}

/* Created Time: Wednesday, November 20, 2013 AM12:51:09 CST */
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 555555;
int n,d,c[55],a[N+7],dp[55];
void work() {
    int sum = 0,i,j;
    a[0] = 0;
    for (i = 1; i <= n && sum+c[i]<=d; i ++) {
        sum += c[i];
        for (j = N; j >= c[i]; j --) if (a[j-c[i]]!=INF) a[j] = 1;
        dp[i] = 1;
    }
    for ( ; i <= n && sum+d>=c[i]; i ++) {
        sum += c[i];
        int minx = INF;
        for (j = max(0,c[i]-d); j <= c[i]; j ++) minx = min(a[j],minx);
        dp[i] = dp[i-1]+minx+1;
        for (j = N; j >= c[i]; j --) a[j] = min(a[j],a[j-c[i]]+minx+1);
    }

    printf("%d %d\n",sum,dp[i-1]);
}
int main() {
    scanf("%d%d",&n,&d);
    for (int i = 1; i <= n; i ++)
        scanf("%d",&c[i]);
    sort(c+1,c+n+1);
    memset(a,INF,sizeof(a));
    work();
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
#include<map>
#include<string>
#include<vector>
#include<set>

using namespace std;
#define For(i,l,r) for (int i = l; i <= r; ++i)
#define Cor(i,l,r) for (int i = l; i >= r; --i)
#define Fill(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define MP make_pair
#define PII pair<int,int>
#define flt double
#define INF (0x3f3f3f3f)
#define MaxN 1020304
#define MaxNode 1020304
#define MD 1000000007

int n,d,s[MaxN],a[MaxN],f[MaxN];
int main() {
	//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	cin >> n >> d;
	int Sum = 0;
	For(i,1,n) scanf("%d",&a[i]), Sum += a[i];
	sort(a + 1,a + n + 1);
	int curS = 0; s[0] = 0;
	For(i,1,n) {
		s[i] = s[i - 1] + a[i];
		if (a[i] - curS > d) {
			n = i - 1; break ;
		}
		curS += a[i];
	}
	Fill(f,INF); f[0] = 0;
	For(i,1,n) {
		For(j,0,i - 1) For(k,0,j) if ((s[i] - s[j]) - s[k] <= d) f[i] = min(f[i],f[j] + f[k] + 1);
	}
	cout << curS << ' ' << f[n] << endl;
	/*For(i,0,Sum + 100) f[i] = INF; f[0] = 0;
	memcpy(g,f,sizeof(f));
	sort(a + 1,a + n + 1);
	int ans1 = 0, ans2 = 0;
	For(i,1,n) {
		if (getProf(a[i]) == INF) break;
		int days = getProf(a[i]) + 1;
		ans1 += a[i]; ans2 += days;
		Cor(j,Sum,a[i]) f[j] = min(f[j],f[j - a[i]] + days);
		Cor(j,Sum - 1,0) g[j] = min(g[j + 1],f[j]);
	}
	cout << ans1 << ' ' << ans2 << endl;*/

	return 0;
}

#pragma comment(linker, "/STACK:268435456,268435456")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define MAXN 510001

int a[50];
bool dp[MAXN];

int main(){
    int n,d;
    scanf("%d %d", &n, &d),dp[0]=1;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i]);
        for(int j=MAXN-1;j>=a[i];j--)
            dp[j]|=dp[j-a[i]];
    }
    //for(int i=0;i<30;i++)
        //printf("%d %d\n", i, dp[i]);
    int curr=0,ans=0,ans2=-1;
    while(true){
        int temp=curr;
        for(int i=d;i>=1;i--)
            if(dp[temp+i]){
                //printf("c%d %d\n",temp,i);
                temp+=i,ans++,ans2=max(ans2,temp);
                break;
            }
        if(temp==curr) break;
        curr=temp;
    }
    printf("%d %d\n", ans2, ans);
    //system("pause");
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int dp[600000],tree[3000000],ara[600000],big=2500000;
vector<int>vc;
void build(int node,int st,int en)
{
    tree[node]=big;
    if(st==en)return ;
    int mid=(st+en)/2;
    build(node*2,st,mid);
    build(node*2+1,mid+1,en);
}
void update(int node,int st,int en,int i,int val)
{
    if(st>i or en<i)return ;
    if(st==en){
        tree[node]=val;
        return;
    }
    int mid=(st+en)/2;
    update(node*2,st,mid,i,val);
    update(node*2+1,mid+1,en,i,val);
    tree[node]=min(tree[node*2],tree[node*2+1]);
}
int query(int node,int st,int en,int l,int r)
{
    if(st>r or en<l)return big;
    if(st>=l and en<=r){
        return tree[node];
    }
    int mid=(st+en)/2;
    int p=query(node*2,st,mid,l,r);
    int q=query(node*2+1,mid+1,en,l,r);
    return min(p,q);

}
int bin(int id,int d)
{
    int l=0,k=id,mid,ans=id,val=vc[id];
    while(l<=k){
        mid=(l+k)/2;
        if(vc[mid]+d>=val){
            ans=mid;
            k=mid-1;
        }
        else l=mid+1;

    }
    return ans;

}
int main()
{
    int i,j,k,l,m,n,d,sum=0;
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++){
            cin>>ara[i];
            sum+=ara[i];
    }
    dp[0]=1;
    for(int i=1;i<=n;i++){
        for(int j=sum;j>=ara[i];j--){
       dp[j]|=dp[j-ara[i]];
        }
    }
   for(int i=0;i<=sum;i++){
    if(dp[i])vc.pb(i);
   }
   build(1,0,vc.size()-1);
   update(1,0,vc.size()-1,0,0);
   for(int i=1;i<vc.size();i++){
       // cout<<vc[i]<<endl;
    l=bin(i,d);
    dp[i]=query(1,0,vc.size()-1,l,i)+1;
    update(1,0,vc.size()-1,i,dp[i]);
   }
  for(int i=vc.size()-1;i>=0;i--){
    if(dp[i]>=big)continue;
    cout<<vc[i]<<" "<<dp[i]<<endl;
    break;
  }

}

//bayemirov
#include <bits/stdc++.h>

#define NAME ""

using namespace std;

typedef long long ll;

#define SZ(x) ((int) (x).size())
#define pb push_back

void tachyon() {ios_base::sync_with_stdio(0); cin.tie(nullptr);}

const int N = 50;
const int MAXE = 1e4 + 1;

int n, d, c[N];
bool was[N * MAXE + 1];
vector<int> a;

void solve() {
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> c[i];
    was[0] = 1;
    for (int i = 0; i < n; i++)
        for (int j = N * MAXE; j >= 0; j--)
            if (was[j])
                was[j + c[i]] = 1;
    for (int i = 1; i <= N * MAXE; i++)
        if (was[i])
            a.pb(i);
    pair<int, int> ans = {0, 0};
    for (int i = 0; i < SZ(a); i++) {
        if (ans.first + d < a[i]) {
            if (i)
                ans.first = a[i - 1];
            if (ans.first + d < a[i])
                cout << ans.first << ' ' << ans.second + 1, exit(0);
            ans.second++;
            i--;
        }
        else {
            if (i == SZ(a) - 1) {
                ans.first = a.back();
                ans.second++;
            }
        }
    }
    cout << ans.first << ' ' << ans.second;
}

int main() {
    tachyon();
    #ifndef ONLINE_JUDGE
        freopen(NAME".in", "r", stdin);
        freopen(NAME".out", "w", stdout);
    #endif
    solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

bitset<111111>dp;
int N,D;
signed main(){
    cin>>N>>D;
    dp[0]=1;
    rep(i,N){
        int a;cin>>a;
        dp|=dp<<a;
    }

    vint lis;
    rep(i,111111)if(dp[i])lis.pb(i);

    int cur=0;
    int cnt=0;
    while(true){
        int nex=cur;
        while(nex+1<lis.size()&&lis[nex+1]-lis[cur]<=D)nex++;
        if(cur==nex)break;
        cnt++;
        cur=nex;
    }

    cout<<lis[cur]<<" "<<cnt<<endl;
    return 0;
}

#include <cstdlib>
#include <iostream>
using namespace std;
int sum[501000];
int main()
{
    int maxs=0,i,j,n,k,d,s,cnt=0,sr;
    cin>>n>>d;
    sum[0]=1;
    for(i=1;i<=n;i++)
    {
        cin>>k;maxs+=k;
        for(j=maxs-k;j>=0;j--)
           if(sum[j])sum[j+k]=1;
    }
  //  for(i=1;i<=maxs;i++)cout<<sum[i]<<" ";
    sr=s=0;
    for(i=1;i<=maxs;i++)
    {
       if(sum[i])
       {
          if(i-sr<=d)
          {
             s=i-sr;
          }
          else
          {
             sr=sr+s;cnt++;
             if(i-sr<=d)
             {
                s=i-sr;
             }
             else break;
          }
        //  cout<<i<<" "<<s<<" "<<sr<<endl;
       }
    }
    if(s<=d){sr+=s;cnt++;}
    cout<<sr<<" "<<cnt<<endl;
    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
long c[10004];
int n,d;
long  x[500005];int z[600004];
int main(){

cin>>n>>d;long s=0;
for(int i=1;i<=n;i++){cin>>c[i];s=s+c[i];}
sort(c+1,c+n+1);
long h=0;long p=d;
    x[c[1]]=1;x[0]=1;
//cout<<s<<endl;
for(long i=2;i<=n;i++){
    for(long j=s;j>=c[i];j--){
        x[j]=x[j-c[i]];
    }}
    z[0]=1;
    for(long i=1;i<=s;i++){
        if(x[i]==1){z[i]=i;}
        else z[i]=z[i-1];
    }
    for(long i=s+1;i<=s+2*d;i++)z[i]=z[s];
int e=0;long l=0;
while(e==0){
    long f=z[p];
  //  cout<<f<<" "<<h<<" "<<p<<endl;
    if(f==h){e=1;}
    else{h=f;p=h+d;l++;}
}
cout<<h<<" "<<l<<endl;}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <stack>
#include <ctime>
#include <unordered_map>
#include <unordered_set>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 60;
const int inf = 1000000000;
int n, a[maxN], s[maxN], d;
int dp[maxN];

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d%d", &n, &d);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; ++i) {
		s[i] = a[i] + s[i - 1];
	}
	for (int i = 1; i <= n; ++i) {
		dp[i] = inf;

		for (int j = 1; j <= i; ++j) {
			int u = s[i] - s[j - 1];
			for (int k = 0; k < j; ++k) {
				int v = s[k];
				if (v + d >= u) {
					dp[i] = min(dp[i], dp[j - 1] + 1 + dp[k]);
				}
			}
		}
	}

	for (int i = n; i >= 0; --i) {
		if (dp[i] != inf) {
			int s = 0;
			for (int j = 1; j <= i; ++j) {
				s += a[j];
			}
			printf("%d %d\n", s, dp[i]);
			return 0;
		}
	}

	return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
	return x*f;
}
const int N=5.1e5+5;
int n,d;
int ans,last;
int f[N];
inline int check()
{
	last=ans;
	for(int i=last+d;i>=last;i--)
	{
		if(f[i])
		{
			ans=i;
			break;
		}
	}
	return ans==last;
}
int main()
{
	n=read(),d=read();
	int s=0;
	f[0]=1;
	for(int i=1;i<=n;i++)
	{
		int x=read();
		for(int j=0;j<=s;j++) f[j+x]|=f[j];
		s+=x;
	}
	int t=0;
	while(++t) if(check()) return printf("%d %d\n",ans,t-1),0;
	return 0;
}

#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define FOR(i, n) for(lo i = 0; i < (n); i++)
#define pi 3.14159265358979323
#define eps 1e-3
#define MN 1000000
#define SM 26
using namespace std;
const li INF = 2e9;

lo f[MN];
lo s[MN];
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#else
  //  freopen("redblack.in", "r", stdin);
  //  freopen("redblack.out", "w", stdout);
#endif
    lo n, d;
    cin >> n >> d;
    vector<lo> good;
    lo maxval = 0;
    FOR(i, n)
    {
        lo a;
        cin >> a;
        good.push_back(a);
        maxval += a;
    }
    maxval++;
    FOR(i, maxval)
    {
        s[i] = INF;
        f[i] = INF;
    }
    s[0] = 0;
    sort(good.begin(), good.end());
    FOR(i, n)
    {
        lo with = good[i];
        if(with <= d)
        {
            f[with] = 1;
        }
        for(lo i = max(0, with - d); i <= with; i++)
        {
            f[with] = min(f[with], s[i] + 1);
        }
        for(lo i = maxval; i - with >= 0; i--)
        {
            if(s[i - with] == INF)
                continue;
            if(i <= d)
                s[i] = 1;
            else
                 s[i] = min(s[i], f[with] + s[i - with]);
        }
    }
    lo maxi = 0, cnt = 0;
    FOR(i, maxval)
    {
        if(s[i] == INF)
            continue;
        maxi = i;
        cnt = s[i];
    }
    cout << maxi << " " << cnt;
}

//date 20140727
#include <cstdio>
#include <algorithm>
#include <cstring>

const int maxn = 55;
const int maxd = 600050;
using namespace std;

inline int getint()
{
	int ans(0); char w = getchar();
	while(w < '0' || '9' < w) w = getchar();
	while('0' <= w && w <= '9')
	{
		ans = ans * 10 + w - '0';
		w = getchar();
	}
	return ans;
}

int n, d, able, sum;

int cost[maxn], f[maxd];
int tf[maxd];

int main()
{
//	freopen("364b.in", "r", stdin);
//	freopen("364b.out", "w", stdout);

	n = getint(); d = getint();
	for(int i = 1; i <= n; ++i) cost[i] = getint();

	std::sort(cost + 1, cost + n + 1);
	for(int i = 1; i <= n; ++i)
		if(sum + d >= cost[i]) sum += cost[able = i];

	memset(f, 0, sizeof f);
	f[0] = 1;
	for(int i = 1; i <= able; ++i)
		for(int j = 0; j < 50000; ++j) if(f[j])
			f[j + cost[i]] = 1;

	for(int i = sum; i; --i)
	{
		for(int j = max(i - d, 0); j <= sum; ++j) if(f[j]){tf[i] = j; break;}
	}
	int asq = 0;
	for(int i = sum; i; i = tf[i]) ++asq;
	printf("%d %d\n", sum, asq);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

int combs[500000];

void main(){
	bool doLoop;
	int n, d, c[50], temp, days=0, val=0, oval=0, sum=0;

	memset(combs, 0, 500000*sizeof(int));
	combs[0] = 1;

	cin >> n >> d;
	for(int i=0 ; i<n ; i++) cin >> c[i];

	for(int i=0 ; i<n ; i++){
		combs[c[i]] = 1;
		for(int j=sum ; j>=0 ; j--)
			if(combs[j])
				combs[c[i]+j] = 1;
		sum+=c[i];
	}

	val = 0;
	for(int i=1 ; i<=sum ; i++){
		oval = val;

		while(i<=sum && i-oval <= d){
			if(combs[i])
				val = i;
			if(i+1 - oval <= d)
				i++;
			else
				break;
		}

		if(val == oval)
			break;
		else{
			oval = val;
			days++;
		}
	}

	cout << val << " " << days << endl;

}

#include <bits/stdc++.h>/*{{{*/
using namespace std;typedef long long ll;typedef long double ld;void run();int main(){ios::sync_with_stdio(0);run();}/*}}}*/

bool can[510001];

void run(){
  ll n,d; cin>>n>>d;

  can[0]=1;
  for (int i=0; i<n; i++){
    int v; cin>>v;
    for (int j=n*10000+1; j-->v;){
      can[j]|=can[j-v];
    }
  }

  ll res1=0;
  ll res2=0;
  for (int i=0,j=-1; i<=n*10000 and i<=res1+d; i++){
    if (can[i]) j=i;

    if (i==res1+d or i==n*10000){
      if (j==-1) break;
      res1=j;
      res2++;
      j=-1;
    }
  }

  cout<<res1<<" "<<res2<<endl;
}

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
int n,d;
int c[111];
int tot[111][111];
bool v[111];
int f[111][111];
int t,sum,ans1,k;
int ans2;
int main()
{
    scanf("%d%d",&n,&d);
    for (int i = 1; i <= n; i++) scanf("%d",&c[i]);
    sort(c + 1, c + 1 + n);
    memset(v,0,sizeof(v));
    memset(f,63,sizeof(f));
    sum = 0;
    for (int i = 1; i <= n; i++)
      if (c[i] - sum <= d)
      {
          v[i] = 1;
          sum += c[i];
      }
      else break;
    ans1 = sum;
    for (int i = 1; i <= n; i++) c[i] = -c[i];
    sort(c + 1, c+ n + 1);
    for (int i = 1; i <= n; i++) c[i] = -c[i];
    memset(tot,0,sizeof(tot));
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++) tot[i][j] = tot[i][j - 1] + c[j];
    for (int i = 1; i <= n; i++)
        if (c[i] <= d) f[i][i] = 1;
    for (int i = n; i >= 1; i--)
        for (int j = i; j <= n; j++)
          for (int k = i; k < j; k++)
          {
            if (tot[i][j] <= d) f[i][j] = min(f[i][j],1);
            if (tot[i][k] - tot[k + 1][j] <= d) f[i][j] = min(f[i][j],f[k + 1][j] * 2 + 1);
            if (tot[i][k] <= d) f[i][j] = min(f[i][j],f[k + 1][j] + f[i][k]);
          }
    //for (int i = 1; i <= n; i++)
   // for (int j = i; j <= n; j++) cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
    for (int i = 1; i <= n; i++)
      if (tot[i][n] == ans1)
    {
        ans2 = i; break;
    }
    printf("%d %d\n",ans1,f[ans2][n]);
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=60,maxv=100010;
bool f[maxv]; int g[maxv];
int v[maxn],totv;
int n,d;

void init(){
    cin>>n>>d;
    for(int i=1;i<=n;++i)
        cin>>v[i];
}

void dp(){
    totv=0; for(int i=1;i<=n;++i) totv+=v[i];
    memset(f,false,sizeof(f)); f[0]=true;
    for(int i=1;i<=n;++i){
        for(int j=totv;j>=v[i];--j)
            f[j]=f[j] || f[j-v[i]];
    }
    g[0]=0;
    for(int i=1;i<=totv;++i)
        if(f[i]) g[i]=i; else g[i]=g[i-1];
}

void solve(){
    int ans1=0,ans2=0;
    for(;;){
        if(g[ans1+d]==ans1) break;
        if(ans1+d>totv){ ans1=g[totv],++ans2; break; }
        ans1=g[ans1+d],++ans2;
        cout<<ans1<<endl;
    }
    cout<<ans1<<' '<<ans2<<endl;
}

int main(){
    init();
    dp();
    solve();
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

// The codes which I use are taken from the book Competitive Programming 3 written by Steven Halim
// Shortcuts for "common" data types in contests
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with it!
#define s(i) scanf("%d",&i)
#define sl(i) scanf("%ld",&i)
#define sll(i) scanf("%lld",&i)
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define NREP(i,a,b) \
for (int i = int(a); i >= int(b); i--)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 1000000000 // 2 billion

const int N = 500009;  // limit for array size
int n = 500001;  // array size
int t[2 * N] , pre[59] , m , d ;

void build() {  // build the tree
  for (int i = n - 1; i > 0; --i) t[i] = min( t[i<<1] , t[i<<1|1] );
}

void modify( int p , int value ) {  // set value at position p
  for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = min( t[p] , t[p^1] );
}

int query(int l, int r) {  // sum on interval [l, r)
  int res = INF;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = min( res , t[l++] );
    if (r&1) res = min( res , t[--r] );
  }
  return res;
}

void recurse( int idx , int cos , int time )
{
    if( idx == m )
        return ;
    REP( i , idx , m - 1 )
    {
        int sy = pre[i] - pre[idx - 1];
        int val = query( max( sy - d , 0 ) , 500000 + 1 );
        modify( cos + sy , min( time + 1 + val , query( cos + sy , cos + sy + 1 ) ) );
    }
    recurse( idx + 1 , cos + pre[idx] - pre[idx - 1] , query( cos + pre[idx] - pre[idx - 1] , cos + pre[idx] - pre[idx - 1] + 1 ) );
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    s(m);s(d);
    REP( i , 0 , m - 1 )
        s(pre[i]);
    sort( pre , pre + m );
    REP( i , 1 , m - 1 )
        pre[i] += pre[i - 1];
    REP( i , 0 , 1000002 )
        t[i] = INF;
    build();
    modify( 0 , 0 );
    recurse( 0 , 0 , 0 );
    int ans = -1 , time = -1;
    REP( i , 0 , 500000 )
    {
        if( query( i , i + 1 ) != INF )
        {
            ans  = i;
            time = query( i , i + 1 );
           // cout << ans << " " << time << endl;
        }
    }
    printf("%d %d\n" , ans , time );
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main()
{
    set<int> s;
    s.insert(0);

    int n,d;
    cin>>n>>d;
    vector<int> a(n);
    for(int i=0;i<n;i++)
     {
        cin>>a[i];
     }
     sort(a.begin(),a.end());
    for(int i=0;i<n;i++)
     {
        set<int> temp;
        for(set<int>::iterator it=s.begin();it!=s.end();it++)
         {
            if(s.find(*it+a[i])==s.end())
                temp.insert(*it + a[i]);
          }
        for(set<int>::iterator it=temp.begin();it!=temp.end();it++)
         {
            s.insert(*it);
          }

     }
     int prev=0;
     int c=0;
     vector<int> v;
         s.insert(INT_MAX);

    for(set<int>::iterator it=s.begin();it!=s.end();it++)
         {
            v.push_back(*it);
          }
    for(int i=0;i<v.size();i++)
    {   if(v[i]>prev+d)
         {
            break;
         }

        while(v[i]<=prev+d)
          i++;
        i--;

         prev=v[i];
         c++;

    }
    cout<<prev<<" "<<c<<endl;
}

#include<stdio.h>
#include<string.h>
char dp[51*10001];
int main(void){
    int i,n,d,sum,k,max,days;
    while(scanf("%d%d",&n,&d)!=EOF){
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        max=days=sum=0;
        while(n--){
            scanf("%d",&k);
            for(i=sum+=k;i>=k;i--)
            {

                dp[i]=dp[i-k];
            }
        }
        while(true){
            int j=max+d;
            while(dp[j]==0&&j>max)
                j--;
            if(j==max)
                break;
            max=j;
            days++;
        }
        printf("%d %d\n",max,days);
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int sign=1,sum=0;
	char c=getchar();
	while(!isdigit(c))
	{
		if (c=='-') sign=-1;
		c=getchar();
	}
	while(isdigit(c))
	{
		sum=(sum<<1)+(sum<<3)+c-'0';
		c=getchar();
	}
	return sign*sum;
}
#define read(x) x=read()
const int N=5e6+50,M=1e5+50;
int n,d;
int f[N],c[M];
int main()
{
	read(n),read(d);
	for (int i=1;i<=n;++i)
	  read(c[i]);
	f[0]=1;
	for (int i=1;i<=n;++i)
	  for (int j=M-1;j>=c[i];--j)
	    f[j]|=f[j-c[i]];
	int ans=0;
	int point=0;
	for (;point<M;++point)
	{
		int cost=ans+d;
		for (int j=cost;j>=ans;--j)
		{
			if (f[j])
			  {
			  	ans=j;
			  	break;
			  }
		}
		if (cost==ans+d) break;
	}
	printf("%d %d\n",ans,point);
}

#include <iostream>
#include <memory.h>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>

using namespace std;

#define x0 askjasf
#define y0 asfxnv
#define ll long long
#define rep(i,n) for(int i = 0; i < n; ++i)
#define forn(i,a,b) for(int i = a; i < b; ++i)

int n, d, s, a[33333], dp[555];
int f[55][55];
int sq[55];

int sum(int l, int r) {
    int s = sq[r];
    if (l > 0) s -= sq[l-1];
    return s;
}

int solve(int l, int r) {
    if (f[l][r] == -1) {
        int tmp = 1000000000;
        int qs = sum(l, r);
        if (qs <= d) {
            f[l][r] = 1;
            return 1;
        }
        for (int lr = 0; lr < l; ++lr)
            for (int rr = lr ; rr < l; ++rr) {
                int dS = sum(lr, rr);
                if (dS + d >= qs) {
                    tmp = min(tmp, solve(lr, rr) + 1);
                }
            }
        for (int i = l; i < r; ++i)
            tmp = min(tmp, solve(l, i) + solve(i + 1, r));
        f[l][r] = tmp;
    }
    return f[l][r];
}

int main(){
    ios::sync_with_stdio(false);
    cin >> n >> d;
    rep(i, n) cin >> a[i];
    sort(a, a+n);

    rep(i, n) {
        sq[i] = a[i];
        if (i > 0) sq[i] += sq[i-1];
        s += a[i];
    }
    rep(i, n+1)
        rep(j, n+1)
            f[i][j] = -1;
    for (int i = n - 1; i >= 0; --i){
        if (solve(0, i) < 100000000) {
            cout << sum(0, i) << " " << solve(0, i) << endl;
            return 0;
        }

    }

    cout << 0 << " " << 0 << endl;
    return 0;
 }

#include <bits/stdc++.h>

using namespace std;

#define out(x) cout << #x"=" << x << endl

const int N = 60;
const int L = 5e5 + 10;
const int INF = 1e9 + 10;

int n, d;
int c[N];
bool f[L];
int dp[L];
int mx;

int main() {
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; ++i) {
    scanf("%d", c + i);
  }
  sort(c, c + n);
  f[0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = L - 1; j >= 0; --j) {
      if (f[j]) f[j + c[i]] = 1, mx = max(mx, j + c[i]);
    }
  }
  fill(dp, dp + L, INF);
  dp[0] = 0;
  int j = 0;
  /*
     for (int i = 0; i < v.size(); ++i) {
     printf("%d ", v[i]);
     }
     puts("");*/
  for (int i = 0; i < L; ++i) {
    if (f[i]) {
      if (dp[i] >= INF) {
        mx = i;
      }
      for (; j - i <= d && j < L; ++j) {
        if (f[j]) {
          dp[j] = min(dp[j], dp[i] + 1);
        }
      }
    }
  }
  printf("%d %d\n", mx, dp[mx]);
  return 0;
}

/*
 * Author:  DreamHigh
 * Created Time:  2013/12/1 19:59:56
 * File Name: T.cpp
 * solve: T.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 501010;
int a[maxn];
int can[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,d;
    while(cin>>n>>d)
    {
        repf(i,1,n) scanf("%d",&a[i]);
        clr(can);
        can[0] = 1;
        repf(i,1,n)
        {
            repd(j,n*d,0)
            {
                if(can[j] && j + a[i] <= n*d)
                    can[j + a[i]] = 1;
            }
        }
        int sum = 0,num = 0;
        while(1)
        {
            int flag = 0;
            repd(i,sum + d,sum + 1)
            {
                if(can[i])
                {
                    sum = i;
                    num++;
                    flag = 1;
                    break;
                }
            }
            if(flag == 0)
                break;
        }
        cout<<sum<<" "<<num<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
	return x*f;
}
const int N=6e5+5;
int ans,t,n,d,f[N];
int main()
{
	n=read(),d=read();
	int s=0;
	f[0]=1;
	for(int i=1;i<=n;i++)
	{
		int x=read();
		for(int j=0;j<=s;j++) f[j+x]|=f[j];
		s+=x;
	}
	while(++t)
	{
		int p=0;
		for(int i=ans+d;i>ans;i--)
			if(f[i])
			{
				ans=i;
				p=1;
				break;
			}
		if(!p) return printf("%d %d\n",ans,t-1),0;
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=500005;
int dp[maxn];
vector<int>g;
int main()
{
    int i,j,n,d;
    cin>>n>>d;
    dp[0]=1;
    for(i=1;i<=n;i++){
        int m;
        scanf("%d",&m);
        for(j=maxn-1;j>=m;j--){
            if(dp[j-m]) dp[j]=1;
        }
    }
    for(i=maxn-1;i>=0;i--){
        if(dp[i]) g.push_back(i);
    }
    int s=0,w=0;
    while(g.size()){
        if(g[g.size()-1]-w>d) break;
        while(g.size()>1&&g[g.size()-2]<=w+d){
            g.pop_back();
        }
        w=g[g.size()-1];
        s++;
        g.pop_back();
    }
    cout<<w<<" "<<s<<endl;
}

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
const int N = 57;
const int W = 1e7;

int n, d, i, j, k, sum;
int c[N];
int in[N], is, out[N], acc[N], ns[N], os;

int cs, ans;
int dist[W], qs;
pair<int, int> q[W];

int main(){
    scanf(" %d%d", &n, &d);
    for(i=0;i<n;i++) scanf(" %d", c+i);
    sort(c,c+n);

    for(i=0;i<W;i++) dist[i] = 1e9;
    dist[0] = 0;
    q[qs++] = mp(0,0);
    while(qs){
        int s = q[0].second, w = -q[0].first, v = 0;
        pop_heap(q,q+(qs--));
        is = os = 0;
        for(i=0;i<n;i++)
            if(s&(1<<i)) in[is++] = i, v += c[i];
            else {
                if(os) ns[os] = (ns[os-1]|(1<<i)), acc[os] = acc[os-1] + c[i];
                else ns[os] = (1<<i), acc[os] = c[i];
                out[os++] = i;
            }
        if(dist[v] > w) continue;
        ans = max(ans, v);
        sum = 0;
        cs = s;
        for(i=0;i<=is;i++){
            j = upper_bound(acc, acc+os, sum+d) - acc - 1;
            if(j >= 0){
                if(dist[v-sum+acc[j]] > dist[v] + 1){
                    dist[v-sum+acc[j]] = dist[v] + 1;
                    q[qs++] = mp(-dist[v-sum+acc[j]], cs|ns[j]);
                    push_heap(q,q+qs);
                }
            }
            if(i < is) sum += c[in[i]], cs &= ~(1<<in[i]);
        }
    }
    printf("%d %d\n", ans, dist[ans]);
}

#include <iostream>
#include <set>

using namespace std;

int N, D;
bool poss[500050];

int main()
{
    for(int i=0;i<500050;i++) poss[i] = false;
    cin >> N >> D;
    poss[0] = true;
    for(int k=0;k<N;k++)
    {
        int c;
        cin >> c;
        for(int i=500049; i>=0; i--) poss[i] |= poss[i-c];
    }
    //for(int i=0;i<20;i++) cout << poss[i];  cout << endl;
    int p = 0, d = 0;
    while(1)
    {
        int latest = -1;
        for(int i = p+1; i <= p+D; i++)
        {
            if(poss[i]) latest = i;
        }
        if(latest == -1) break;
        p = latest;
        d++;
    }
    cout << p << ' ' << d;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define MOD
#define MAXN            50
#define MAX             (int)1e4
#define eps             1e-9
#define inf             1<<30
#define sz              size
#define pb              push_back
#define segsz(n)        1<<(int)ceil(log2(n-eps)+1+eps)
#define fo(i, n, b)     for (int i = b ; i < n ; i ++)
#define foo(i, n, b)    for (int i = n-1 ; i >= b ; i --)

typedef pair <int, int> pii;

int arr[MAXN+5];
bool Sum[MAX*MAXN+5];
vector<int > vec;

int main()
{
    #ifndef ONLINE_JUDGE
        //freopen ("in.in","r",stdin);
    #endif

    int N, D, ans = 0, maxi = 0, tot = 0;
    scanf("%d %d",&N,&D);
    for (int i = 0 ; i < N ; i ++){
        scanf("%d",&arr[i]);
        tot += arr[i];
    }
    Sum[0] = 1;
    for (int i = 0 ; i < N ; i ++){
        for (int j = tot-arr[i] ; j >= 0; j --)
            if (Sum[j] && !Sum[j+arr[i]]) {
                vec.push_back(j+arr[i]);
                Sum[j+arr[i]] = 1;
            }
    }
    sort(vec.begin(), vec.end());
    while (1){
        int low = lower_bound (vec.begin(), vec.end(), maxi+D) - vec.begin();
        if (low == (int)vec.size() || vec[low] > maxi+D) low --;
        if (vec[low] <= maxi) break;
        maxi = vec[low];
        ans ++;
    }

    printf("%d %d\n",maxi,ans);

    return 0;
}

#include <bits/stdtr1c++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef complex<ld> pt;

//cannot exchange if d < s(y)-s(x).
//i.e. permissible if s(y)-s(x) <= d.
//i.e. if the profit does not exceed some value.
//after exchanging an item, i can spend the same number of days
//and get the same set of items as one that ive exchanged.
//if i get a set of items, S in y days, i can get S again in 2y days.
//i want all the items i can get in the end, and it suffices to consider
//items in sorted order.

//first sample
//{}->{1}
//{1}->{2}
//{}->{1}
//no more exchanges possible.

//second sample
//{}->{2,3}
//{}->{1}

//is it optimal to be greedy? take the most value you can, trade the most value you can?
//or a dp[d][n] := max value i can get with first n items on day d?
//third sample
//{}->{1,9999}
//{1,9999}->{10000,10000}
//{}->{1,9999}
//{10000}->{10000,10000}
//{10000}->{10000,10000}
//{1,2,3,4}

//i think greedy w/ knapsack each day is optimal.
//suppose not, i.e. you can act ungreedy for some day, and get a less than optimal set
//then use part of that set to get even bigger gains the next day.
//hmm... not too obvious to prove.

//ok, well we could just try it.
//d=6
//1 2 4 5 7
//{}->{2,4}
//{2,4}->{5,7}
//{}->{2,4}
//{1}.

//{}->{1,2}
//{1,2}->{4}
//{}->{1,2}
//{1,2,4}->{5,7}

int n, d;
int a[55], sum[55];
int memo[55][55];

const int INF = 100000;
int dp(int i, int j) {
    if (memo[i][j] != -1) return memo[i][j];
    //cout << "Yo " << endl;
    if ((i == 0 ? sum[j] : sum[j]-sum[i-1]) <= d) return 1;

    //cout << i << " " << j << endl;
    int ans = INF;
    if (i == 0) {
        for (int k = 0; k < j; k++) {
            ans = min(ans, dp(i,k)+dp(k+1,j));
        }
    } else {
        for (int r = 0; r < i; r++) {
            for (int s = r; s < i; s++) {
                int S = (r == 0 ? sum[s] : sum[s]-sum[r-1]);
                int T = sum[j]-sum[i-1];
                if (S+d >= T) {
                    //cout << "(" << r << "," << s << "): " << S << " " << T << endl;
                    //cout << "dp(r,s): " << dp(r,s) << endl;
                    ans = min(ans, 1+dp(r,s));
                    //cout << ans << endl;
                }
            }
        }
    }
    //cout << i << " " << j << " " << ans << endl;
    memo[i][j] = ans;
    return memo[i][j];
}

//dp(0,2) = dp(2,2)
int main() {
    memset(memo, -1, sizeof memo);

    cin >> n >> d;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a+n);
    partial_sum(a, a+n, sum);

    if (a[0] > d) {
        cout << 0 << " " << 0 << endl;
        return 0;
    }
    int j = n;
    while (j--) {
        //cout << "yo " << endl;
        if (dp(0, j) < INF) {
            cout << sum[j] << " " << dp(0,j) << endl;
            break;
        }
    }
    return 0;
}

#include<cstdio>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<cstring>
#include<string>
#include<numeric>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<bitset>
#include<cassert>
using namespace std;
typedef pair<int,int> PII;
#define pi acos(-1)
#define INF 100000000
#define sqr(x) ((x)*(x))
#define debug(x) cerr<<#x"="<<x<<endl;
#define foreach(it,v) for (__typeof(v.begin()) it=v.begin();it!=v.end();it++)

long long a[100],s[100],f[100],v[100];
long long n,d;
pair<long long,long long> ans,tmp;

int main() {
    int i,j,k;
    cin>>n>>d;
    for (i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+n+1);
    for (i=1;i<=n;i++) s[i]=s[i-1]+a[i];
    v[0]=1;
    f[0]=0;
    for (i=1;i<=n;i++) {
        for (j=0;j<i;j++) if (v[j])
            for (k=0;k<i;k++) if (v[k] && s[k]+d>=s[i]-s[j])
                if (!v[i] || f[k]+f[j]+1<f[i]) {
                    f[i]=f[k]+f[j]+1;
                    v[i]=1;
                }
        if (d>=s[i])
            if (!v[i] || 1<f[i]) {
                f[i]=1;
                v[i]=1;
            }
        if (v[i]) {
            tmp.first=s[i];
            tmp.second=f[i];
            ans=max(ans,tmp);
        }
//      debug(f[i]);
    }
    cout<<ans.first<<" "<<ans.second<<"\n";
}

#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)

vector<int> primes;
set<int> res;
int n;

const int SZ=100000;

int sito[SZ];

void solve(int val, int cur)
{
	if(cur<0) return;
	if(val>2*n*n) return;
	res.insert(val);
	solve(val*primes[cur],cur);
	solve(val,cur-1);
}

int main()
{
	for(int i=2;i*i<=SZ;i++)
		if(!sito[i])
			for(int j=i*i;j<=SZ;j+=i)
				sito[j]=1;
	int ptr=2;
	scanf("%d",&n);
	while(res.size()<n)
	{
		while(sito[ptr]) ptr++;
		primes.push_back(ptr);
		solve(ptr,primes.size()-1);
		ptr++;
	}
	for(set<int>::iterator it=res.begin();it!=res.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
	return 0;
}

#include <bits/stdc++.h>/*{{{*/
using namespace std;typedef long long ll;typedef long double ld;void run();int main(){ios::sync_with_stdio(0);run();}/*}}}*/

bool can[510001];

void run(){
  ll n,d; cin>>n>>d;

  can[0]=1;
  for (int i=0; i<n; i++){
    ll v; cin>>v;
    for (int j=n*10000+1-v; j-->0;){
      can[j+v]|=can[j];
    }
  }

  ll res1=0;
  ll res2=0;
  for (int i=0,j=-1; i<=n*10000; i++){
    if (can[i]) j=i;

    if (i==res1+d or i==n*10000){
      if (j==-1) break;
      res1=j;
      res2++;
      j=-1;
    }
  }

  cout<<res1<<" "<<res2<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
const int maxn = 52;
const int maxsum = 52*10000;

int n,d;
int A[maxn];
int works[maxsum];

int32_t main() {
	FAST
	cin >> n >> d;
	for (int i =0;i<n;i++) {
		cin >> A[i];
	}

	works[0] = 1;
	for (int i =0;i<n;i++) {
		for (int j =maxsum; j >= 1;j--) {
			if (works[j - A[i]]) works[j] = 1;
		}
	}

	vector <int> v;
	for (int j=0;j<maxsum;j++) {
		//if (works[j]) cout << j << "\n";
		if (works[j]) v.push_back(j);
	}

	int sum1 = 0, sum2 = 0;
	int days = 0;
	while (true) {
		sum2 = *(--upper_bound(all(v),sum1+d));
		if (sum2 == sum1) break;
		swap(sum1,sum2);
		days++;
	}
	cout << sum1 << " " << days << "\n";
}

#ifdef _MONYURA_
#pragma comment(linker,"/STACK:256000000")
#endif

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <bitset>
#include <memory>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <cstring>
#include <fstream>
#include <functional>
#include <stack>
#include <complex>
#include <wchar.h>
#include <wctype.h>
#include <cmath>
#include <queue>
#include <ctime>
#include <numeric>

using namespace std;

template<typename T> T mabs(const T &a){ return a<0?-a:a;}
#define rep(x,y,z) for(int x=(y),e##x=(z);x<e##x;x++)
#define SQR(x) ((x)*(x))
#define all(c) (c).begin(), (c).end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int SZ = 51000;
int D[51][SZ];
void run()
{
    int n,d;
    cin>>n>>d;
    int A[100];
    rep(i,0,n)
       scanf("%d",A+i);
    sort(A,A+n);
    int sum = 0;
    int to = n;
    rep(i,0,n)
    {
        if (sum + d < A[i])
        {
            to = i;
            break;
        }
        sum += A[i];
    }
    const int INF = 1e9;
    rep(i,0,to+1)
    {
        rep(j,0,SZ)
        D[i][j] = INF;
    }
    D[0][0] = 0;
    int canGet[51]={0};
    rep(i,0,to+1)
    canGet[i] = INF;
    canGet[0] = 0;
    rep(i,1,to+1)
    {
        int cost = 0;
        int here = INF;
        for (int j = i;j>0;j--)
        {
            int mn = INF;
            cost += A[j-1];
            rep(s,0,SZ)
            if (s + d >= cost)
                mn = min(mn, D[j-1][s]);
            mn = canGet[j-1] + mn + 1;
            here = min(here,mn);
            rep(s,0,SZ)
            {
                D[i][s] = min(D[i][s],D[i-1][s]);
                if (s + cost < SZ)
                    D[i][s+cost] = min(D[i][s+cost], D[j-1][s] + mn);
            }
        }
        canGet[i] = here;
    }
    cout<<sum<<' '<<canGet[to]<<endl;

}

//#define prob "kids"

int main()
{
#ifdef _MONYURA_
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    time_t st=clock();
#else
#ifdef prob
    freopen(prob".in","r",stdin);
    freopen(prob".out","w",stdout);
#endif
#endif
    run();
#ifdef _MONYURA_
    printf( "\n=============\n");
    printf("Time: %.2lf sec\n",(clock()-st)/double(CLOCKS_PER_SEC));
#endif

    return 0;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll __int64
#include<iostream>
using namespace std ;

const int maxn = 1000001 ;
int dp[55] , a[maxn] , sum[maxn] ;
const int INF = 111111111 ;

int main () {
    int n , m , i , j , k , d ;
    scanf ( "%d%d" , &n , &d ) ;
    m = 0 ;
    for ( i = 1 ; i<= n ; i ++ ) {
        scanf ( "%d" , &a[i] ) ;
        m += a[i] ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    for ( i = 1 ; i <= n ; i ++ ) sum[i] = sum[i-1] + a[i] ;
    for ( i = 0 ; i <= n ; i ++ ) dp[i] = INF ;
    dp[0] = 0 ;
    for ( i = 1 ; i <= n ; i ++ )
        for ( j = 0 ; j < i ; j ++ )
            for ( k = 0 ; k <= j ; k ++ ) {
                if ( sum[k] + d >= sum[i] - sum[j] ) {
                    dp[i] = min ( dp[i] , dp[j] + dp[k] + 1 ) ;
              //      printf ( "fuck dp[%d] = %d\n" , j , dp[j] ) ;
             //       printf ( "fuck dp[%d] = %d\n" , k , dp[k] ) ;
             //       printf ( "dp[%d] = %d\n" , i , dp[i] ) ;
                }
            }
    for ( i = n ; i >= 0 ; i -- ) {
        if ( dp[i] != INF ) {
            printf ( "%d %d\n" , sum[i] , dp[i] ) ;
            break ;
        }
    }
    return 0 ;
}

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
int main()
{

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 57;
const int K = 500007;

int n, d;
int c[N];

int tim[K];
int qtd[K];
multiset<int> s;

int main () {
    scanf("%d %d", &n, &d);

    for (int i = 0; i < n; i++)
        scanf("%d", c+i);

    qtd[0] = -1;
    tim[0] = 0;
    s.insert(0);

    for (int i = 1; i < K; i++) {
        if (i-d-1 >= 0 && tim[i-d-1] != -1)
            s.erase(s.find(tim[i-d-1]));
        if (s.empty()) {
            printf("%d %d\n", i-d-1, tim[i-d-1]);
            return 0;
        }

        qtd[i] = n;
        for (int j = n-1; j >= 0; j--) {
            if (i >= c[j] && qtd[i-c[j]] < j)
                qtd[i] = j;
        }

        if (qtd[i] == n) {
            tim[i] = -1;
        } else {
            tim[i] = *(s.begin()) + 1;
            s.insert(tim[i]);
        }
    }
}

//Written by Zhu Zeqi
//Come on,baby
//Hack,please
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define vi vector<int>
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 1000000000
typedef long long ll;
using namespace std;
int a[55],n,d,t;
bool dp[2][500005],vis[500005];
vector<int> vec;
void cal(pair<int,int> &a,pair<int,int> b){
    if(a.first<b.first)
	a=b;
    else if(a.first==b.first && a.second>b.second)
	a=b;
}
pair<int,int> DP[50005];
pair<int,int> sol(int now){
	if(now==vec.size())
	return pair<int,int> (-1,100);
    pair<int,int> &res=DP[now];
    if(vis[now])
	return res;
    vis[now]=1;
    res=make_pair(-1,100);
    int loc=now+1;
	int high=vec.size();
    int solve=-INF;
    while(loc<=high){
        int mid=(loc+high)/2;
        if(vec[mid]<=vec[now]+d){
        	loc=mid+1;
			solve=max(solve,mid);
		}
        else
		high=mid-1;
    }
    if(solve>-INF)
    for(int j=now+1;j<vec.size();j++){
        if(vec[j]-vec[now]>d)
		break;
        cal(res,pair<int,int>(vec[j],1));
        pair<int,int> temp=sol(j);
        temp.second++;
        cal(res,temp);
    }
    //cout<<vec[now]<<"="<<res.first<<","<<res.second<<endl;
    return res;
}
int main(){
	//freopen("input.in","r",stdin);
	//freopen("output.out","w",stdout);
    cin>>n>>d;
    for(int i=0;i<n;i++){
        cin>>a[i+1];
        t+=a[i+1];
    }
    dp[0][0]=1;
    bool h=0,p=1;
    for(int i=1;i<=n;i++){
        h=!h,p=!p;
        for(int j=0;j<=t;j++){
            dp[h][j]=dp[p][j];
            if(j>=a[i])
			dp[h][j]|=dp[p][j-a[i]];
        }
    }
    for(int j=0;j<=t;j++)
	if(dp[h][j])
	vec.push_back(j);
    for(int j=vec.size()-1;j>=0;j--)
	sol(j);
    cout<<sol(0).first<<" "<<sol(0).second<<endl;
	//system("pause");
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
#include<map>
#include<string>
#include<vector>
#include<set>

using namespace std;
#define For(i,l,r) for (int i = l; i <= r; ++i)
#define Cor(i,l,r) for (int i = l; i >= r; --i)
#define Fill(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define MP make_pair
#define PII pair<int,int>
#define flt double
#define INF (0x3f3f3f3f)
#define MaxN 1020304
#define MaxNode 1020304
#define MD 1000000007

int n,d,s[MaxN],a[MaxN],f[MaxN],dis[MaxN];
int main() {
//  freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    cin >> n >> d;
    int Sum = 0;
    For(i,1,n) scanf("%d",&a[i]), Sum += a[i];
    Fill(f,0); f[0] = 1;
    For(i,1,n) Cor(j,Sum,a[i]) f[j] |= f[j - a[i]];
    int last = 0;
    Fill(dis,-1); dis[0] = 0;
    For(i,1,Sum) if (f[i]) {
        int newLast = -1;
        For(j,last,i) if (f[j] && i - j <= d) {
            newLast = j;
            break ;
        }
        last = newLast;
        if (last != -1) dis[i] = dis[last] + 1;
        if (last == -1) {
            break ;
        }
    }
    Cor(i,Sum,0) if (dis[i] != -1) {
        cout << i << ' ' << dis[i] << endl;
        break ;
    }
    /*For(i,0,Sum + 100) f[i] = INF; f[0] = 0;
    memcpy(g,f,sizeof(f));
    sort(a + 1,a + n + 1);
    int ans1 = 0, ans2 = 0;
    For(i,1,n) {
        if (getProf(a[i]) == INF) break;
        int days = getProf(a[i]) + 1;
        ans1 += a[i]; ans2 += days;
        Cor(j,Sum,a[i]) f[j] = min(f[j],f[j - a[i]] + days);
        Cor(j,Sum - 1,0) g[j] = min(g[j + 1],f[j]);
    }
    cout << ans1 << ' ' << ans2 << endl;*/

    return 0;
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;

int n;
int d;

#define MAX 50

vector<int> v;

#define MAXX 500002
bool flag[MAXX];
vector<int> vv;

int main(){
	cin >> n >> d;
	for (int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		v.push_back(a);
	}
	sort(v.begin(), v.end());
	if (d<v[0]){
		puts("0 0");
		return 0;
	}
	flag[0] = true;
	int mx = 0;
	for (int i = 0; i < v.size(); i++){
		for (int j = mx; j >= 0; j--){
			flag[j + v[i]] = true;
		}
		mx += v[i];
	}
	for (int i = 0; i <= mx; i++){
		if (flag[i]){
			vv.push_back(i);
		}
	}
	int cur = 0;
	int cnt = 0;
	while (1){
		int id = upper_bound(vv.begin(), vv.end(), cur + d) - vv.begin();
		id--;
		if (id>=0&&cur < vv[id]){
			cur = vv[id];
			cnt++;
		}
		else{
			break;
		}
	}
	cout <<cur<<" "<< cnt << endl;
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define S second
#define F first

using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pii;
const int maxn = 1e5 + 10;
const int mod  = 1e9  + 7;
int dp[52][maxn];
int seq[maxn];
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(false);
    int n,d;
    cin >>n >> d;
    dp[0][0] = 1;
    for (int i = 1;i <= n;i++) cin >> seq[i];
    for (int i = 1;i <= n;i++){
        for (int j = 0;j < maxn;j++){
            dp[i][j] = dp[i - 1][j];
            if (j - seq[i] >= 0) dp[i][j] |= dp[i - 1][j - seq[i]];
        }
    }
    int cur = 0;
    vector<int> v;
    for (int j = 0;j < maxn;j++){
        if (dp[n][j]) v.pb(j);
    }
    int days = 0;
    int l = -1;
    for (int i = 0;i < v.size();i++){
        if (v[i] - cur > d){
            if (i - 1 == l) break;
            days++;
            cur = v[i - 1];
            l = i - 1;
            i--;
        }
    }
    if (v[v.size()-  1] - cur <= d){
        cur  = v[v.size() - 1];
        days++;
    }
    cout << cur << ' ' << days << endl;
    return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
int dp[555555];	int map[111111];
int main()
{
int n,d,i,j;

dp[0]=1;

scanf("%d%d",&n,&d);
int sum=0;
for(i=1;i<=n;i++)
  {
   scanf("%d",&map[i]);
    sum+=map[i];
  }
for(i=1;i<=sum;i++)
dp[i]=111111;
sort(map,map+n);

for(i=1;i<=n;i++)
for(j=sum;j>=0;j--)
  {
	if(j+map[i]<=dp[j]*d&&dp[j]!=111111)
	dp[j+map[i]]=min(dp[j],dp[j+map[i]]);
	else if(j+map[i]<=dp[j]*d+d&&dp[j]!=111111&&map[i]<=d)
		dp[j+map[i]]=min(dp[j]+1,dp[j+map[i]]);
		else if(j+d>=map[i]&&dp[j]!=111111)
		{
		dp[map[i]]=min(dp[j]+1,dp[map[i]]);
		dp[map[i]+j]=min(dp[j]*2+1,dp[map[i]+j]);
		}

  }

  for(i=sum;i>=0;i--)
  if(dp[i]!=111111)
  {
  	printf("%d %d\n",i,dp[i]);
  	break;
  }
}

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>

using namespace std;

const int MAXN = 55;

int n;
int d;
int a[MAXN];
int sum = 0;
int ans;
long long t;
int dp[10500];
int can[10500][55];
int go;
bool used[MAXN];

int main() {
    //assert(freopen("input.txt","r",stdin));
    //assert(freopen("output.txt","w",stdout));

    scanf("%d %d", &n, &d);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);

    sort(a + 1, a + n + 1);

    go = n + 1;
    for (int i = 1; i <= n; i++) {
        if (a[i] > sum + d) {
            go = i;
            break;
        }
        sum += a[i];
        ans += a[i];
    }

    //cout << ans;

    for (int i = 1; i <= n; i++) {
        memset(can, 0, sizeof(can));
        for (int j = 1; j <= n; j++) {
            int w = a[j];
            for (int k = d; k >= 0; k--) {
                can[k][j] = can[k][j - 1];
                if (used[j])
                    continue;
                if (w > d)
                    continue;
                if (k == w || dp[k - w] == i) {
                    dp[k] = i;
                    can[k][j] = j;
                }
            }
        }
        for (int j = d; j >= 1; j--)
            if (dp[j] == i) {
                int w = j;
                //ans += w;
                int lst = n + 1;
                while (w > 0) {
                    while (can[w][lst - 1] == 0)
                        lst--;
                    used[can[w][lst - 1]] = true;
                    lst = can[w][lst - 1];
                    w -= a[lst];
                }
                t++;
                break;
            }
    }

    int sum = 0;
    for (int i = 1; i < go; i++) {
        if (!used[i]) {
            t += t + 1;
        }
        sum += a[i];
    }

    cout << ans << " " << t << endl;

    return 0;
}

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <deque>
using namespace std;

#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rep( i , a , b )  for( int i = (a) ;   i <= (b) ; ++i)
#define foreach(c , itr) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#define SZ(x) (int)x.size()
#define LEN(x) (int)x.length()

#define two(x) ((1LL)<<(x))
typedef long long int64 ;

const int64 oo = 1LL<<60 ;
int data[ 55 ] ;
int sum[ 55 ];
int64 dp[ 55 ];
int main(){
    int n,d;
    //int data[ 55 ];
    while( cin >> n >>d){
        //dp[ 0 ] = 0;
        for(int i=1; i <= n ; ++i){
            cin >> data[ i ] ;
        }
        sum[ 0 ] = 0 ;
        sort(data + 1 , data + 1 + n ) ;
        for(int i=1; i <= n ; ++i){
            sum [ i ] = sum[ i - 1] + data[ i ];
        }
        //memset( dp, 0x3f, sizeof(dp)) ;
        for(int i=1; i <= n ; ++i)
            dp[i]=oo;
        dp[ 0 ] = 0 ;
        for(int i=1; i <= n ; ++i){
            for(int ii=i; ii>= 0 ; --ii){
            for(int j=ii-1; j >= 0  ; --j){

                 if( sum[i]-sum[ii-1] - sum[j] <= d ){
                     dp[ i ] = min(dp[i], 1 + dp[j]+dp[ii-1]);
                 }
                // else dp[ i ] = min(dp[i], 1 + dp[ ii -1] ) ;
            }
            }
        }
        int64 x = 0, y = 0 ;
        for(int i=1; i <= n ; ++i){
            if( dp[i] < oo){

                x = sum[i] ;
                y = dp[ i ] ;
            }
        }
        cout << x<<" " << y << endl ;
    }
    return 0;
}

#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define FOR(i, n) for(lo i = 0; i < (n); i++)
#define pi 3.14159265358979323
#define eps 1e-3
#define MN 1000000
#define SM 26
using namespace std;
const li INF = 2e15;

li f[MN];
li s[MN];
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#else
  //  freopen("redblack.in", "r", stdin);
  //  freopen("redblack.out", "w", stdout);
#endif
    lo n, d;
    cin >> n >> d;
    //n = 50;
    //d = 10000;
    vector<lo> good;
    lo maxval = 0;
    FOR(i, n)
    {
        lo a;
        cin >> a;
        //a = 10000;
        good.push_back(a);
        maxval += a;
    }
    maxval++;
    FOR(i, maxval)
    {
        s[i] = INF;
        f[i] = INF;
    }
    s[0] = 0;
    sort(good.begin(), good.end());
    FOR(i, n)
    {
        lo with = good[i];
        if(with <= d)
        {
            f[with] = 1;
        }
        for(lo i = max(0, with - d); i < maxval; i++)
        {
            if(s[i] == INF)
                continue;
            f[with] = min(f[with], s[i] + 1);
        }
        for(lo i = maxval - 1; i - with >= 0; i--)
        {
            if(s[i - with] == INF)
                continue;
            if(i <= d)
                s[i] = 1;
            else
                 s[i] = min(s[i], f[with] + s[i - with]);
        }
        for(lo i = with; i <= with + d; i++)
        {
            f[i] = min(f[i], f[with] + 1);
           // s[i] = min(s[i], f[with] + 1);
        }
    }
    lo maxi = 0, cnt = 0;
    FOR(i, maxval)
    {
        if(s[i] == INF)
            continue;
        maxi = i;
        cnt = s[i];
    }
    cout << maxi << " " << cnt;
}

#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 500050;

bool f[N + 50];
int dt[N], n, d, rgmax[N];

int main ()
{
	scanf("%d%d", &n, &d);
	f[0] = 1;
	for (int i = 1; i <= n; ++i)
	{
		int a;
		scanf("%d", &a);
		for (int j = N - a; j >= 0; --j)
			if (f[j]) f[j + a] = true;
	}
	for (int j = 0; j < N; ++j)
		if (f[j]) rgmax[j / d] = j;
	int ans = 0;
	while (rgmax[ans + 1]) ++ans;
	ans = rgmax[ans];
	printf("%d %d\n", ans, ans == 0 ? 0 : ans / d + 1);
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define fr(i,a,b) for(int i = a; i < b; ++i)
#define rep(i, n) fr(i,0,n)
#define N 505000

bool pd[N];
int n, d;
int c[100], aux[N];

int main(){
    memset(pd, 0, sizeof pd);
    scanf("%d%d", &n, &d);
    pd[0] = true;
    rep(i, n) scanf("%d", c + i);

    rep(i, n) for(int j = N - 1; j >= c[i]; --j){
        pd[j] |= pd[j-c[i]];
    }
    int a = 0, last = 0, dias = 0;

    rep(i, N) if(pd[i]) aux[a++] = i;
    int i = 0;

    while(i < a){
        int x = i;
        while(i < a && aux[i] - last <= d) ++i;
        if(x == i) break;
        if(i) last = aux[i-1], ++dias;
    }

    printf("%d %d\n", last, dias);

}