# Minimize the cost of buying the Objects

Given **‘T’** which represents the total number of items a person has. **‘P’** represents the price of each item and **‘M’** represents the number of free items he gets **free** if he buys **‘N’** items. The task is to find the total amount the person has to pay to get T items.

**Examples:**

Input:T = 13, P = 10, N = 3, M = 1

Output:Amount = 100

Explaination:

Total number of fruit bottle a person has = 13

Offer available is buy 3 get 1 free

So, person has to buy 9 Fruit juice bottle to get 3 fruit juice bottle for free.

Now, since the total number of the bottle is 13 so the person buys 1 bottle at P price.

The total amount the person has to pay = (9 + 1) * 10 i.e 100

Input:T = 12, P = 8, N = 2, M = 1

Output:Amount = 64

**Approach:**

- First of all, we should try to get free items as much as possible, so this can reduce the cost.
- Divide the total number of items with the sum of
**N**and**M**because we get**M**free items when we buy atleast**N**items. - Then calculate the total number of items for which you have to pay for by subtracting the free items from the total number of items
- Finally the price can be calculated by multiplying the cost of one item with the total number of items.

Below is the implementation of the above approach:

## C++

`// C++ program of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that will calculate the price ` `int` `totalPay(` `int` `totalItems, ` `int` `priceOfOneItem, ` ` ` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `freeItems = 0, actual = 0; ` ` ` ` ` `// Calculate the number of items ` ` ` `// we can get for free ` ` ` `freeItems = totalItems / (N + M); ` ` ` ` ` `// Calculate the number of items ` ` ` `// we will have to pay the price for ` ` ` `actual = totalItems - freeItems; ` ` ` ` ` `// Calculate the price ` ` ` `int` `amount = actual * priceOfOneItem; ` ` ` ` ` `return` `amount; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `T = 12, P = 8; ` ` ` `int` `N = 2, M = 1; ` ` ` ` ` `// Calling function ` ` ` `cout << ` `"Amount = "` ` ` `<< totalPay(T, P, N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function that will calculate the price ` `static` `int` `totalPay(` `int` `totalItems, ` ` ` `int` `priceOfOneItem, ` ` ` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `freeItems = ` `0` `, actual = ` `0` `; ` ` ` ` ` `// Calculate the number of items ` ` ` `// we can get for free ` ` ` `freeItems = totalItems / (N + M); ` ` ` ` ` `// Calculate the number of items ` ` ` `// we will have to pay the price for ` ` ` `actual = totalItems - freeItems; ` ` ` ` ` `// Calculate the price ` ` ` `int` `amount = actual * priceOfOneItem; ` ` ` ` ` `return` `amount; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `T = ` `12` `, P = ` `8` `; ` ` ` `int` `N = ` `2` `, M = ` `1` `; ` ` ` ` ` `// Calling function ` ` ` `System.out.print(` `"Amount = "` `+ ` ` ` `totalPay(T, P, N, M)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program of above approach ` ` ` `# Function that will calculate the price ` `def` `totalPay(totalItems, priceOfOneItem, N, M): ` ` ` `freeItems ` `=` `0` ` ` `actual ` `=` `0` ` ` ` ` `# Calculate the number of items ` ` ` `# we can get for free ` ` ` `freeItems ` `=` `totalItems ` `/` `/` `(N ` `+` `M) ` ` ` ` ` `# Calculate the number of items ` ` ` `# we will have to pay the price for ` ` ` `actual ` `=` `totalItems ` `-` `freeItems ` ` ` ` ` `# Calculate the price ` ` ` `amount ` `=` `actual ` `*` `priceOfOneItem ` ` ` ` ` `return` `amount ` ` ` `# Driver code ` `T ` `=` `12` `P ` `=` `8` `N ` `=` `2` `M ` `=` `1` ` ` `# Calling function ` `print` `(` `"Amount = "` `, totalPay(T, P, N, M)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function that will calculate the price ` `static` `int` `totalPay(` `int` `totalItems, ` ` ` `int` `priceOfOneItem, ` ` ` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `freeItems = 0, actual = 0; ` ` ` ` ` `// Calculate the number of items ` ` ` `// we can get for free ` ` ` `freeItems = totalItems / (N + M); ` ` ` ` ` `// Calculate the number of items ` ` ` `// we will have to pay the price for ` ` ` `actual = totalItems - freeItems; ` ` ` ` ` `// Calculate the price ` ` ` `int` `amount = actual * priceOfOneItem; ` ` ` ` ` `return` `amount; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `T = 12, P = 8; ` ` ` `int` `N = 2, M = 1; ` ` ` ` ` `// Calling function ` ` ` `Console.Write(` `"Amount = "` `+ ` ` ` `totalPay(T, P, N, M)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

Amount = 64

## Recommended Posts:

- Min Cost Path | DP-6
- Maximum profit by buying and selling a share at most twice
- Maximum profit by buying and selling a share at most k times
- Minimize the absolute difference of sum of two subsets
- Maximum profit after buying and selling the stocks
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Minimize the difference between the maximum and minimum values of the modified array
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimize the value of N by applying the given operations
- Find Selling Price from given Profit Percentage and Cost
- Minimum cost to cover the given positions in a N*M grid
- Find cost price from given selling price and profit or loss percentage
- Cost of painting n * m grid
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Minimize sum of adjacent difference with removal of one element from array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.