Problem Statement: Minimum Cost to Buy Ninja Blades

Ninja Yuki wants to purchase ninja blades at the Spring Fair in his village. Initially, he has 0 blades, and his goal is to buy 'N' blades. The merchant offers a peculiar pricing mechanism for the blades:

  • Buy 1 blade for a cost of 'A'. This increases the total number of blades to 'K+1'.
  • Buy additional 'K' blades for a cost of 'B', doubling the total to '2*K' blades.

Your task is to help Yuki find the minimum cost required to obtain exactly 'N' blades.

Input:

The first line contains an integer 'T', the number of test cases.
For each test case, the first line contains three space-separated integers 'N', 'A', and 'B':
  • 'N' is the total number of blades Yuki wants.
  • 'A' is the cost to add 1 blade.
  • 'B' is the cost to double the current number of blades.

Output:

Return the minimum cost needed to acquire 'N' blades for each test case.

Example:

Input:
1
5 2 1
Output:
6

Explanation:

To acquire 5 blades at the minimum cost:
1) Add 1 blade to 0 blades at cost 2. Total: 1 blade, Cost: 2
2) Double 1 to get 2 blades at cost 1. Total: 2 blades, Cost: 3
3) Double 2 to get 4 blades at cost 1. Total: 4 blades, Cost: 4
4) Add 1 blade to get 5 blades at cost 2. Total: 5 blades, Cost: 6

Constraints:

  • 1 <= 'T' <= 100
  • 1 <= 'N' <= 104
  • 0 <= 'A', 'B' <= 103

Note: Implement the function to calculate the minimum cost, as no output printing is required.

AnswerBot
4mo

Calculate the minimum cost to acquire a specific number of ninja blades using a given pricing mechanism.

  • Iterate through each test case to determine the minimum cost needed to acquire the desired numbe...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Oyo Rooms

Q. Leaves at Same Level Problem Statement Given a binary tree with 'N' nodes, deter...read more
Q. What is the difference between Mutex and Semaphores?
Q. LRU Cache Design Question Design a data structure for a Least Recently Used (LRU...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits