#K46607. Maximizing Magic Tricks
Maximizing Magic Tricks
Maximizing Magic Tricks
Alice is practicing her magic tricks, but each trick requires a certain number of magic points to perform. She has an initial total of (M) magic points and there are (N) available tricks. Each trick costs a certain number of magic points given in an array (P). In order to perform a trick, Alice must have at least as many points as the trick demands. Once she performs a trick, her magic points decrease by the trick's cost, but she regains (\lfloor \frac{P_i}{2} \rfloor) points as a reward. The tricks can be attempted in any order; however, one optimal strategy is to sort the trick costs in ascending order. Your task is to determine the maximum number of tricks Alice can perform.
The process can be formally described as follows:
- Sort the array (P) in non-decreasing order.
- For each trick with cost (P_i):
- If the current magic points (C) satisfy (C \geq P_i), perform the trick by updating (C = C - P_i + \lfloor \frac{P_i}{2} \rfloor) and increment the count of tricks.
- Otherwise, stop and return the count.
Determine and output the maximum number of tricks that can be performed.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers (N) and (M), where (N) is the number of magic tricks and (M) is the initial number of magic points.
- The second line contains (N) space-separated integers representing the trick costs in the array (P).
outputFormat
Output a single integer to standard output (stdout) -- the maximum number of tricks Alice can perform.## sample
3 10
4 6 8
2