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

C++版本一

简单排序

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG

using namespace std;
typedef long long ll;
const int N=100000+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m;
int c[N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&m);
    int a,b;
    ll suma=0;
    ll sumb=0;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a,&b);
        c[i]=a-b;
        suma+=a;
        sumb+=b;
    }
    if(sumb>m){
        cout << -1 << endl;
        return 0;
    }
    if(suma<=m){
        cout << 0 << endl;
        return 0;
    }
    sort(c+1,c+n+1);
    int ans=n;
    for(int i=1;i<=n;i++){
        if(sumb+c[i]>m){
            ans=i;
            break;
        }else{
            sumb+=c[i];
        }
    }
    cout << n-ans+1 << endl;
    //cout << "Hello world!" << endl;
    return 0;
}