Songs Compression

Ivan has nn songs on his phone. The size of the ii-th song is aiai bytes. Ivan also has a flash drive which can hold at most mm bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all nn songs to the flash drive. He can compress the songs. If he compresses the ii-th song, the size of the ii-th song reduces from aiai to bibi bytes (bi<aibi<ai).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most mm. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to mm).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.

Input

The first line of the input contains two integers nn and mm (1≤n≤105,1≤m≤1091≤n≤105,1≤m≤109) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next nn lines contain two integers each: the ii-th line contains two integers aiaiand bibi (1≤ai,bi≤1091≤ai,bi≤109, ai>biai>bi) — the initial size of the ii-th song and the size of the ii-th song after compression.

Output

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.

Examples

Input

4 21
10 8
7 4
3 1
5 4

Output

2

Input

4 16
10 8
7 4
3 1
5 4

Output

-1

下午把限定条件写错了,结果没写出来。这道题要求出每一个的压缩前和压缩后之差,按从大到小排序,用原来的和和m之差贪心,数据范围注意

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m;
struct node
{
    int a,b,c;
}p[100005];
bool cmp(node a,node b)
{
    return a.c>b.c;
}
int main()
{while(~scanf("%d%d",&n,&m))
{
    long long ans=0,sum=0,m1=0;
    for(int i=1;i<=n;i++)
    {scanf("%d%d",&p[i].a,&p[i].b);
    p[i].c=p[i].a-p[i].b;
    }
    sort(p+1,p+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        m1+=p[i].a;

}
    sum=m1-m;

for(int i=1;i<=n;i++)
{
 if(sum>0)
 {
     sum-=p[i].c;
     ans++;
 }
 else
 break;

}
if(sum>0)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/81367411