Given five integers **N**, **A**, **B**, **X** and **Y**. The task is to find the maximum profit obtained from the numbers from the range **[1, N]**. If a positive number is divisible by **A** then the profit increases by **X** and if a positive number is divisible by **B** then the profit increases by **Y**.

**Note:** Profit from a positive number can be added at most once.

**Examples:**

Input:N = 3, A = 1, B = 2, X = 3, Y = 4

Output:10

1, 2 and 3 are divisible by A.

2 is the only number in the given range which is divisible by B.

2 is divisible by both A and B.

1 and 3 can be divided by A to get the profit of 2 * 3 = 6

2 can be divided by B to get the profit of 1 * 4 = 4

2 is divisible by both but in order to maximise the profit it is divided by B instead of A.

Input:N = 6, A = 6, B = 2, X = 8, Y = 2

Output:12

**Approach:** Easy to see that we can divide a number with both **A** and **B** only if the number is a multiple of **lcm(A, B)**. Obviously, that number should be divided with the number that gives more profit.

So the answer equals to **X * (N / A) + Y * (N / B) – min(X, Y) * (N / lcm(A, B))**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum profit ` `int` `maxProfit(` `int` `n, ` `int` `a, ` `int` `b, ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `res = x * (n / a); ` ` ` `res += y * (n / b); ` ` ` ` ` `// min(x, y) * n / lcm(a, b) ` ` ` `res -= min(x, y) * (n / ((a * b) / __gcd(a, b))); ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6, a = 6, b = 2, x = 8, y = 2; ` ` ` `cout << maxProfit(n, a, b, x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to return the maximum profit ` `static` `int` `maxProfit(` `int` `n, ` `int` `a, ` `int` `b, ` ` ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `res = x * (n / a); ` ` ` `res += y * (n / b); ` ` ` ` ` `// min(x, y) * n / lcm(a, b) ` ` ` `res -= Math.min(x, y) * (n / ((a * b) / __gcd(a, b))); ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `6` `, a = ` `6` `, b = ` `2` `, x = ` `8` `, y = ` `2` `; ` ` ` `System.out.println(maxProfit(n, a, b, x, y)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd ` ` ` `# Function to return the maximum profit ` `def` `maxProfit(n, a, b, x, y) : ` ` ` ` ` `res ` `=` `x ` `*` `(n ` `/` `/` `a); ` ` ` `res ` `+` `=` `y ` `*` `(n ` `/` `/` `b); ` ` ` ` ` `# min(x, y) * n / lcm(a, b) ` ` ` `res ` `-` `=` `min` `(x, y) ` `*` `(n ` `/` `/` `((a ` `*` `b) ` `/` `/` ` ` `gcd(a, b))); ` ` ` `return` `res; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `6` `;a ` `=` `6` `; b ` `=` `2` `; x ` `=` `8` `; y ` `=` `2` `; ` ` ` ` ` `print` `(maxProfit(n, a, b, x, y)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to return the maximum profit ` `static` `int` `maxProfit(` `int` `n, ` `int` `a, ` `int` `b, ` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `res = x * (n / a); ` ` ` `res += y * (n / b); ` ` ` ` ` `// min(x, y) * n / lcm(a, b) ` ` ` `res -= Math.Min(x, y) * (n / ((a * b) / __gcd(a, b))); ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 6, a = 6, b = 2, x = 8, y = 2; ` ` ` `Console.WriteLine(maxProfit(n, a, b, x, y)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` `return` `__gcd(` `$b` `, ` `$a` `% ` `$b` `); ` ` ` `} ` ` ` `// Function to return the maximum profit ` `function` `maxProfit(` `$n` `, ` `$a` `, ` `$b` `, ` `$x` `, ` `$y` `) ` `{ ` ` ` `$res` `= ` `$x` `* (` `$n` `/ ` `$a` `); ` ` ` `$res` `+= ` `$y` `* (` `$n` `/ ` `$b` `); ` ` ` ` ` `// min(x, y) * n / lcm(a, b) ` ` ` `$res` `-= min(` `$x` `, ` `$y` `) * (` `$n` `/ ` ` ` `((` `$a` `* ` `$b` `) / __gcd(` `$a` `, ` `$b` `))); ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver code ` `$n` `= 6; ` `$a` `= 6; ` `$b` `= 2; ` `$x` `= 8; ` `$y` `= 2; ` `print` `(maxProfit(` `$n` `, ` `$a` `, ` `$b` `, ` `$x` `, ` `$y` `)); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

12

## Recommended Posts:

- Maximize the total profit of all the persons
- Maximize Profit by trading stocks based on given rate per day
- Check the divisibility of Hexadecimal numbers
- Maximize the numbers of splits in an Array having sum divisible by 3
- Maximize the summation of numbers in a maximum of K moves in range [L, R]
- Check divisibility by 7
- Repeated Unit Divisibility
- Divisibility by 12 for a large number
- Sub-string Divisibility by 11 Queries
- Sub-string Divisibility by 3 Queries
- Check if a large number is divisibility by 15
- Check divisibility in a binary stream
- Divisibility by 64 with removal of bits allowed
- To check divisibility of any large number by 999
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Maximum path sum for each position with jumps under divisibility condition
- Program to calculate Profit Or Loss
- Program to calculate the profit sharing ratio
- Maximum profit such that total stolen value is less than K to get bonus
- Maximum profit by selling N items at two markets

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.