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