c--歌曲压缩 C - Songs Compression

这题只要了解sort这个函数就ok;

其余的就无了

Ivan has nn songs on his phone. The size of the ii-th song is a_iai​ 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 a_iai​ to b_ibi​ bytes (b_i < a_ibi​<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 \le n \le 10^5, 1 \le m \le 10^91≤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 a_iai​ and b_ibi​ (1 \le a_i, b_i \le 10^91≤ai​,bi​≤109, a_i > b_iai​>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.

Sample 1

InputcopyOutputcopy
4 21
10 8
7 4
3 1
5 4
2

Sample 2

InputcopyOutputcopy
4 16
10 8
7 4
3 1
5 4
-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 \le 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 \le 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.

#include<cstdio>
#include<algorithm>
using namespace std;
long long n,m,ans=0,cnt=0,x,y,flag=0,a[10000]; 
int main()
{
	scanf("%lld%lld",&n,&m);
	  for(int i=1;i<=n;i++)
	  {
	  	scanf("%lld &lld",&x,&y);
	  	a[i]=x-y; ans+=x;cnt+=y;
	  }
	  if(cnt>m)
	  {
	  	printf("-1\n");
	  	return 0;
	  }
	  sort(a+1,a+1+n);
	  for(int i=n;i>=1;i--)
	  {
	  	if(ans<=m) break;
	  	else
	  	{
	  		ans=ans-a[i];
	  		flag++;
		  }
	  }
	  printf("%d\n",flag);
	  return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是你的奥奥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值