- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find maximum profit after cutting rods and selling same length rods in Python

Suppose we have a list of rod lengths called rodLen. We also have another two integers called profit and cost, represents profit per length and cost per cut. We can make gain profit per unit length of a rod but we can only sell rods that are all of the same length. We can also cut a rod into two pieces such that their lengths are integers, but we have to pay cost amount for each cut. We can cut a rod as many times as we want. We have to find the maximum profit that we can make.

So, if the input is like rodLen = [7, 10] profit = 6 cost = 4, then the output will be 82, as we can cut the rod of length 7 into two rods, with length 5 and 2. We can then cut the rod of length 10 into two rods, both with length 5. Then sell all 3 rods of length 5 for a total profit of (5 + 5 + 5) * 6 - (2*4) = 82.

To solve this, we will follow these steps −

- n := size of rodLen
- if n is same as 0, then
- return 0

- l_max := maximum of rodLen
- p_max := 0
- for cuts in range 1 to l_max, do
- p_cut := 0
- for each rod_len in rodLen, do
- if rod_len < cuts, then
- go for next iteration

- c_count := rod_len / cuts
- total_len := c_count * cuts
- if rod_len is same as total_len, then
- c_count := c_count - 1

- curr_profit := total_len * profit - cost * c_count
- if curr_profit < 0, then
- go for next iteration

- p_cut := p_cut + curr_profit

- if rod_len < cuts, then
- p_max := maximum of p_max and p_cut

- return p_max

## Example

Let us see the following implementation to get better understanding −

def solve(rodLen, profit, cost): n = len(rodLen) if n == 0: return 0 l_max = max(rodLen) p_max = 0 for cuts in range(1, l_max + 1): p_cut = 0 for rod_len in rodLen: if rod_len < cuts: continue c_count = rod_len // cuts total_len = c_count * cuts if rod_len == total_len: c_count -= 1 curr_profit = total_len * profit - cost * c_count if curr_profit < 0: continue p_cut += curr_profit p_max = max(p_max, p_cut) return p_max rodLen = [7, 10] profit = 6 cost = 4 print(solve(rodLen, profit, cost))

## Input

[7, 10], 6, 4

## Output

82

- Related Articles
- Program to find maximum profit after buying and selling stocks at most two times in python
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum profit by cutting the rod of different length in C++
- Maximum profit after buying and selling the stocks in C++
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum length of k ribbons of same length in Python
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- Three measuring rods are 64 cm, 80 cm and 96 cm in length respectively. Find the least length of cloth that can be measured an exact number of times using any one of the above rods.
- What are Rods and Cones in an human eye?
- Program to find maximum element after decreasing and rearranging in Python
- Maximum profit by buying and selling a share at most twice
- Program to find number of items left after selling n items in python