You can use formulas.
The first three cells to be filled manually. With the fourth and enter the formula below*:
We get the desired result. Yes, the formula could be done smarter and prettier, but it works, the cost of the solution should not be unreasonably high :)
* if excel English, if Russian, the formula should be translated, thanks to Microsoft!