fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. //int t;cin>>t;
  5. //while(t--){
  6. long long n,m;cin>>n>>m;
  7. long long a[n*2];
  8. for (int i=0;i<2*n;i++){
  9. cin>>a[i];
  10. }
  11. long long totsum=0;
  12. long long res=0;
  13. for (int i=0;i<2*n;i+=2){
  14. totsum+=a[i];
  15. }
  16. long long k=0;
  17. long long b[n];
  18. long long x=0;
  19. while (x<n){
  20. b[x]=abs(a[k]-a[k+1]);
  21. k+=2;
  22. x++;
  23. }
  24. sort(b,b+n);
  25. reverse(b,b+n);
  26. if (totsum<=m){
  27. cout << 0;
  28. return 0;
  29. }
  30. else if (totsum>m){
  31. for (int i=0;i<n;i++){
  32. totsum-=b[i];
  33. res++;
  34. if (totsum<=m){
  35. cout << res ;
  36. return 0;
  37. }
  38. }
  39. }
  40. cout <<-1;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Standard output is empty