Maximum profit by selling N items at two markets | C ++ implementation of the approach ; Max profit will be saved here ; loop to check all possible combinations of sales ; the sum of the profit after the sale for products 0 to i in market A ; the sum of the profit after the sale for products i to n in market B ; Replace the value of Max Profit with a bigger value among maxP and sumA + sumB ; Return the value of Max Profit ; Driver Program11111111111111111111111
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( vector < int > a , vector < int > b , int n ) { int maxP = -1 ; for ( int i = 0 ; i < n + 1 ; i ++ ) { int sumA = 0 ; for ( int j = 0 ; j < min ( i , ( int ) a . size ( ) ) ; j ++ ) sumA += a [ j ] ; int sumB = 0 ; for ( int j = i ; j < b . size ( ) ; j ++ ) sumB += b [ j ] ; maxP = max ( maxP , sumA + sumB ) ; } return maxP ; } int main ( ) { vector < int > a = { 2 , 3 , 2 } ; vector < int > b = { 10 , 30 , 40 } ; cout << maxProfit ( a , b , 4 ) ; return 0 ; }