CodeForces C. Songs Compression

來源:互聯網
上載者:User

標籤:oar   element   har   boa   nbsp   ace   codeforce   ber   city   

http://codeforces.com/contest/1015/problem/C

 

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.

ExamplesinputCopy
4 21
10 8
7 4
3 1
5 4
outputCopy
2
inputCopy
4 16
10 8
7 4
3 1
5 4
outputCopy
-1

代碼:

#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 10;int N, M;long long sum1 = 0, sum2 = 0;struct Node{    int s;    int e;    int t;}node[maxn];bool cmp(const Node& a, const Node& b) {    return a.t > b.t;}int main() {    scanf("%d%d", &N, &M);    for(int i = 1; i <= N; i ++) {        scanf("%d%d", &node[i].s, &node[i].e);        sum1 += node[i].e;        sum2 += node[i].s;        node[i].t = node[i].s - node[i].e;    }    if(sum1 > M) {        printf("-1\n");        return 0;    }    if(sum2 <= M) {        printf("0\n");        return 0;    }    int ans = 0;    sort(node + 1, node + 1 + N, cmp);    for(int i = 1; i <= N; i ++) {        sum2 -= node[i].t;        ans ++;        if(sum2 <= M)            break;    }    printf("%d\n", ans);    return 0;}

  

CodeForces C. Songs Compression

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.