Some crazy white old man, calling himself the “Danger”, after watching a certain T.V series planned to research and invent his own “chemical”. After finishing his recipe, he found out he needs exactly n grams of the main ingredient to make it. If he makes with less than n grams, the mixture is not reactive enough and if he makes it with more than n grams, it simply explodes. He browsed Ebay and found only 2 sellers for the item. Seller A only sells n1 gram containers for c1 rupees each. Seller B only sells n2 gram containers for c2 rupees each. If it’s possible to buy exactly n grams from these 2 sellers, output the minimum cost of buying the ingredients

I tried using linear diaphontine equations, but the solution fails for large inputs.

Can someone help me solving this problem?

Link to my approach

I found one of the solutions and tried to make the both values positive.