All problems

Problem 476

Bag of Tokens

Medium · 20 pts · Array, Two Pointers, Greedy, Sorting

Problem

You start with power P and a score of 0, and you have a bag of tokens with values. Each token can be played at most once, in one of two ways. Face up: if your power is at least the token's value, lose that much power and gain 1 score. Face down: if your score is at least 1, gain that much power and lose 1 score. Find the largest score you can have at any moment. Greedily, play the cheapest tokens face up, and when stuck, trade one score for the most valuable token's power.

Input

First line: N and P. Second line: the N token values (empty if N = 0).

Output

Print the maximum score.

Constraints

0 <= N <= 1000; 0 <= value, P <= 10000

Example input

4 200
100 200 300 400

Example output

2

Explanation

With P = 200 and tokens 100 200 300 400: play 100 face up (score 1, power 100), play 400 face down (score 0, power 500), then play 200 and 300 face up (score 2). The best score is 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Broken Calculator Next: Reduce Array Size to Half