codeforces div3 501 C. Songs Compression

C. Songs Compression

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan has n

songs on his phone. The size of the i-th song is ai bytes. Ivan also has a flash drive which can hold at most m

bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all n

songs to the flash drive. He can compress the songs. If he compresses the i-th song, the size of the i-th song reduces from ai to bi bytes (bi<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 m

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

).

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 n

and m (1≤n≤105,1≤m≤109

) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next n

lines contain two integers each: the i-th line contains two integers ai and bi (1≤ai,bi≤109, ai>bi) — the initial size of the i-th song and the size of the i

-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

Copy

4 21
10 8
7 4
3 1
5 4

Output

Copy

2

Input

Copy

4 16
10 8
7 4
3 1
5 4

Output

Copy

-1

Note

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8+7+1+5=21≤21

. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8+4+3+5=20≤21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10+4+3+5=22>21

).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8+4+1+4=17>16

.

很简单的一个题目, 然而由于条件写的不对无情的wa了。

题目意思就是给定一个容器值, 然后往里面放东西, 求被压缩的歌曲数目。如果全被压缩还是都放不进去那么输出-1。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct song
{
    int s,e;
    int incr;
};
song s[100005];
int compare(const void *a,const void *b)
{
    song *pa=(song *)a;
    song *pb=(song *)b;
    if(pa->incr<pb->incr)
        return 1;
    else
        return -1;
}
int n,m;
int main()
{
    long long int sum1=0,sum2=0;
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++)
    {
        scanf("%d%d",&s[i].s,&s[i].e);
        s[i].incr=s[i].s-s[i].e;
        sum1+=s[i].s;
        sum2+=s[i].e;
    }
    long long int cha=sum1-m;
    //printf("%d\n",cha);
    if(sum1<=m)
        printf("0\n");
    else if(sum1>m&&sum2>m)
    {
        printf("-1\n");
    }
    else if(sum1>m&&sum2==m)
        printf("%d\n",n);
    else if(sum1>m)
    {
        qsort (s,n,sizeof(s[0]),compare);
        /*for (int i=0;i<n;i++)
            printf("%d\n",s[i].incr);*/
        int num=0;
        while (cha>0)
        {
            cha-=s[num++].incr;
        }
        printf("%d\n",num);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81407353