# C++ program to find minimum how much rupees we have to pay to buy exactly n liters of water

Suppose we have three numbers n, a and b. We want to buy n liters of water. There are only two types of water bottles nearby, 1-liter bottles and 2-liter bottles. The bottle of the first type a rupees and the bottle of the second type costs b rupees. We want to spend as few money as possible. We have to find the minimum amount of money we need to buy exactly n liters of water.

So, if the input is like n = 7; a = 3; b = 2, then the output will be 9, because with 3 2-liter bottles we can get 6 liters of water with price 6, then one 1 liter bottle is needed for cost 3.

## Steps

To solve this, we will follow these steps −

b := minimum of a * 2 and b
return (n / 2 * b) + (n mod 2) * a

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(int n, int a, int b) {
b = min(a * 2, b);
return n / 2 * b + n % 2 * a;
}
int main() {
int n = 7;
int a = 3;
int b = 2;
cout << solve(n, a, b) << endl;
}

## Input

7, 3, 2

## Output

9