Installing Apps
Time limit2sMemory limit512 MB
Choose a largest subset of apps installable within c free space and order them so each install fits, preferring the lexicographically smallest index set.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Sandra recently bought her first smartphone. A friend gave her a long list of applications (apps) that she should install on it. Sandra started installing the apps from the list right away, but after a few installs the phone no longer had enough disk space for any more. Sometimes an installation failed because there was not even enough space to download the installation package. Other apps downloaded fine but then had too little space to store the installed app.
Each app has a download size and a storage size . To download the app, the phone needs at least megabytes of free disk space. Once the app is installed, it occupies megabytes of disk space on the phone. The download size can be smaller than the storage size (for example, when the app data is heavily compressed) or larger than the storage size (for example, when the download carries material that may never be used, such as translations into other languages). The installer is very efficient and turns the downloaded package into an installed app without using any extra disk space. So, to install an app, the phone needs at least megabytes of free disk space.
Sandra soon realised that she may have run out of space only because she installed the apps in the wrong order. She uninstalled every app and now wants to pick an installation order that lets her install as many apps from the list as possible. Sandra installs each app at most once.
Help her decide which apps on the list to install, and in which order.
Input
The input consists of:
- One line with two integers and (, ), the number of available apps and the free disk space of the phone in megabytes.
- lines, each with two integers and (), the download size and the storage size of an app, in megabytes.
Output
On the first line, print the maximum number of apps that can be installed. If is at least 1, print a second line with the numbers of those apps, separated by single spaces, in the order Sandra should install them. If no app can be installed, print only the first line.
The apps are numbered from 1 to in input order. Several sets of apps may be installable, so the accepted answer is fixed as follows. Among all sets of apps that can be installed in some order, choose the set whose app numbers, written in increasing order, form the lexicographically smallest sequence. Of two sequences, the one with the smaller number at the first position where they differ is lexicographically smaller. Print that set sorted by in decreasing order, and by app number in increasing order among apps with the same value of . Any set that can be installed in some order can also be installed in this order, so the printed order is always valid.