C++ Program to find out the maximum amount of money that can be made from selling cars


Suppose, there is a demand for red and blue cars for sale. An automobile company decides to sell p red cars and q blue cars of different prices. Currently, the company has 'a' number of red cars, 'b' number of blue cars, and 'c' numbers of colorless cars (the cars are yet to be painted) in their stock. The values of the different cars are given in arrays A, B, and C. So, the company has to sell p + q number of cars in a day and they have to make maximum profit from them. The colorless cars can be painted in any color, red or blue. We find out the maximum amount of profit that can be achieved from selling the cars.

So, if the input is like p = 3, q = 3, a = 3, b = 3, c = 2, A = {150000, 200000, 200000}, B = {150000, 120000, 180000}, C = {210000, 160000, 150000}, then the output will be 1100000.

The company can sell blue cars of value 200000, 200000 and paint the car of value 210000 to blue. Total value obtained from selling blue cars will be 610000. Also, they can sell red car of value 18000 and paint cars of value 160000 and 150000 to gain a total of 490000. The total profit value obtained will be 610000 + 490000 = 1100000.

To solve this, we will follow these steps −

Define an array dp
sort the arrays A, B, and C
for initialize i := 0, when i < p, update (increase i by 1), do:
   insert A[i] at the end of dp
for initialize i := 0, when i < q, update (increase i by 1), do:
   insert B[i] at the end of dp
sort the array dp
reverse the array dp
for initialize i := 1, when i < size of dp, update (increase i by 1), do:
   dp[i] := dp[i] + dp[i - 1]
tmp := 0
res := last element of dp
for initialize i := 1, when i < (minimum of (c and p +q), update (increase i by 1), do:
   tmp := tmp + C[i - 1]
   res := maximum of (res and dp[p + q - i] + tmp)
return res

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(int p, int q, int a, int b, int c, vector<int> A, vector<int> B, vector<int> C){
   vector<int> dp(1, 0);
   sort(A.rbegin(), A.rend());
   sort(B.rbegin(), B.rend());
   sort(C.rbegin(), C.rend());
   for(int i = 0; i < p; ++i)
      dp.push_back(A[i]);
   for(int i = 0; i < q; ++i) 
      dp.push_back(B[i]);
   sort(dp.begin(), dp.end());
   reverse(dp.begin() + 1, dp.end());
   for(int i = 1; i < (int)dp.size(); ++i)
      dp[i] += dp[i - 1];
   int tmp = 0;
   int res = dp.back();
   for(int i = 1; i <= min(c, p + q); ++i) {
      tmp += C[i - 1];
       res = max(res, dp[p + q - i] + tmp);
   }
   return res;
}
int main() {
   int p = 3, q = 3, a = 3, b = 3, c = 2;
   vector<int> A = {150000, 200000, 200000}, B = {150000, 120000, 180000}, C = {210000, 160000, 150000};
   cout<< solve(p, q, a, b, c, A, B, C);
   return 0;
}

Input

3, 3, 3, 3, 2, {150000, 200000, 200000}, {150000, 120000, 180000}, {210000, 160000, 150000}

Output

1100000

Updated on: 02-Mar-2022

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements