C++20 sections · 1024 units
Open in Course

Dragons - Algorithm

Breaking it down

Here's your approach:

1.1. Read your initial strength ss and number of dragons nn.

2.2. Store each dragon as a pair (strength, bonus) in a vector.

3.3. Sort the vector by dragon strength in ascending order.

4.4. Loop through the sorted dragons.

5.5. For each dragon, check if your current strength is greater than the dragon's strength.

6.6. If yes, add the bonus to your strength and continue.

7.7. If no, output "NO" and stop.

8.8. If you defeat all dragons, output "YES". This solution combines sorting with sequential checking. You sort once, then process in order. The sort does the hard work of finding the right sequence for you.