C - Songs Compression

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

Inputcopy Outputcopy
4 21 10 8 7 4 3 1 5 4 2

Sample 2

Inputcopy Outputcopy
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.

题意翻译

现有 n(1≤n≤10^5)个物品,第 i 个物品的重量为 ai​,可以进行 1 次操作使 ai​ 变为 bi​(1≤bi​<ai​≤10^9)。

问最少多少次操作后能将所有物品装入空间为 mm(1≤m≤10^9)的背包。

若无论如何也无法完成,请输出 -1

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll n, m; 

ll a[N]; //记录体积压缩前后的差

//记得开long long,输入用scanf,不然会超时
int main()
{
	scanf_s("%lld%lld", &n, &m);
	ll sum1 = 0, sum2 = 0;

	for (ll i = 1; i <= n; i++)
	{
		ll num1, num2;
		scanf_s("%lld%lld", &num1, &num2);
		a[i] = num1 - num2;//记录体积差
		sum1 += num1;      //记录压缩前的体积和
		sum2 += num2;      //记录压缩后的体积和
	}

	if (sum2 > m)   //如果把所有歌曲都压缩还装不下,则返回-1
	{
		cout << -1 << endl;
		return 0;
	}

	sort(a + 1, a + n + 1,greater<int>());  //对体积差进行降序排序

	ll i = 1, cnt = 0;

	while (sum1 > m)
	{
		sum1 -= a[i];  
		i++;
		cnt++;
	}

	cout << cnt << endl;
}
相关推荐
不会打代码呜呜呜呜1 小时前
小白零基础--CPP多线程
开发语言·c++·算法
涛ing1 小时前
【5. C++ 变量作用域及其深入探讨】
java·linux·c语言·开发语言·c++·ubuntu·vim
SY师弟1 小时前
蓝桥杯单片机第七届省赛
c语言·c++·单片机·嵌入式硬件·职场和发展·蓝桥杯
Hi Man1 小时前
Python之如何在Visual Studio Code 中写的python程序打包成可以在Windows系统下运行的.exe程序
开发语言·vscode·python
CHANG_THE_WORLD2 小时前
C++并发编程指南04
开发语言·c++
轩情吖2 小时前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
powershell 与 api2 小时前
C#,shell32 + 调用控制面板项(.Cpl)实现“新建快捷方式对话框”(全网首发)
开发语言·windows·c#·.net
SomeB1oody2 小时前
【Rust自学】19.2. 高级trait:关联类型、默认泛型参数和运算符重载、完全限定语法、supertrait和newtype
开发语言·后端·rust
山茶花开时。3 小时前
[SAP ABAP] 静态断点的使用
开发语言·sap·abap
纠结哥_Shrek3 小时前
Java 有很多常用的库
java·开发语言