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
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . Min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } static void Main ( ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; Console . WriteLine ( maxProfit ( n , a , b , x , y ) ) ; } }