1. 程式人生 > >Codeforces Round #501 (Div. 3) C(貪心)

Codeforces Round #501 (Div. 3) C(貪心)

C. Songs Compression

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aiai and 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

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≤218+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≤218+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>2110+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>168+4+1+4=17>16.

先判斷不壓縮能否放下跟全部壓縮是否能放下

對ai-bi排序,從大往小選,這樣就可以得到最小的壓縮數量了。

#include<bits/stdc++.h>
#define mp make_pair
#define fir first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const int maxn=2e5+10;
const ll mod=1e9+7;
const int maxm=1e6+10;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
const double pi = acos (-1.0);
int n;
ll m,sum1,sum2;
pair<int,int> p[maxn];
vector<int> v;
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        cin>>p[i].fir>>p[i].se;
    for (int i=1;i<=n;i++){
        sum1+=p[i].fir;
        sum2+=p[i].se;
    }
    if (sum2>m){
        cout<<-1<<endl;
        return 0;
    }
    for (int i=1;i<=n;i++)
        v.pb(p[i].fir-p[i].se);
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    ll dif=sum1-m;
    int num=0;
    ll sum=0;
    for (int i=0;i<v.size();i++){
        if (sum>=dif) break;
        num++;
        sum+=v[i];
    }
    cout<<num<<endl;
    return 0;
}